Add commit-splitting instructions from docs
[git-cheat-sheet.git] / README.md
CommitLineData
95f663c4
SK
1git-cheat-sheet
2===============
3
4How to do stuff with git
6b6ba9f3
SK
5
6
7Splitting a repo
8----------------
9
aa63a549 10##### Remove history of all but select files in cloned repo
6b6ba9f3 11```sh
fe790851 12$ git clone old new
6b6ba9f3
SK
13$ cd new
14$ git clean -dfx
15$ git gc --aggressive --prune=now
16$ git remote rm origin
17$ git filter-branch \
18 --prune-empty \
19 --index-filter '
20 git ls-tree -r --name-only HEAD \
21 | grep -v file_i_want_to_keep_1 \
22 | grep -v file_i_want_to_keep_.. \
23 | grep -v file_i_want_to_keep_n \
24 | xargs git rm --cached -r --ignore-unmatch
25 ' \
26 HEAD
27$ git gc --aggressive --prune=now
28```
29
aa63a549 30##### Remove history of previously-removed (not in current tree) files
6b6ba9f3 31```sh
3fd4f973
SK
32$ git log --pretty=format: --name-status \
33 | awk '$0 != "" {print $2}' \
34 | sort -u > /tmp/tree.old
6b6ba9f3 35$ git ls-tree -r --name-only HEAD > /tmp/tree.new
3fd4f973
SK
36$ git filter-branch \
37 --prune-empty \
38 --index-filter '
39 grep -Fvxf /tmp/tree.new /tmp/tree.old \
40 | xargs git rm --cached -r --ignore-unmatch
41 ' \
42 HEAD
6b6ba9f3 43```
c32e7894
SK
44
45
46Inserting a new root commit
47---------------------------
48```sh
49git checkout --orphan $TEMP_BRANCH
50git rm -rf .
51git commit --allow-empty -m $INIT_COMMIT_MSG
52git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH
53git branch -d $TEMP_BRANCH
54```
524e725b
SK
55
56
57Deleting all tags, locally and remotely
58---------------------------------------
59```sh
60for tag in `git tag`;
61do
62 git tag -d $tag
b5a37a6e 63 git push $REMOTE_NAME :refs/tags/$tag
524e725b
SK
64done
65```
66329b94
SK
66
67
68Get an older version of a file
69------------------------------
70
71```sh
72git cat-file -p $COMMIT_DIGEST:$FILE_PATH
73```
0c3f98d9
SK
74
75
76Splitting a commit
77------------------
78
79From [docs](https://git-scm.com/docs/git-rebase#_splitting_commits):
80
81- Start an interactive rebase with `git rebase -i <commit>^`, where `<commit>`
82 is the commit you want to split.
83- Mark the commit you want to split with the action "edit".
84- When it comes to editing that commit, execute `git reset HEAD^`. The effect
85 is that the HEAD is rewound by one, and the index follows suit. However, the
86 working tree stays the same.
87- Now add the changes to the index that you want to have in the first commit
88 (`git add`)
89- Commit the now-current index with whatever commit message is appropriate now.
90- Repeat the last two steps until your working tree is clean.
91- Continue the rebase with `git rebase --continue`.
This page took 0.022776 seconds and 4 git commands to generate.