The Git Cheat Sheet
Friday, 6th September 2013, 11:30
Believe it or not, Git is actually quite simple 99% of the time. You just need to do certain things properly and avoid certain other things, mostly though it won't let you do anything damaging beyond making a mess of your local repository.
Whilst there are UIs out there which help, you can't beat the command-line, not because it's more geeky and cool, though perhaps some might argue it is. No, it's best for two simple reasons, control and information. It gives you complete control without hiding away features, and it gives you all the information you can stomach without hiding any issues.
But it isn't always easy to find a good guide to all the git commands you'll need 99% of the time, so here is a quick one, based on the simple fact I have a terrible memory so I end up making a list for when I forget.
I'm not here to explain Git, but just to make sure we are on the same page here are a few terms used in this article:
Repository - a git repository, that exists either locally on your own PC, or on a remote server somewhere
Local - stuff on your own computer
Remote - stuff on a remote server somewhere
Branch - either the main set of code, or an off-shoot based on the main set of code
Master - the main branch
Commit - saving your latest changes to a branch, also the act of doing a commit
Head - the most up to date committed code of a branch, aka the tip
Downloading a Repository
Real simple one this, you'll use it when you start work on someone elses repository, want to just get a copy of a remote repository into a subfolder of the current directory:
git clone URL
git clone https://github.com/RobeeeJay/Fastworks.js.git
git clone email@example.com:RobeeeJay/Fastworks.js.git
Creating a New Repository
If you are inside the root directory that you want to make a repository of, then you can create a new repository ready to add files into it with this:
That creates the .git subdirectory. If you want to upload this to a new remote repo, you need to add that destination with a command like this:
git remote add origin URL
git remote add origin https://github.com/RobeeeJay/Fastworks.js.git
git remote add origin firstname.lastname@example.org:RobeeeJay/Fastworks.js.git
Any Command From Now On
Just something worth noting, all the commands from now on can be done anywhere inside any directory or subdirectory of a repo. It doesn't really matter! So don't feel you always have to visit the root directory to do use them. Hang around wherever you like, just as long as it's inside the repo somewhere.
Changing Branches Within a Repository
You'll be doing this a lot, it changes your local repository between all the branches it is currently aware of.
git checkout BRANCHNAME
git checkout master
git checkout 0.8.3
git checkout 798ed8a00350a209a77407a53bce935133e06d5c
As the cryptic hash on that last example shows, you can also use it to checkout individual commits in case you need to work out how far back you need to go for a major bug to dissappear. You can also use it to check out any tagged commits, tags being useful ways to mark a state-in-time of a codebase so you don't have to use hashes like the above.
Updating Branches and Changes From the Remote
Git isn't automatic, it doesn't know about any new branches or changes on the server. To get those you have to tell it to fetch them:
This will not change any local code, it just makes the local repo aware of any changes to the remote.
Creating a New Branch
Only a lone gunman works on master, and even then it's handy to try out new things without polluting your release code with untested bugs. Doing this involves creating branches, and every branch is based off another, it's like a tree with the master branch being the main trunk.
git checkout -b NEWBRANCHNAME PARENTBRANCH
git checkout -b newfeature master
git checkout -b 0.8.4 master
git checkout -b experiments 0.8.4
Deleting a Local Branch
Maybe you made one by mistake, maybe it's so old and you're done with it and don't need it anymore. Any branch other than master can be nuked.
git branch -d BRANCHNAME
git branch -d newfeature
git branch -d 0.8.4
Deleting a Remote Branch
The above only deletes them locally, any you or someone else pushed to the remote repo will remain there until deleted. You can do this like so:
git push origin --delete BRANCHNAME
git push origin --delete newfeature
git push origin --delete 0.8.4
Deleting Local Branches No Longer on Remote
So someone tidied up the remote and you want all branches on your local repo that are no longer on remote to go too. BE CAREFUL here, you may not want a local branch deleted on the remote to be killed! To be safe, do a dry run first!
git remote prune -n origin
Remove the -n to do it for real.
View a List of Branches
Want to see a list of all branches in your local repo? Do this:
Want to see a list of all branches in your local repo and the remote repo? Do this:
git branch -a
Update Your Local Branch With Remote Changes
It's always a good idea to keep your local branch up to date, and whenever any changes happen to the remote branch, or any branches yours is based on, then you should pull and merge any remote changes. In fact if you are not the only one working on a project, you should always pull before you push your changes.
git pull origin REMOTEBRANCH
git pull origin newfeature
git pull origin 0.8.4
See Local Changes Not Yet Committed
Added files, made changes, want to see what branch you are on as you've forgotten? Check the status of your current branch with:
This will show anything modified, and also any files that have been deleted and added, plus those which you haven't yet added to the repo.
Compare Your Branch to Any Other Branch
Sometimes you just want to make sure nothing odd has changed between a branch you are on and another. You can do this by showing a diff, and you can do it against local and remote branches. Just make sure you recently fetched!
git diff BRANCH
git diff master
git diff origin master
git diff origin newfeature
Add Files to a Branch
Git only tracks files you add to it. You can use wildcards, add whole directories, and so on. But it won't track anything unless you add them.
git add FILENAME
git add *
git add static/stylesheets/basic.css
git add ../file.js
Remove Files From a Branch
Sometimes you need git to not track a file, because perhaps you added it before including that file in .gitignore. Easily done!
git rm FILENAME
git rm *
git rm static/stylesheets/basic.css
git rm ../file.js
Commit Your Changes
Made your changes and ready to committ them to your branch? The easiest way is to commit all current changes on tracked files.
git commit -a
Whatever your default editor is will be used (EDITOR env variable usually) for your commit message, any line commented with a # is stripped hence why you don't need to delete the list of files committed.
Push Your Changes to Remote
Don't wait, do this often. I tend to do it mostly after I've finished with a branch for the day, but if you work with an active team do it more.
git push origin BRANCHNAME
git push origin master
git push origin newfeature
Undo Changes Put in the Wrong Branch or Stashing Changes
You've started making changes, then you realise you are on the wrong branch and meant to do it in another or a new one. What do you do? Stash is your friend! It saves all current changes since the last commit so you can retrieve them later.
Think of it as like reverting to the last commit but with your changes safe. You can then switch to the right branch, and do:
git stash pop
There are other uses for this, but really if you are using them for anything more than trying one small change then another, you are doing something wrong. But say you wanted to try a small code change and maybe set a few undos along the way, you could stash a few. Then you can use this:
git stash list
Which shows you all the stashes and then you can pop any of them off (a one time only deal remember!) with:
git stash pop X
Where X is the number on the list.
Undo the Last Commit
Accidentally committed? Need to undo it? Don't panic, as long as you haven't committed to a remote repo you can do this:
git reset --soft HEAD^
Now you can edit your code and re-commit.
Undo All Changes Since Last Commit
This is not the same as undoing a commit, this will delete ALL of your changes since the last commit. They'll be totally gone and no way to get them back! That is ALL your work since you last did a "git commit" deleted. Still want the command? Okay...
git reset --hard HEAD
Undo All Local Changes so Local Matches Remote
Made a total mess? Need to revert your entire local branch so it is the same as the remote? You must be really in trouble to need this, even more than the last one. But here we go:
git reset --hard origin/BRANCHNAME
git reset --hard origin/master
git reset --hard origin/newfeature
Remember Kids... Pull Regularly, Push Often
Merges fail and require human intervention the less frequently you do them, so pull often. The more often you push, the easier it is for others to pull your updates regularly. And most of all, the less a disaster your HDD dying is.
This is by no means a fully comprehensive guide to using git. It is however pretty much every command I've used regularly and needed occasionally this last year. If you think I've missed something major, let me know in the comments and I'll consider adding it.