Commit | Line | Data |
---|---|---|
95f663c4 SK |
1 | git-cheat-sheet |
2 | =============== | |
3 | ||
4 | How to do stuff with git | |
6b6ba9f3 SK |
5 | |
6 | ||
7 | Splitting a repo | |
8 | ---------------- | |
9 | ||
10 | ### Remove history of all but select files in cloned repo | |
11 | ```sh | |
12 | $ cp -Rp old new | |
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 | ||
30 | ### Remove history of previously-removed (not in current tree) files | |
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 | ||
46 | Inserting a new root commit | |
47 | --------------------------- | |
48 | ```sh | |
49 | git checkout --orphan $TEMP_BRANCH | |
50 | git rm -rf . | |
51 | git commit --allow-empty -m $INIT_COMMIT_MSG | |
52 | git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH | |
53 | git branch -d $TEMP_BRANCH | |
54 | ``` |