Quick Reference: Git Commands

I find myself looking for or double checking certain git commands at varying frequencies. I made this list to help myself, it’s a bonus if it helps you too!

None of these recipes were invented by me. I’ll try to include links if I have saved them in my notes, if not, sorry but thank you internet stranger for making life a little bit better.

Cleanup commits in a feature branch

I have lost code that I have written and saved but not committed, because the development server I used crashed. So, I’ve learned to commit often. But sometimes (read, most times) I want to save work that is not ready for any human to ever see. I don’t want all these intermediate commits to show up in a pull request.

To make it concrete: I have a branch dirty that branched-off from master, and I have possibly merged master into dirty several times, and made several commits to dirty. I want to now create a “clean” pull request by creating a branch clean that has all the commits of dirty squashed into one. Below is a pretty safe git recipe for that (there are several ways to do it, but below is the one I prefer).

Let’s first make sure we have the latest master.

git checkout master

git pull

Let’s branch off the latest master into clean.

git checkout -b clean

Let’s bring changes in dirty into clean, but squash them.

git merge --squash dirty

You can now commit changes in clean, with a unified and meaningful commit message, by deleting all the intermediate commit and merge messages.

git commit

Now, you can create a pull request from the clean branch that hides all your commit transgressions in dirty.

Avatar
Vineeth Kashyap
Software Engineer, Programming Languages