Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Development Last updated: October 18, 2022
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

The GIT commands cheat sheet is a handy reference for developers and contains the most commonly used commands for git projects.

What is GIT?

GIT is an open-source repository where users can place their files, including code, zips, text, or the entire project.

It has several features, like collaboration, version control, process control, etc. GIT stores file more efficiently and provide better integrity than other version control systems. Git is a command-line tool.

What is GitHub?

With its neat user interface, GitHub, a hub for developers, has become the most widely used version control system and can be accessed by anyone, anywhere.

Git Workflow

Git has different workflows, and every project should have one based on its requirements. Git workflow is nothing but a lightweight, branch-based workflow that helps teams that deploy projects regularly.

For example, a forking Git workflow creates a local copy of the repository. So, each developer has a local workspace and a central repository. This kind of workflow is good when multiple developers are involved and allows developers to collaborate on the forked copy before making changes to the main repository.

Why do we need Git?

As Git is a version control system, developers constantly work on it to save their changes, make them viewable for others, merge their changes with others, and much more.

For any changes to the Git repository, developers need to know the right commands for the right action. Below is a list of the important and commonly used Git commands:

Common Git Commands

git add

To add the specified file(s) to the staging

git add <file> -- add a specific file
git add * -- add all the files

git archive

Creates archives of commits, branches, and trees and combines multiple files into one. You can retrieve the contents by extracting the files when needed

git archive HEAD — create an archive from the HEAD ref of the repo

git archive output = ‘.tar’ — store the archived files in the given location

git archive --format=tar.gz — specifies the format of the archived file, like tar, zip, tar.gz

git branch

Lists all the branches in the repository. A new branch with the name will be created if a branch is specified. To delete a branch, use the -d option with the branch name. To rename a current branch, use -m.

git branch — lists all branches

<a href="">git branch</a> <branch_name> -- creates a new branch named 'branch_name' but doesn't check out the new branch.<br><br><code>git branch -d <branch_name> — deletes the branch, prevents the delete if any unmerged changes are there

git branch -D <branch_name> — force delete the branch even if unmerged changes are there

git branch -m <branch2> — rename the current branch

git cat-file

Display content or size/type information of repository objects

git cat-file <object> — shows object content

git cat-file -t <object> — displays the type of object

git cat-file -s <object> — shows the size of the object

git cat-file -p <object> — print the type of object in a pretty manner

git cat-file -e <object> — shows the error on stderr if the object has an invalid format or does not exist

git cat-file <type> <object> — shows the raw contents of an object

git checkout

To switch between different branches of a repository, use it carefully, as there is no ‘undo’ for it.

git checkout <branch_name> — checks out the specified branch

git checkout -b <branch_name> — create a new branch and switch to this branch

For example:

C:\Users\geekflare>git checkout -b development_branch
Switched to a new branch 'development_branch'

git clean

Clean up the working director. Files that are committed will not be removed

git clean -n  — lists the files that will be removed

git clean -f — removes the files

git clone

Create a copy of the existing repo into a new directory. Useful to get a development copy of a central repo.

git clone <central_repo> <new_dir> — create a copy of the central repo into a new directory

For example:

C:\Users\geek>git clone geekflare master
Cloning into 'master'…

git clone -branch <branch> <repo> — clones a branch from the mentioned repository

git commit

Saves the changes into a staging environment so that others can see it.

git commit — commit the changes to the staging

git commit -m <useful_msg> — gives a message upon commit to highlight the changes made

git commit -a  — commit the changes directly, without staging

Let’s say you added a file named samplefile.txt to your working directory and want to commit the file. On giving the above command, you will get the output:

Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit.
On branch master
Initial commit
Changes to be committed:
new file: samplefile.txt

Once you give the message, the changes are committed:

C:\Users\geekflare>git commit -m 'samplemsg'
[integration_branch (root-commit) ed52dd0] 'samplemsg'
1 files changed, 24 insertions(+)
create mode 100644 samplefile.txt

git config

Specifies the configuration level to write a property value into. The ‘Local’ level is the default (when nothing is specified).

git config –local — saves the configuration in the .git directory of the repo

git config –global — saves the configuration in the user’s home directory

git config –system — contains the configuration of all users and repositories and is located in the git config file of the root.

git diff

Compare the changes in the git repo, which can be done before staging, at staging, and after staging (commit).

git diff — track repo changes that are not yet staged

git diff --staged — track changes of staged files (which are not committed)

git diff HEAD  — track file changes after the commit

git diff <commit1_id> <commit2_id> — track changes between two commits; you can find out the commit_ids using ‘git log -p --follow --filename

git fetch

Fetch a branch or entire remote repository

git fetch <remote-url> — get the entire repository from the remote repository URL

git fetch <branch_url> <branchname> — fetch the specific branch

git fetch -all — fetch all the branches of a remote repo

git fetch origin — update and sync the local repo with the new changes in the remote repo

git fsck

File System ChecK command checks for validity and connectivity of database objects. It checks the SHA-1ID of the objects and the connections they make. Fsck is useful for recovering lost commits and files.

git fsck –full

git gc

Runs garbage collection on the current repository and cleans up unused files.

git gc

git grep

Searches for specific content in the repository. Git provides many options to search in different ways

git grep -i  ‘search_term’ — search by ignoring case [Man and man will be the same]

git grep -f <file_name> — show matching patterns of a particular file

git grep -e ‘search-term’ — use -e for pattern matching

git grep -E ‘regexp|multiple_exp’ –Search for regular expressions, we can search multiple by using the pipe (OR) operator

git grep -n ‘expr’ — prefix line number of the matching line

git grep -c ‘expr’ — show the count of lines that matched instead of each line

git ls-tree

To list the contents of a tree object from the current directory.

git ls -tree -d <path> — show only the specified tree entry, not the children

git ls -tree -r — recurse into sub-trees

git ls -tree -l — show the size of file (blob) objects

git ls -tree --name-only — display only file names and not the long output

git ls -tree --full-name — show full path names and not just the path relative to the current directory


C:\Users\geek>git ls-tree --full-tree -r HEAD
100644 blob e883e60087e84f1660a9673ccb86eb0adc4f004d samplefile.txt
100644 blob 1426dc6fbff0677a484b248983a8740ff30fbb80 sample_jsindex.js

git init

Create a new blank repository. It is the first command you execute to make a git project.

git init — creates a .git repository in the working directory,

For example, to create a new repo called ‘geekflare’, give the command as:

$ git init geekflare
Initialized empty Git repository in C:/Users/geek/geekflare/.git/

git instaweb

User Interface to browse the git repository through a browser. Uses the CGI script GitWeb for the same.

git instaweb --httpd=webrick — starts the server (httpd) and opens the web browser on the page.

To stop the server, use the above command with the –stop option.

git log

Records every activity in the git repository.

git log — displays the last few commits

git log --oneline — shows the output as the first 7 characters of SHA and the commit message with one commit per line

git log stat — displays more information about the modified files, like the number of lines added/removed, a summary of total records changed, lines added/removed

git log --patch (or -p) — shows the files modified, the specific changes, and their location

git log --graph — View log results in graph form

git log -<n> — display the last ‘n’ number of commits

git log --after=<date/x days/yesterday> — display all commits after a specified date. You can use --before to display commits before the specified date

git log --author=<author_name> — display commits of a particular author (user).

git log --grep=<commit message> — filter commits based on the commit message

git merge

Integrates all the development files into a single branch, combines two branches, and merges multiple commits into a single history. The merge stops if there is a conflict, and git presents the conflict files. Once the conflicts are resolved, the merge continues.

git checkout -b — first, checkout the branch to merge

git add <file>

git commit — add and commit the files

git checkout master

git merge — merge the branch with master

git prune

Deletes (Prunes) the files that are unreachable from the current branch. It is a cleaning process to remove unused files from the branch

git prune -n  — don’t prune, just show what all can be pruned

git prune -v — shows the output of actions done by the prune

git prune --progress — shows the progress of the prune

git fetch --prune — prunes all the outdated branches

git pull

It receives data from a remote server to a working repository. It updates the local (working) branch with all the latest files from the remote repo.

git pull — pull the remote repo

git pull <repo URL> — pull a particular remote branch  

git push

Pushes all the local changes into the remote repository. It is an upload process exactly opposite to the pull and fetches commands

git checkout master  — checkout the branch that has the latest changes

git push origin master — push the changes to the remote repo


C:\Users\geek>git checkout development_branch
Switched to branch 'development_branch'
C:\Users\geek>git push master development_branch
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 612 bytes | 204.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To master
[development_branch] development_branch -> development_branch

We can also use push to delete a remote branch using the command git push --delete <branch>.

For example:

C:\Users\geek>git push master --delete development_branch
To master
[deleted] development_branch

git rebase

Combines multiple commits from different branches to create a new final base commit. Useful before merging all the changes, to commit changes from different branches one by one (linearly).

git rebase <branch name> — combine the commits into one final base

If any conflicts are there, resolve them and continue the rebase:
git rebase --continue

To skip any change:
git rebase --skip

git remote

Checks the configuration of the remote server and allows connection access between remote and local.

git remote  — by default, it returns ‘origin’, the default name of the remote server given by Git

git remote -v — lists short names and URLs of all the available remote connections

git remote add <short name> <remote url> — explicitly add the remote server to the available connections. The short name can be used for git commands instead of giving the entire URL.

git remote remove <remote url/short name> — removes the remote server from the repository

git reset

Go back to a previous commit, and discard changes made after that commit

git reset <hash of the commit> — restores the repo to the specified commit. Git will retain the commit history, so you can do another reset with a different commit (hash) to undo a reset.

git rm

Removes the specific file from git. You can undo the effect of rm using the reset or checkout command

git rm <file_ name> — remove the specific file

To remove files from git but keep them in your local (staging), use:

git rm --cached

git show

View any object such as blob, tree, commit, or tag by specifying its SHA1

git show  — not specifying the <object> shows the details of the last commit on the branch.

git show <SHA1_id> — shows the object’s content specified by its SHA1 id, for example, blob id, tree id, etc. To check the type of object, use the command git cat-file -t

git show –pretty — shows the output in a pretty format.

You can provide a custom format:

git show --pretty = ‘format:Name: <format>’ Or you can use one of the short, full, one line, medium, or fuller formats, for example:

git show --pretty= ‘medium’ — this will show the author, date, title, and full commit message

git show --abbrev-commit — to abbreviate SHA1

git stash

To switch branches without committing in the current branch, store the non-committed data safely

git stash — saves the work and index state

git stash save <message> — give a message while saving

git stash list  — View the list of stashed content

git stash apply — commit the stashed changes. To apply the changes of a specific stash, use the stash index id along with apply

git stash show — view the contents of the stashed files

git stash drop — removes the most recent stash from the queue

git status

View the status of the repository and staging, i.e., the status before the commit stage. You can use this command after any other git command, like adding, updating, or removing a file

git status — shows the changes that are to be committed, or untracked (no staged) changes

For example, if you add a file in your working directory named samplefile.txt and check if it’s added, you can give the above command. It will result in the output as follows:

On branch master
No commits yet
Changes to be committed:
(use "git rm --cached …" to unstage)
new file: samplefile.txt

git tag

Friendly references are used to indicate milestones or reference points in the code

git tag <tag_name> — create a tag with the given name

git tag — list all the available tags

git tag show <tag_name> — show details of the specified tag

git tag -l “.*” — show tags that match the specified pattern or characters


Launches the git user interface that displays the contents, commits, full diff, and other details in a window

gitk — open the git repo in a window for visualization

git version

Use the git version command to check the version of the git you are using.

C:\Users\geekflare>git version
git version

Final Words

This post has listed the most commonly used git commands with their options.

Next, you can check out the GitHub credentials scanner.

  • Butterfly Thoughts
    From childhood to now, my love of writing never stopped, rather it only got better by the day, thanks to the opportunities that came along my way! I started with simple blog entries that I wrote just by observing my surroundings, and then hooked… read more
Thanks to our Sponsors
More great readings on Development
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder