August 28th, 2019 · 4 min read
There’s no question that Git is tricky at first and takes time to get used to, all developers struggle with it initially.
Disclaimer! I’m not a Git expert and have only been actively using Git daily for the past 6 months since starting my first Software Developer role. These are some useful commands picked up from senior developers on my team which I thought I’d share to help other new developers. It’s not a comprehensive or a complete beginner guide — if you’re completely new to Git I recommend checking out these references:
In this list, I’m referring to using Git in the command line. Although I used some GUI tools at the very beginning in learning Git, I much prefer the command line as it’s possible to do all kinds of things that you can’t do when using Github desktop.
Made some changes but forgot to git pull (fetch and download content from the remote repository) before starting? Or started to work on the wrong branch? Git stash and Git stash pop are what you need:
Git stash to move the changes away from your staging area then:
git stash pop
Git stash pop will bring them back after you git pull / switch to the right branch.
What changes are currently in your staging area?
Git status will show you the file/s that have been changed. For a more accurate breakdown of those changes I’d recommend:
This will clearly show you each change within a file down to the line of code. (‘q’ to exit).
Committing only some changes from a file
In Git ‘-p’ is short for ‘patch’ and this can be used with many other Git commands to break down the staged changes into pieces or according to Git ‘hunks’. (I prefer to think of ‘chunks’ which makes a lot more sense and is less associated with cheese!) This is a lesser-known Git command but makes life so much easier when you’ve made a bunch of changes but don’t want to commit all of them.
git commit -p
Git commit -p allows you to select which changes or hunks to go ahead and commit. The most common selection is ‘y’ or ’n’ however there’s also:
By selecting ’n’ or ‘q’ the changes will still be there in your staging area, but will not added to the current commit.
Unsure of what was committed recently? Check the previous commit history:
and to view the commit history in one line:
Git log --oneline
Save all changes to an external file if you’re in doubt about undoing changes.
Git can be unstable and it is possible to lose your staged changes unless they are committed, for example, by undoing changes in your staging area. If you’re unsure about your git commands and potentially losing all your hard work you can save the change detail to a file called changes.txt outside the repository. So, no matter what happens the changes are stored somewhere. (This is really a hack and not recommended as usual practice). You can read about how to undo things here
git diff >~/changes.txt
When you’ve made changes on master and want to move them on to a new branch.
Git checkout -b <new branch name>
If you’re creating a new branch locally, don’t forget to push to the branch to the remote repository:
git push -u origin <branch name>
I hope this was helpful and you can try out some new Git commands. I’ll be adding to this list as time goes on. If you can share any other useful commands, please add those to the comments.
If you enjoyed this post and want to get more like it in the future, subscribe below. By joining the ZTM community of over 100,000 developers you’ll receive Web Developer Monthly (the fastest growing monthly newsletter for developers) and other exclusive ZTM posts, opportunities and offers.
By the way, you can also check out all the ZTM courses available here.