Basic Git commands : A complete cheat sheet for beginners

Rahul Mohata
<Coder/Decoder>
Published in
7 min readMay 30, 2020

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Git was initially designed and developed by Linus Torvalds for Linux kernel development. Git is a free software distributed under the terms of the GNU General Public License version 2.

Git is a speedy and efficient distributed VCS tool that can handle projects of any size, from small to very large ones. It is free, open-source software that lowers the cost because developers can use Git without paying money. It enables multiple developers or teams to work separately without having an impact on the work of others.

Git has steadily risen from being just a preferred skill to a must-have skill for multiple job roles today.

To download git on your system, you must visit https://git-scm.com/

This article covers the Git commands every beginner must know. I have handpicked top Git commands from a very practical point of view which are very much essential for any beginner to start with.

1. git config

git config –global user.name “[name]”

git config –global user.email “[email address]”

This command sets the author name and email address respectively to be used with your commits.

2. git init

git init [repository name]

This command is used to start a new repository.

3. git clone

git clone [url]

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

4. git add

git add [file]

or

git add -A

The first one adds a single file to the staging area and the second one adds all the modified files to the staging area .

5. git commit

git commit -m “[ Type in the commit message]”

This command records or snapshots the file permanently in the version history.

git commit -a

This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

6. git diff

git diff

This command shows the file differences which are not yet staged.

git diff –staged

This command shows the differences between the files in the staging area and the latest version present.

git diff [first branch] [second branch]

This command shows the differences between the two branches mentioned.

7. git reset

git reset [file]

This command unstages the file, but it preserves the file contents.

git reset [commit]

This command undoes all the commits after the specified commit and preserves the changes locally.

git reset –hard [commit]

This command discards all history and goes back to the specified commit.

8. git status

git status

This command lists all the files that have to be committed.

9. git rm

git rm [file]

This command deletes the file from your working directory and stages the deletion.

10. git log

git log

This command is used to list the version history for the current branch.

git log –follow[file]

This command lists version history for a file, including the renaming of files also.

11. git reflog

git reflog

This is essentially a shortcut equivalent to

git reflog show HEAD

This will output the reflog of the HEAD ref.

git reflog show [branch name]

This will output the reflog for a specific branch.

12. git show

git show [commit]

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

13. git tag

git tag [commitID]

This command is used to give tags to the specified commit.

14. git branch

git branch

This command lists all the local branches in the current repository.

git branch [branch name]

This command creates a new branch.

git branch -d [branch name]

This command deletes the feature branch.

15. git checkout

git checkout [branch name]

This command is used to switch from one branch to another.

git checkout -b [branch name]

This command creates a new branch and also switches to it.

git checkout -b [new branch name] [existing branch name]

This command creates a new branch from the existing branch and also switches to it.

16. git merge

git merge [branch name]

This command merges the specified branch’s history into the current branch.

17. git remote

git remote add [variable name] [Remote Server Link]

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

18. git push

git push [variable name] master

This command sends the committed changes of master branch to your remote repository.

git push [variable name] [branch]

This command sends the branch commits to your remote repository.

git push –all [variable name]

This command pushes all branches to your remote repository.

git push [variable name] :[branch name]

This command deletes a branch on your remote repository.

19. git pull

git pull [Repository Link]

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

20. git stash

git stash save

This command temporarily stores all the modified tracked files.

git stash pop

This command restores the most recently stashed files.

git stash list

This command lists all stashed changesets.

git stash drop

This command discards the most recently stashed changeset.

Popular Git solutions

Following are the most popular git solutions currently in the community

1. GitHub

GitHub, Inc. is a United States-based global company that provides hosting for software development version control using Git. It offers the distributed version control and source code management functionality of Git, plus its own features. It provides access control and several collaboration features such as bug tracking, feature requests, task management, and wikis for every project. It is the largest repository hosting platform with more than 38 million projects.

PS: I actually use Github and have no plans to change to anything else.

2. Bitbucket

Bitbucket is a web-based version control repository hosting service owned by Atlassian, for source code and development projects that use either Mercurial or Git revision control systems. Bitbucket offers both commercial plans and free accounts. It offers free accounts with an unlimited number of private repositories (which can have up to five users in the case of free accounts).

3. GitLab

GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager providing wiki, issue-tracking and continuous integration /continuous deployment pipeline features, using an open-source license, developed by GitLab Inc.The software was created by Ukrainians Dmitriy Zaporozhets and Valery Sizov, and is used by several large tech companies including Juniper, Cisco, IBM, Sony, Jülich Research Center, NASA, Alibaba, Oracle, Invincea, O’Reilly Media, Leibniz-Rechenzentrum (LRZ), CERN, European XFEL, GNOME Foundation, Boeing, Autodata, NVIDIA, and SpaceX.

I hope I provided you at least a good overview of what you need to know to get started with Git. If you found value in this article click the CLAP button below and share it with more and more people so that they can get the same value as well.

Keep learning! Keep supporting!

--

--