Useful Git commands for new developers

Caroline Chan

There’s no question that Git is tricky at first and takes time to get used to, all developers struggle with it initially.

Image: https://xkcd.com/1597/

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 

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

Git status will show you the file/s that have been changed. For a more accurate breakdown of those changes I’d recommend:

git diff 

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.

For example:

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:

Git log

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.

You can read more about my journey to Software Development here and also follow me on Twitter for dev related tweets!


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.

More from Zero To Mastery

Web Developer Monthly September 20...

27th issue of Web Developer Monthly! Read by 50,000+ developers every month. Kee...

Learn to code in 2020, get hired, a...

I've taught thousands of students how to code over the past 2 years. Many of the...

Tech Trends Showdown🏆: React vs An...

Wait, didn’t I already write about this? Well…yes, but too many of you complaine...