in git coding tips ~ read.

Git commands everyone should know

Git is a phenomenal source control system. Having used some of its predecessors, SVN, CVS and SourceSafe, I'm always impressed at how much better things have gotten. If you have not seen Linus Torvalds talk about why he created git you should take an hour and watch it.

I do think too many developers just gloss over Git. They learn the basic commands they need day to day but miss out on some of the robust features Git has.

XKCD GIT

So here is my list of my most used and helpful Git commands.

Most used

I checked my bash history and the list is exactly what you'd expect. These are the commands everyone knows and uses day to day:

git status  #show the status of your working copy

git checkout <branch_name> #switch to another branch

git checkout -b <new_branch_name> #create a new branch

git add . #add everything from your working copy to be committed

git commit -m "<commit message>" #commit changes 

git pull #push your copy to the remote repository 

git push #pull any changes from the remote repository  

Most useful

Here is my list of commands I use much less often but are very useful in a pinch.

Merging branches

git merge --squash <branch_name>  

You should be using a branching model during development. This how to merge one branch into another. --squash is optional. Using it will squash all the commits into a single commit on the target branch.

Merging remote branch

git merge origin/<branch_name>  

Not need to switch to and update a branch before merging it. Just git pull and merge the remote URL.

Squashing Commits

git rebase -i HEAD~<number_of_commits>  

Forgot a file in the last commit? Did you do a lot of commits to track your work but want to roll them into just one commit? Use rebase to squash them together!

Cherry picking a commit

git cherry-pick <commit_hash>  

Want to bring over one commit from another branch into yours? Well, that's what cherry pick is for! It'll just bring over the selected commit to your branch.

Resetting your local copy

git reset --(hard/soft) (<commit_hash>/<branch_name>)  

We've all completely fucked up our local copy at least once. It's always fixable with git. Resetting hard will drop your changes, while soft will keep them as staged files. You can also use remote URL's here to reset to the server's working copy.

Create a patch file

git diff > a_file.patch  
git diff --cached > a_file.patch # if staged  

Working remotely and want to have someone look at some code for you? Send them a patch file.

Apply a patch file

git apply --stat a_file.patch  

Apply a patch to your local copy.

Stashing

This is my most used Git feature. I'd always surprised when I see people not using stashes. Stashes gives you a way to save local changes then restore them later.

This makes a few git tasks much easier. Need to switch tasks to work on something else? Stash your changes! Need to merge a branch into your local copy but you have active changes? Stash your changes! Tried an idea that didn't work out but what to revisit the code later? Stash your changes!

Saving a stash

git stash save "Optional Description"  

Listing stashes

git stash list  

Apply a stash

git stash apply <stash_index>  

There you have it, a few commands to add to your git toolkit.