Useful git #63
thecodedrift
announced in
Thunked
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I love git. It and its other distributed version control systems have changed the way we think and write about code. Not too long ago, we used to commit giant monoliths of code to SVN locations. Complex environments often had to dedicate at least one person to managing branches. It was the complete opposite of good. I've been using git now for over 8 years, and I'm still learning tips and tricks that make my day to day development easier and better. These are some of my favorite commands.
git bisect: When did the code go wrong?
Eventually something will break, and if you're unlucky, it will only happen after a long stream of commits. The git bisect command allows you to begin searching for that commit where things went south. The command looks like:
You'll need to specify a "bad" revision (often
HEAD
), a "good" revision (perhaps from a tag or git log), and then you're off to the races. Git will select a commit exactly halfway between "good" and "bad". So you'll test your code. If it worked, typegit bisect good
, and if it didn't typegit bisect bad
.Every time you tell git if things are good/bad, it will cut the remaining revisions down by half. Before you know it, you'll have zeroed in on the commit that is causing everything to fail. Check the commit log, see what happened, and you'll usually be 99% of the way towards fixing your broken build. All of the bells and whistles that make bisect even cooler are available on the git-bisect docs page.
pull + rebase: 2 for 1
You can pull. You can rebase. Or you can do both in a single line
git config: Type less with aliases
Maybe you don't like reaching up for the hyphen key
-
and want a simple command to pull and rebase at the same time.git config
lets you set any of the config options. There are a lot, but for now we'll just show off an alias.From this point forward, you can type
git pr
to pull & rebase. Your hyphen key will thank you!git-reflog: Undo nearly anything
git reset
(with the hard flag) is probably one of the scariest operations you can do. Just like that, it could appear your entire repository was catapulted back in time. This can be pretty great… as long as you didn't need any of those changes. It only took me accidentally blowing away my changes once before I didgit reset
in new branches only. This was before I learned about the "reflog".The
git reflog
command spits out a sequence of operations.It turns out that it's damn-near impossible to lose something in git. As you commit, rewind, replay, and revert, git keeps a record of every single thing you've done. Now, let's reset to some arbitrary distance ago, destroying all my hard work.
Sure, I'm back in "pygment happy land", but I just lost 7 commits worth of work.
git reflog
knows you better than you think; it has your history of changes, including your foolish reset:A quick reset back to
1907832
, and everything is back as if nothing was ever destroyed. There's a line added to your reflog for the reset you made too! This makesgit-reflog
incredibly powerful. It's also comforting to sometimes just look at the reflog and know git's got your back.Most of these tips were pulled from notes I took while learning git. I then learned there is an entire website dedicated to improving your git-fu. http://gitready.com/ has examples, tricks, tips, and even more amazing things that go beyond this.
Beta Was this translation helpful? Give feedback.
All reactions