DEV Community

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

Posted on • Updated on

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

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 control system that allows you to track revisions, identify file versions and recover older versions if necessary.

Users with some programming experience can get started with Git fairly easily, but it’s not easy to pick up all the advanced features. In this article, I’ll show you some of the most useful commands that will make you a Git pro.

1. git config

git config is one of the basic Git commands that you must know. The command helps in setting the configuration values for email, username, file formats, preferred file algorithm, and many other attributes. The example of the command is as follows:

# configure the user which will be used by Git
# this should be not an acronym but your full name
$ git config --global user.name "Firstname Lastname"
# configure the email address
$ git config --global user.email "your.email@example.org"
Enter fullscreen mode Exit fullscreen mode

2. git init

git init is one of the top Git commands and is ideal for initializing a Git repository. The command helps in the creation of the initial .git directory in an existing or new project. The .git folder remains hidden, and you have to disable the feature in the case of Windows to see it. In the case of Linux, you can use the ‘ls –a’ command for viewing the .git directory. It is recommended that no one should tamper the contents of the .git folder.

$ git init <the name of your repository>
Enter fullscreen mode Exit fullscreen mode

3. git clone

This command is used to obtain a repository from an existing URL

$ git clone <the url of the repository>
Enter fullscreen mode Exit fullscreen mode

4. git add

The ‘git add’ command helps in adding file modifications, presently in the working directory to the user’s index. The command helps in adding untracked files that are ready for committing to the remote repository. The example of using the ‘git add’ command is as follows.

$ git add myfile
Enter fullscreen mode Exit fullscreen mode

This command would add myfile to the staging area.

5. git branch

The ‘git branch’ is a notable mention among Git commands for beginners. The “branch” command helps you create, delete, and list branches.

This command has some important options:

. -v -a
Provides more information about all your branches. Listing your branches, by default, will only show your local branches’ names.

  • Adding the “-a” flag will make sure remote branches are also included in the list.
    Adding the “-v” flag will make the command more “verbose” and

  • include SHA-1 hashes as well as commit subjects of the latest commits on your branches.

— no-merged
Returns all branches that have not been merged into your current HEAD branch.

-d
Deletes a specified branch.

Usage

#list all branches
$ git branch -a -v 
#Return all branches that has not merged
$ git branch --no-merged
#Return all branches thaat has merged
$ git branch --merged
Enter fullscreen mode Exit fullscreen mode

6. git commit

The git commit command captures a snapshot of the project's currently staged changes.

$ git commit -m “first commit”
Enter fullscreen mode Exit fullscreen mode

7. git push

The ‘git push’ command can help in pushing all modified local objects to the remote repository and then growing its branches. An example of using this command is as follows

$ git push origin master
Enter fullscreen mode Exit fullscreen mode

8. git diff

The ‘git diff’ command is useful for creating patch files or the statistics of differences between paths or files in your index, working directory, or git repository. An example of using this command is as follows

$ git diff
Enter fullscreen mode Exit fullscreen mode

9. git status

The ‘git status’ command can help in displaying the status of files in the index and the ones in the working directory. The command would list out untracked, modified, and staged files easily. An example of using the ‘git status’ command is as follows

$ git status
Enter fullscreen mode Exit fullscreen mode

10. git show

This command shows the metadata and content changes of the specified commit.

$ git show
Enter fullscreen mode Exit fullscreen mode

11. git tag

This command would help in tagging a particular commit with a simple, durable, and human-readable handle. The example of this command is as follows

git tag –a v2.0 –m ‘this is version 2.0 tag’
Enter fullscreen mode Exit fullscreen mode

12. git merge

git merge” is a robust feature that allows you to combine work from two branches into one. This is useful when developers work on the same code and want to integrate their changes before pushing them up in a branch.

$ git merge branch_name
Enter fullscreen mode Exit fullscreen mode

13. git log

The “git log” command lists every commit that has ever happened in your project to see what has changed over time, along with some other information about how the commit was done.

$ git log
Enter fullscreen mode Exit fullscreen mode

14. git reset

Use git reset to “un-track” a file to no longer have any links to the Git repository.

$ git reset [commit id]
Enter fullscreen mode Exit fullscreen mode

15. git rm

This command is used to delete a specific file from the current working directory and stages the deletion. For deleting a specific file from the current working directory and stages the deletion, use the following command:

$ git rm <filename>
Enter fullscreen mode Exit fullscreen mode

16. git remote

This command is used to connect the local git repository to the remote server.

$ git remote add [variable name] [Remote Server Link]
Enter fullscreen mode Exit fullscreen mode

17. git fsck

This command is used to check the integrity of the Git file system and it also helps in identifying corrupted objects.

$ git fsck
Enter fullscreen mode Exit fullscreen mode

18. git pull

This command fetches and merges changes on the remote server to your working directory.

$ git pull repository_link
Enter fullscreen mode Exit fullscreen mode

19. git checkout

The “git checkout” command allows us to switch to an existing branch or create and switch to a new branch. To achieve this, the branch you want to switch to should be present in your local system and the changes in your current branch should be committed or stashed before you make the switch. You can also use this command for checking out the files.

# Switch to an existing branch:
$ git checkout <branch-name>
#Create and switch to a new branch
$ git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

20. git stash

This command is used to temporarily store all the changed files in the working directory.

Usage: Save all the modified tracked files temporarily:

$ git stash
Enter fullscreen mode Exit fullscreen mode

Usage: List all the stashes:

$ git stash list
Enter fullscreen mode Exit fullscreen mode

Usage: Delete the latest stash:

$ git stash drop
Enter fullscreen mode Exit fullscreen mode

Summary

We have reached the end of this post. You can now claim to be a version control Pro. But remember, there are other useful git commands and Git is not the only version control tool.

Thanks for reading! If you have any questions or feedback, please leave a comment below.

THANK YOU FOR READING
I hope you found this little article helpful. Please share it with your friends and colleagues. Sharing is caring.

Connect with me on various platforms

Top comments (58)

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.