Git commands you should know

Git is one of the most powerful VCS’s on the market. Yes, there are other great VCS (as Mercurial and Bazaar), and it has its problems (the cryptic documentation, the transcendental meaning of git-reset, etc.). But the collection of tools it provides to you is capable of diminish any difficulty.

There are some tools that don’t seem useful at first, but will come incredibly handy sometimes. For instance,

#1 – cherry-pick

Picture this: you’re working on a feature branch. You see some minor bug from your main development branch while you’re working on it, so you commit a correction for it. The feature doesn’t come out as expected, so you delete the branch and remake the change on the main development branch. Right?

Well, you could do that. But you could also use the git cherry-pick.

This command takes a commit identifier as a parameter, and applies the commit to your current branch. Simple and clean.

After you applied the commit, you can fearlessly delete your failed feature branch.

#2 – bisect

Bisect is an awesome tool, and is implemented across various DVCS’s. The concept is really simple: at some point, someone made a bad commit that inserted a bug in your code. You don’t know where the bug is, or how it began.

Now, you may debug your whole software until you find the line that inserts the bug. But this is laborious, and doesn’t guarantee you have a solution (for example, the bug is inside a CSS. How to debug it?)

So you use bisect. You know that the current branch (let’s say, feature-x) is broken, but the tag v1.0-12 didn’t have the problem yet.

So, you do this:

git bisect start feature-x v1.0-12

And test your software. This will select the middle commit between your feature-x branch and your v1.0-12 tag. If the bug disappeared, you mark the current commit as “good”. If it’s there, you mark it as “bad”. If you can’t test the current commit for some reason, you skip it. If you want to see a log with the commits that git is suspecting to have the bug, it’s also possible.

git bisect good
git bisect bad
git bisect skip
git bisect view

When it finishes, it will tell you in what commit the bug was inserted. So you reset the repository to your current HEAD. Always remember to do that.

git bisect reset

Then, you can edit your files accordingly.

#3 – gitk –all $(git reflog –pretty=format:%h)

Pretty monster, uh? Test it now. It’ll show every commit you made, even those that are not reachable anymore. Sometimes it may be useful. In pragmatic terms, it’s a reflog inside of gitk.

#4 – rebase –interactive

It opens your text editor with a script that allows you to squash, edit, reorder or delete commits from your current branch, based on the parameter you gave. For example, if you are on master and insert a

git rebase -i origin/master

You’ll edit the commits that are not present yet on origin/master.

NEVER use this command with commits that already public, otherwise you’ll make your friends really upset. 😦

#5 – add -p

This command allows you to interactively select which parts of the file you want to stage, allowing you to commit only a specific part of the file. Use “?” for help. It’s pretty intuitive.

If you know more useful commands, let me know on the comments.