Blog by Sumana Harihareswara, Changeset founder

31 Oct 2014, 16:59 p.m.

A Few Intermediate Git Tips

Hi, reader. I wrote this in 2014 and it's now more than five years old. So it may be very out of date; the world, and I, have changed a lot since I wrote it! I'm keeping this up for historical archive purposes, but the me of today may 100% disagree with what I said then. I rarely edit posts after publishing them, but if I do, I usually leave a note in italics to mark the edit and the reason. If this post is particularly offensive or breaches someone's privacy, please contact me.

Today I led an intermediate Git workshop at Hacker School, with occasional help from more experienced Git users. We covered:

  • cherry-picking versus merging a commit from one branch to another
  • git blame [filename] to see who last touched a line
  • git log --full-diff -p [filename] to view full diffs, and a few cool things to put in your .gitconfig to better view your log, e.g., aliasing something to log --oneline --graph --all --decorate -30
  • better search with git grep, and file listing with git ls-files, to only look at the files in your repository (thus ignoring files mentioned in your .gitignore)
  • git add -p to make your commits cleaner and improve your pull requests (with thanks to this blog post by Allison Kaptur)
  • git rebase -i to rewrite history in your branches and thus also improve your pull requests
  • shallow cloning with git clone --depth 1 (demonstrating that it is faster and takes less disk space, but this took a few tries, since Git is so efficient at storing past revisions that the effect barely registers for small, young repositories)
  • git reset and the differences among default, --soft, and --hard
  • ways to talk about history and what git rev-parse does under the hood (and thus HEAD~1 and HEAD^2 and parents and ancestors and whatnot)

Only afterwards did I see this super useful explanation of the Git model which articulates what's actually doing what.

As we were discussing rebase, I said I didn't yet feel smart enough to do non-interactive rebases. My peer Connor frowned at that. I sought a replacement word. Skilled? Experienced? Audacious? Confident? Maybe that last one.

I'm also going to play around with the gitk GUI tool, maybe with tig, and git bisect. And I heard a brilliant suggestion: when you're about to do something in Git that feels scary, in terms of rebasing or resetting or whatnot, clone your repo and try out your idea on the clone!


31 Oct 2014, 17:38 p.m.

Here is a great visual tutorial on git branching: