Blog by Sumana Harihareswara, Changeset founder
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:
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
.gitconfigto better view your log, e.g., aliasing something to
log --oneline --graph --all --decorate -30
git grep, and file listing with
git ls-files, to only look at the files in your repository (thus ignoring files mentioned in your
git add -pto make your commits cleaner and improve your pull requests (with thanks to this blog post by Allison Kaptur)
git rebase -ito rewrite history in your branches and thus also improve your pull requests
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 resetand the differences among default,
git rev-parsedoes under the hood (and thus
HEAD^2and 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
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.