Add git add -p
[git-cheat-sheet.git] / README.md
CommitLineData
95f663c4
SK
1git-cheat-sheet
2===============
3
4How to do stuff with git
6b6ba9f3 5
402d8d8e
SK
6Staging 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
26https://git-scm.com/docs/git-add#Documentation/git-add.txt-patch
27
6b6ba9f3
SK
28
29Splitting a repo
30----------------
31
aa63a549 32##### Remove history of all but select files in cloned repo
6b6ba9f3 33```sh
fe790851 34$ git clone old new
6b6ba9f3
SK
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
aa63a549 52##### Remove history of previously-removed (not in current tree) files
6b6ba9f3 53```sh
3fd4f973
SK
54$ git log --pretty=format: --name-status \
55 | awk '$0 != "" {print $2}' \
56 | sort -u > /tmp/tree.old
6b6ba9f3 57$ git ls-tree -r --name-only HEAD > /tmp/tree.new
3fd4f973
SK
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
6b6ba9f3 65```
c32e7894
SK
66
67
68Inserting a new root commit
69---------------------------
70```sh
71git checkout --orphan $TEMP_BRANCH
64e53fa7 72git rm -rf *
c32e7894
SK
73git commit --allow-empty -m $INIT_COMMIT_MSG
74git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH
75git branch -d $TEMP_BRANCH
76```
524e725b
SK
77
78
79Deleting all tags, locally and remotely
80---------------------------------------
81```sh
82for tag in `git tag`;
83do
84 git tag -d $tag
b5a37a6e 85 git push $REMOTE_NAME :refs/tags/$tag
524e725b
SK
86done
87```
66329b94
SK
88
89
90Get an older version of a file
91------------------------------
92
93```sh
94git cat-file -p $COMMIT_DIGEST:$FILE_PATH
95```
0c3f98d9
SK
96
97
98Splitting a commit
99------------------
100
101From [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`.
4b6ac450
SK
114
115
116Merge 2 repos
117-------------
118
119```sh
120$ ls -1
121repo_A
122repo_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.02747 seconds and 4 git commands to generate.