Add git add -p
[git-cheat-sheet.git] / README.md
1 git-cheat-sheet
2 ===============
3
4 How to do stuff with git
5
6 Staging a subset of changes in a file
7 -------------------------------------
8
9 git add -p
10
11 y - stage this hunk
12 n - do not stage this hunk
13 q - quit; do not stage this hunk or any of the remaining ones
14 a - stage this hunk and all later hunks in the file
15 d - do not stage this hunk or any of the later hunks in the file
16 g - select a hunk to go to
17 / - search for a hunk matching the given regex
18 j - leave this hunk undecided, see next undecided hunk
19 J - leave this hunk undecided, see next hunk
20 k - leave this hunk undecided, see previous undecided hunk
21 K - leave this hunk undecided, see previous hunk
22 s - split the current hunk into smaller hunks
23 e - manually edit the current hunk
24 ? - print help
25
26 https://git-scm.com/docs/git-add#Documentation/git-add.txt-patch
27
28
29 Splitting a repo
30 ----------------
31
32 ##### Remove history of all but select files in cloned repo
33 ```sh
34 $ git clone old new
35 $ cd new
36 $ git clean -dfx
37 $ git gc --aggressive --prune=now
38 $ git remote rm origin
39 $ git filter-branch \
40 --prune-empty \
41 --index-filter '
42 git ls-tree -r --name-only HEAD \
43 | grep -v file_i_want_to_keep_1 \
44 | grep -v file_i_want_to_keep_.. \
45 | grep -v file_i_want_to_keep_n \
46 | xargs git rm --cached -r --ignore-unmatch
47 ' \
48 HEAD
49 $ git gc --aggressive --prune=now
50 ```
51
52 ##### Remove history of previously-removed (not in current tree) files
53 ```sh
54 $ git log --pretty=format: --name-status \
55 | awk '$0 != "" {print $2}' \
56 | sort -u > /tmp/tree.old
57 $ git ls-tree -r --name-only HEAD > /tmp/tree.new
58 $ git filter-branch \
59 --prune-empty \
60 --index-filter '
61 grep -Fvxf /tmp/tree.new /tmp/tree.old \
62 | xargs git rm --cached -r --ignore-unmatch
63 ' \
64 HEAD
65 ```
66
67
68 Inserting a new root commit
69 ---------------------------
70 ```sh
71 git checkout --orphan $TEMP_BRANCH
72 git rm -rf *
73 git commit --allow-empty -m $INIT_COMMIT_MSG
74 git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH
75 git branch -d $TEMP_BRANCH
76 ```
77
78
79 Deleting all tags, locally and remotely
80 ---------------------------------------
81 ```sh
82 for tag in `git tag`;
83 do
84 git tag -d $tag
85 git push $REMOTE_NAME :refs/tags/$tag
86 done
87 ```
88
89
90 Get an older version of a file
91 ------------------------------
92
93 ```sh
94 git cat-file -p $COMMIT_DIGEST:$FILE_PATH
95 ```
96
97
98 Splitting a commit
99 ------------------
100
101 From [docs](https://git-scm.com/docs/git-rebase#_splitting_commits):
102
103 - Start an interactive rebase with `git rebase -i <commit>^`, where `<commit>`
104 is the commit you want to split.
105 - Mark the commit you want to split with the action "edit".
106 - When it comes to editing that commit, execute `git reset HEAD^`. The effect
107 is that the HEAD is rewound by one, and the index follows suit. However, the
108 working tree stays the same.
109 - Now add the changes to the index that you want to have in the first commit
110 (`git add`)
111 - Commit the now-current index with whatever commit message is appropriate now.
112 - Repeat the last two steps until your working tree is clean.
113 - Continue the rebase with `git rebase --continue`.
114
115
116 Merge 2 repos
117 -------------
118
119 ```sh
120 $ ls -1
121 repo_A
122 repo_B
123 $ cd repo_B
124 $ git remote add repo_A ../repo_A
125 $ git fetch repo_A --tags
126 $ git merge --allow-unrelated-histories repo_A/$BRANCH
127 $ git remote remove repo_A
128 ```
This page took 0.044954 seconds and 4 git commands to generate.