DEV Community

Cover image for 20 Git Commands That Will Make You a Version Control Pro.

20 Git Commands That Will Make You a Version Control Pro.

Thomas Sentre on February 03, 2023

Version control is essential for programmers who want to collaborate effectively and track changes when working on code in a team. Git is a version...
Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Mastering these basic commands is a great start for anyone looking to get started with version control using Git. Thank you so much Thomas for collecting all these commands in one post!

Collapse
 
devland profile image
Thomas Sentre

You're welcome! I'm glad that the information was helpful.

Collapse
 
macanudo527 profile image
Neal Chambers

One thing that might come in handy with git checkout is the ability to checkout or restore an individual file from another branch (usually main, but could be something else). For instance, if you make changes to a file that break the code and can't see an easy way to revise it, you can simply checkout the file from main and restore it to the original:

git checkout main file_with_too_many_mistakes.py
Enter fullscreen mode Exit fullscreen mode

This will overwrite the current branch's file with the, hopefully working, file from the main branch.

Collapse
 
devland profile image
Thomas Sentre

Thanks for sharing.

Collapse
 
mickmelon profile image
MickMelon

git rebase is another useful one to know for rewriting history.

Collapse
 
ant_f_dev profile image
Anthony Fung

Definitely - interactive rebases (-i) are also great for squashing multiple commits and/or changing the commit message

Collapse
 
devland profile image
Thomas Sentre

It's helpful. Thanks for sharing.

Collapse
 
kkm000 profile image
Cy "kkm" K'Nelson

Goes along especially well with git commit --fixup=<SHA>, when you work on multiple changes at once but want to keep each commit small, representing a single consistent logical change. Keep in mind a risk of introducing a dependency on a future change, tho.

Collapse
 
guzcode profile image
Juan Guzman • Edited

git stash pop Is a must, it take back the stashes changes, thank you good post

Collapse
 
devland profile image
Thomas Sentre

Thanks for sharing.

Collapse
 
aizensousuke profile image
AizenSousuke

i use git stash apply instead because i dont want to lose the stash

Collapse
 
guzcode profile image
Juan Guzman

Humnn, good approach

Collapse
 
parthprajapati profile image
Parth

Thank you for sharing this valuable article. Version control is an essential tool for software development and working with source code, and understanding how to use the various Git commands is a great way to become a proficient version control user. We hope you found the article useful in your journey towards becoming an expert in version control!

Collapse
 
devland profile image
Thomas Sentre

Glad you liked it.

Collapse
 
codeofrelevancy profile image
Code of Relevancy

There are some hidden risks with 1. git config

I have made detailed article on it:
dev.to/codeofrelevancy/the-hidden-...

BTW, thanks for sharing the article on git commands in details.

Collapse
 
devland profile image
Thomas Sentre

Glad you liked it.

Collapse
 
milan_vala profile image
milan vala

git stash save "message" and git stash apply stash{id} are some good commands.

Collapse
 
devland profile image
Thomas Sentre

Thank you Milan, for sharing these useful commands.

Collapse
 
ppaul profile image
Paul

this is useful for all beginners or advanced users, however the title is not precise. It will not make you a 'PRO', but capable. I think the 'PRO' features are a lot more advanced and complex and they are not mentioned here (eg. atlassian.com/git/tutorials/advanc... )

Collapse
 
devland profile image
Thomas Sentre

I'm glad the information was beneficial to you. To be honest, the title was a bit misleading, my aim was to attract Git enthusiasts to read the article and either learn a new command or share their own knowledge with others.

Collapse
 
hadeelsharaf profile image
Hadeel Sharaf

Good collection for beginners thanks. I would add fetch and switch as recommendations for git beginners

Collapse
 
devland profile image
Thomas Sentre

You're welcome 😊.

Collapse
 
nicolamassarenti profile image
Nicola Massarenti

To check the log of the commits, an other useful command is git log --graph - it will display the graph view of the branches and the merges.

Collapse
 
kkm000 profile image
Cy "kkm" K'Nelson

Try this git lg alias in the [alias] section of ~/.gitconfig for a narrower, more informative graph (tweak colors to your liking). git la is a variant that also adds initial 8 characters commit author's name.

[alias]
lg = log --graph --date=format:%y%m%d --format='%C(142)%h %C(36)%ad%C(auto)%d %s'
la = log --graph --date=format:%y%m%d --format='%C(142)%h %C(36)%ad %C(71)%<(8,trunc)%al %C(auto)%d %s'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
emcek profile image
Michal Plichta

Good guide, all basic commands with simplest parameters, but imho this make developer beginner of git, not professional. So, subject is misleading.

Collapse
 
mamutalib profile image
Md. Abdul Mutalib

Great article for absolute beginner as well for others.

Collapse
 
devland profile image
Thomas Sentre

Thanks.

Collapse
 
mdrijwan profile image
Md Rijwan Razzaq Matin

The only new command I see here is fsck. Will check it out. Other than that, I think these are commands a regular dev uses on a daily basis. Thanks for putting them together.

Collapse
 
candlepeter profile image
Isienyi Paschal Ejike

Thank you for sharing such useful information...

Collapse
 
bakardev profile image
Muhib ur Rahman Bakar

Thanks for sharing, rebase is also a very helpful command.

Collapse
 
n1ckdm profile image
Nick

Just use lazygit

Collapse
 
florentbo profile image
Florent Bonamis

Thank you for the article. Is it not recommended to use git switch instead of git checkout?

Collapse
 
devland profile image
Thomas Sentre

The advantage of using git switch over git checkout when switching branches is that it provides more safety checks and clearer error messages, making it easier to use for some users. However, if you're already familiar with git checkout and feel comfortable using it, there's no compelling reason to switch to using git switch.

Collapse
 
fcojperez profile image
Francisco Perez

Pretty cool, thanks for sharing this cheatsheet 👍

Collapse
 
devland profile image
Thomas Sentre

Glad you liked it.

Collapse
 
clay profile image
Clay Ferguson

Great summary of the most important commands. Thanks!

Collapse
 
devland profile image
Thomas Sentre

You're welcome! I'm glad I could help.

Collapse
 
el__hussain profile image
ᴇʟ-ʜᴜssᴀɪɴ🌐🇳🇬

Helpful

Collapse
 
devland profile image
Thomas Sentre

Thanks, El-Hussain

Collapse
 
alexiscesar profile image
Alexis Cesar

Didn't know the git fsck command, cool!!

Collapse
 
devland profile image
Thomas Sentre

Thanks, Alexis.

Collapse
 
lotfijb profile image
Lotfi Jebali

Helpful

Collapse
 
devland profile image
Thomas Sentre

Thank you, Lotfi.

Collapse
 
rameshkmunjal profile image
RAMESH KUMAR MUNJAL

Thanks for the post .it Helped me to understand some git commands .

Collapse
 
devland profile image
Thomas Sentre

Glad you liked it!

Collapse
 
aiminglinux profile image
aiminglinux • Edited

I am new with this, this post help me alot. thx!

Collapse
 
devland profile image
Thomas Sentre

I'm glad to hear that!

Collapse
 
sukeshkumar1096 profile image
sukeshkumar1096

Thanks for making this effort for the community. I'll take note of it and use it right away. Keep up the good work.

Collapse
 
devland profile image
Thomas Sentre

Glad you liked it.

Collapse
 
thi3rry profile image
Thierry Poinot • Edited

Great post !
I will add

git add -p

With that one you can check all your modifications one by one and review all your working directory modifications before adding them to the staging area of your futur commit

Collapse
 
devland profile image
Thomas Sentre

Thanks for sharing.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
devland profile image
Thomas Sentre

Thank you for the positive feedback! I'm glad to hear that the post was helpful in your learning journey with Git. If you have any further questions or need clarification on anything, feel free to ask!

Collapse
 
udanielnogueira profile image
Daniel Nogueira

Very useful

Collapse
 
devland profile image
Thomas Sentre

Thanks.

Collapse
 
corentinbettiol profile image
Corentin Bettiol

(please source your content)

Collapse
 
kacperperschke profile image
KAcper Perschke

If google hadn't suggested such a "valuable" article to me,
I wouldn't have known that I've been a pro since 2016.

Collapse
 
leandro_nnz profile image
Leandro Nuñez

I haven’t had the time (or at least I haven’t made any) to fully understand git. This is extremely helpful. Thanks!