If you are tired of skimming multiple website pages and books or watching lengthy YouTube videos to find a git command, we got your back. This git command cheat sheet presents you with 25 commonly used commands arranged in alphabetical order.
You will learn how to use them with various flags in several scenarios. It would be best to bookmark this tutorial as reference material, speeding up your software development processes.
Understand git workflow before going through the git command cheat sheet
Git, in technical terms, is distributed version control system. Think of it as a binary tree to add new changes and retreat your steps.
Working on the terminal involves three primary git workflow levels: working directory, index, and history. The working directory is where you actively modify the files. Index, also called the staging area, is the intersection of the working directory and history.
At the index, git snapshots the changes, waiting for you to undo them or permanently store them in the git database. Each permanently stored change becomes part of the history and is identifiable with a commit hash.
You can remotely track the changes on a cloud-based service, such as Bitbucket, GitLab, or GitHub. In a collaborative workflow, you may include sub-workflows called branches.
Several commands are applicable in the above lines of file tracking. Here is a typical git command cheat sheet.
25 most popular git commands
1. git add
Git add stages one or many files depending on the options given. You can stage one file using
git add <file>
or many files using
git add .
git add *
2. git branch
Git branch is crucial in listing or deleting branches. To list the branch(es), use
for local branches,
git branch -r
for remote branches,
git branch -a
for local and remote branches
Delete a branch using
git branch -d <branch name>
3. git checkout
Git checkout command helps to switch between branches. You can also use it to undo changes in a file or commit.
4. git clean
Git clean enables you to view or discard untracked files or folders depending on the given flag. For instance,
git clean -n
lists untracked files about to get deleted,
git clean -f
clears all untracked files,
git clean -i
interactively removes all untracked files, while
git clean -fd
deletes both untracked files and directories.
git clean -fX
to delete ignored and untracked files and
git clean -fx
to remove both unignored and ignored files.
5. git clone
git clone <remote URL>
enables you to create a copy of a remote repo locally.
6. git commit
Git commit is one of the most crucial commands we had to include in this git command cheat sheet. Depending on the provided flag, it saves changes in the history.
-m flag symbolizes message,
--all flag saves modified and deleted files. Here is an example of a git commit message with the
git commit -m "Feat: Add a text file"
7. git fetch
It transfers remote branch refs, commits, and tags into your local project.
Running the fetch command with a remote name
git fetch <remote>
gets all the repo's branches, downloading all the needed files and commits from the remote. You can specify the remote branch to get a more condensed answer.
git fetch <remote> <branch>
If you want all remotes with their respective branches, use
git fetch --all
git fetch --dry-run
to output actions to be taken without applying the actions.
git fetch -v
shows all the fetched commits.
8. git init
The init command instantiates a repo. The level of interaction with the repo depends on the supplied flags. For example, the
git init --bare
omits the working directory, keeping the files in the
9. git log
Running the git log commands, such as
git log --oneline
git log --pretty=oneline
reveals the state of the commit history.
10. git ls-files
lists all the tracked files.
11. git ls-remote
Git ls-remote is one of the alternatives to
git branch -r.
git ls-remote --heads <remote name>
It lists all remote branches.
12. git merge
git merge <branch>
takes changes from the feature branch and adds them to the main one, whereas
git merge --squash <branch>
groups feature branch's commits before combining the feature branch's changes on the main branch's workflow.
13. git pull
git pull synchronizes a local repo with its upstream by downloading the most updated state of the repo
14. git push
git push is the opposite of
git pull. It synchronizes a remote repository by uploading the most updated state of the local repository.
15. git rebase
Git rebase <branch>
creates a linear repo by stacking the changes from the feature branch onto the main branch's commits.
16. git reflog
The reflog command
reveals all updates made at each branch's tip. It mainly works with the checkout command to restore files you deleted (a few hours ago) using the hard reset command.
17. git reset
Git reset takes three forms: hard, soft, mixed. Here is the example of each command in action.
The hard reset on a commit hash
git reset --hard <commit hash>
removes a commit, then deletes the untracked files from the working directory.
A soft reset
git reset --soft <commit hash>
git reset --soft HEAD^
git reset --soft HEAD~1
changes the commit HEAD's reference, taking the changes to the index. Lastly, the mixed reset command on a file
git reset HEAD <file>
unstages the file, keeping the changes in the working directory.
18. git restore
The restore command in combination with the
git restore --staged <file>
unstages a modified file, whereas
git restore <file>
deletes untracked files.
19. git remote
Remote is the name of your repository on a cloud service. You can add it as follows:
git remote add <remote-name> <url>
To delete the remote use
git remote rm <remote-name>
git remote remove <remote-name>
git remote rename <old-name> <new-name>
to rename a remote.
Lastly, introducing the
git remote -v
lists the remotes.
20. git rm
Git remove command
git rm --cached <file>
unstages a new file.
21. git revert
git revert <commit hash>
undoes operations then creates a new commit, inversing the specified changes.
22. git stash
The stash command
discards the uncommitted changes, recording the discarded changes in the
You can view the stashed files using the
git stash show
Another commonly applied subcommand is
git stash apply
which restores the discarded changes.
23. git status
git status command inspects the staging condition of files. For example, files marked in red are untracked, whereas those marked in green are indexed.
24. git switch
Git switch enables you to enter or leave a branch.
git switch <branch name>
You can also use it to create a branch and check out the branch by combining it with the
git switch -c <branch name>
25. git tag
You can create a lightweight tag using the command
git tag <tag name>
git tag -a <tag name> -m <message>
for an annotated tag.
To list the tags in alphabetical order, use
git tag only.
An updated, straightforward git command cheat sheet is one of the most necessary reference materials for a software developer. Use it to improve the efficiency of your software development.