Git commands cheat sheet [2024 Updated]

Do you want to learn some basic GIT commands? You’ve come to the right place. Read our simple cheat sheet that you can use for daily reference.

What is git cheat sheet?

The git cheat sheet is a concise, easy-to-read document for quickly learning how to use some of the most popular and useful commands in the Git environment. It’s designed for beginners who are new to Git and have little or no prior experience with it.

Most Common GIT Commands

A git command cheat sheet is a document that lists the most commonly used commands and their corresponding syntax.
The following is a list of git commands with syntax:

  • -git add -A
  • -git commit -m “commit message”
  • -git push origin master
  • -git pull origin master
  • -git clone {URL of git repository}

Basic Commands of Git

Here are some basic GIT commands you need to know:

#01: How to setup git config

git config:- can be used to set user-specific configuration values like username, email, file format, and so on.

  • set a username that is identifiable for credit when review version history
git config --global user.name
  • set an email address that will be associated with each history marker
git config --global user.email
  • set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto
  • git init:- initialize an existing directory as a Git repository [ following Git command will create a repository in the current directory ]
git init
  • retrieve an entire repository from a hosted location from URL
git clone [url]

#02: Git Stage & Snapshot

  • git status:- show modified files in working directory, staged for your next commit
git status
  • add a file as it looks now to your next commit (stage)
git add [file]
  • unstage a file while retaining the changes in working directory
git reset [file]
  • diff of what is changed but not staged
git diff
  • diff of what is staged but not yet committed
git diff --staged
  • commit your staged content as a new commit snapshot
git commit -m "name of working details "

#03: Git Branch & Merge

Isolating work in branches, changing context, and integrating changes

  • list your branches. a * will appear next to the currently active branch
git branch
  • create a new branch at the current commit
git branch [branch name]
  • switch to another branch and check it out into your working directory
git checkout
  • merge the specified branch’s history into the current one
git merge [branch]
  • show all commits in the current branch’s history
git log

#04: Git Inspect & Compare

Examining logs, diffs and object information

  • show all commits in the current branch’s history
git log
  • show the commits on branchA that are not on branchB
git log branchB..branchA
  • show the commits that changed file, even across renames
git log --follow [file]
  • show the diff of what is in branchA that is not in branchB
git diff branchB...branchA
  • show any object in Git in human readable format
git show [SHA]
shortcut keys of computer

Best 200+ Keyboard Shortcut Keys

Hello Guys, today in this article you will get the Best 200+ Shortcut Keys of Computer, and also we will learn about how we can use these shortcut keys to increase the speed of working on the computer many times.

#05: Git Share & Update

Retrieving updates from another repository and updating local repository

  • add a git URL as an alias
git remote add [alias] [url]
  • merge a remote branch into your current branch to bring it up to date
git merge [alias]/[branch]
  • fetch down all the branches from that Git remote
git fetch [alias]
  • Transmit local branch commits to the remote repository branch
git push [alias] [branch]
  • fetch and merge any commits from the tracking remote branch
git pull

#06: Git Temporary Commits

Temporarily store modified, tracked files in order to change branches

  • Save modified and staged changes
git stash
  • list stack-order of stashed file changes
git stash list
  • write working from top of stash stack
git stash pop
  • discard the changes from top of stash stack
git stash drop

#07: Git Tracking Path Changes

Versioning file removes and path changes

  • delete the file from project and stage the removal for commit
git rm [file]
  • change an existing file path and stage the move
git mv [existing-path] [new-path]
  • show all commit logs with indication of any paths that moved
git log --stat -M

#08: Git Remote Management

git remote add [alias] [url]: Add a remote repository URL as an alias.
git remote -v: View remote repositories along with their URLs.
git remote rm [alias]: Remove a remote repository alias.
git remote rename [old-alias] [new-alias]: Rename a remote repository alias.

#09: Git Remote Management

git fetch [remote]: Download objects and refs from another repository.
git merge [remote]/[branch]: Merge changes from a remote branch into the current branch.
git pull [remote] [branch]: Fetch and merge changes from a remote repository.
git push [remote] [branch]: Push local branch commits to a remote repository.

#10: Git Tagging & Versioning

git tag [tagname]: Create a lightweight tag at the current commit.
git tag -a [tagname] -m "tag message": Create an annotated tag with a message.
git tag -l: List all tags.
git push [remote] [tagname]: Push tags to a remote repository.
git push --tags: Push all tags to a remote repository.

#11: Git Stash Management

git stash: Stash changes in the working directory.
git stash list: List all stashed changes.
git stash pop: Apply the most recent stash and remove it from the stack.
git stash apply [stash@{n}]: Apply a specific stash without removing it from the stack.
git stash drop [stash@{n}]: Remove a specific stash from the stack.

#12: Git Submodule Operations

git submodule add [repository] [path]: Add a submodule to the repository.
git submodule init: Initialize submodules.
git submodule update: Update submodules to the latest commit.
git submodule foreach [command]: Run a command in each submodule.

#13: Git Ignore & Exclude Patterns

.gitignore: Create or modify the .gitignore file to specify intentionally untracked files.
.git/info/exclude: Exclude files locally without committing to the repository.

#14: Git Bisect for Troubleshooting

git bisect start: Start the bisecting process.
git bisect bad: Mark the current commit as bad.
git bisect good [commit]: Mark a specific commit as good.
git bisect reset: Finish the bisecting process and return to the original branch.

#15: Git Bisect for Troubleshooting

git rebase -i [base]: Start an interactive rebase session.
pick: Keep the commit unchanged.
reword: Keep the commit but edit the commit message.
edit: Pause rebase to amend the commit.
squash: Combine the commit with the previous one.
drop: Remove the commit from the history.

How to rollback an old commit in Git:

You can revert an old commit using its commit id. This opens the editor so you can add a commit message.

git revert [commit id here]

How to delete a branch in Git:

you can delete a branch using the command below:

git branch -d [branch name]

Git MCQ questions and answers

Which is Distributed version control system?

Answer:- git

How do you supply a commit message to a commit?

Answer:- git commit -m “work name”

What’s a Command to staging all the changes you have?

Answer:- git add

How to check your Git configuration?

Answer:- git config -l

how to initialize a git repository?

Answer:- git init

How to check a repository status in Git?

Answer:- git status

How to see your commit history in Git?

Answer:- git log

How to check a specific commit in Git?

Answer:- git show [commit-id]

How to rename files in Git?

Answer:- git mv old_filename new_filename

How to check remote URLs in Git?

Answer:- git remote -v

Awesome GIT
Commands Cheat Sheet pdf

The GIT Commands Cheat Sheet pdf is a quick reference guide that can be used by beginners and experienced developers alike. It contains all the GIT commands in one place and is easy to read.

Conclusion

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

The git cheat sheet will provide you with the most basic commands that are used by developers on a daily basis. If you think I have miss an important command or would like to add to the discussion, do leave a comment below.

Leave a Comment