Git Tricks

tech, git,

Git tricks

(likely to add how-to’s as we go ahead)

Splitting a diff with Sourcetree

  1. check out a new branch
    1. cherry pick your original diff onto the new branch
    2. or check out the new branch directly on top of your original branch
  2. using sourcetree, revert changes that you don’t want
    1. note: this is Windows and Mac only. 😞
  3. repeat from #1 until you’ve created all your component diffs
  4. optionally stack them using rebase Splitting a diff by hand
  5. make a copy of your branch in case something goes horribly wrong, or be comfortable with reflog
  6. squash everything using git rebase -i
  7. git reset HEAD~1 to unstage your one and only commit
  8. split into one commit per diff
    1. git add <filename> for all the files you want in this commit
      • if you only want some of the changes within a file, use git add -p <filename> , then hit yes/no/split/edit for each chunk
      • you usually won’t need edit
    2. git commit -m <message> (not -a !!!)
    3. Repeat for each commit. You can use git commit -a for the last one.
  9. git branch until you have one branch per split
  10. for each branch, git rebase -i and remove the last N commits that you don’t want
  11. for each branch, arc diff <hash> where hash is the previous git commit hash, so that your diff only has one commit in it. Make sure you specify dependent revisions in phabricator.

NOTE: An alternative to removing commits with rebase is to add commits with git cherry-pick

Squash

  1. git rebase -i
  2. Except for the first line, replace all instances of pick with fixup Merge

in ~/.gitconfig :

[merge]
        conflictstyle = diff3

http://www.ryanzec.com/2014/12/13/git-merge-conflict-style-diff3/

Undo

git reflog git reset --hard <hash> git checkout -b mynewbranch

Rebase

git rebase -i <destination>