| 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 | --force \ |
| 60 | --prune-empty \ |
| 61 | --index-filter ' |
| 62 | grep -Fvxf /tmp/tree.new /tmp/tree.old \ |
| 63 | | xargs git rm --cached -r --ignore-unmatch |
| 64 | ' \ |
| 65 | HEAD |
| 66 | ``` |
| 67 | |
| 68 | |
| 69 | Inserting a new root commit |
| 70 | --------------------------- |
| 71 | ```sh |
| 72 | git checkout --orphan $TEMP_BRANCH |
| 73 | git rm -rf * |
| 74 | git commit --allow-empty -m $INIT_COMMIT_MSG |
| 75 | git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH |
| 76 | git branch -d $TEMP_BRANCH |
| 77 | ``` |
| 78 | |
| 79 | |
| 80 | Deleting all tags, locally and remotely |
| 81 | --------------------------------------- |
| 82 | ```sh |
| 83 | for tag in `git tag`; |
| 84 | do |
| 85 | git tag -d $tag |
| 86 | git push $REMOTE_NAME :refs/tags/$tag |
| 87 | done |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | Get an older version of a file |
| 92 | ------------------------------ |
| 93 | |
| 94 | ```sh |
| 95 | git cat-file -p $COMMIT_DIGEST:$FILE_PATH |
| 96 | ``` |
| 97 | |
| 98 | |
| 99 | Splitting a commit |
| 100 | ------------------ |
| 101 | |
| 102 | From [docs](https://git-scm.com/docs/git-rebase#_splitting_commits): |
| 103 | |
| 104 | - Start an interactive rebase with `git rebase -i <commit>^`, where `<commit>` |
| 105 | is the commit you want to split. |
| 106 | - Mark the commit you want to split with the action "edit". |
| 107 | - When it comes to editing that commit, execute `git reset HEAD^`. The effect |
| 108 | is that the HEAD is rewound by one, and the index follows suit. However, the |
| 109 | working tree stays the same. |
| 110 | - Now add the changes to the index that you want to have in the first commit |
| 111 | (`git add`) |
| 112 | - Commit the now-current index with whatever commit message is appropriate now. |
| 113 | - Repeat the last two steps until your working tree is clean. |
| 114 | - Continue the rebase with `git rebase --continue`. |
| 115 | |
| 116 | |
| 117 | Merge 2 repos |
| 118 | ------------- |
| 119 | |
| 120 | ```sh |
| 121 | $ ls -1 |
| 122 | repo_A |
| 123 | repo_B |
| 124 | $ cd repo_B |
| 125 | $ git remote add repo_A ../repo_A |
| 126 | $ git fetch repo_A --tags |
| 127 | $ git merge --allow-unrelated-histories repo_A/$BRANCH |
| 128 | $ git remote remove repo_A |
| 129 | ``` |