DEV Community

Cover image for Useful Git Commands For Beginners
Juan Sebastian
Juan Sebastian

Posted on • Edited on

Useful Git Commands For Beginners

Alt Text

I’m going to share with you a few commands I’ve found extremely useful on my way to become a good software engineer.

git log

The git log command is going to show you the history of your repo.

Alt Text

The goods are that you can improve this command as much as you want.
git log —-oneline will show you the same but in one line.
Alt Text

if you have a big project that you implemented branches, you can run the git log command like this.
git log —-all —-graph —-decorate —-oneline
This is going to show you all, in oneline, decorate and in a graph (like a tree).

git checkout

git checkout could be very helpful in some situations.
1) When creating a branch and jump into it.
Usage: git checkout -b new_branch

Alt Text

2) Jumping between branches
Usage: git checkout branch_tojump

Alt Text

3) With committed files but not pushed yet, is very useful to turn back into a stable version of your project.
assume your code is broken, and you can’t find what’s going on.
With git log, you can identify the SHA of your commit and make a: git checkout , this will bring you back to that specific commit.
Usage: git checkout

Alt Text

We have made a commit that was a huge mistake and now our code is broken, we want to turn back to the commit when our code was stable, this case is the SHA of the commit called ‘Another change ‘ = ab12b0b.

Alt Text

Now HEAD is in the ab12b0b commit. and your program is working again

git revert

What if your changes are in your remote repo, I mean are already pushed.
In that case, you can use git revert, works similar to git checkout, it will turn you back to a stable version of your project, and then you can rebuild your project history again.
Usage: git revert

amend

amend is useful to append changes to a commit you haven't push yet.
Assume you make some changes and you committed them, but you are missing something, will not look professional to push the commit, and then make another one to the changes you miss.
In that case, amend could be your friend.
Usage: git commit --amend

git diff

It will show the differences you have made in a file. with the + sign is telling you what lines are being added to the file.
Usage: git diff
Alt Text

git shortlog

git shortlog is going to show you a log of your repo. but you can improve it to see much more.

git shortlog -sn
Will show the number of commits by person.
Alt Text

git shortlog -sn — all — no-merges
Will show the commits, the person who made it, but will not include the merges.

There’s a lot more of useful command but to not make this blog longer I’ll not include them, I hope you like and find them as useful as me.
To be a better software engineer and speed up your abilities with git, I’m going to recommend this book. 📚

https://www.amazon.com/-/es/Pro-Git-Scott-Chacon-ebook/dp/B01ISNIKES/ref%3Dsr_1_5?__mk_es_US=ÅMÅŽÕÑ&dchild=1&keywords=pro+git&qid=1599060334&sr=8-5

Thanks you for reading ✌🏻.

Top comments (0)