Skip to content

Git Commands Cheat Sheet

Git is an open-source distributed version control system that can handle version management of projects from very small to very large efficiently and at high speed. Git is an open-source version control software developed by Linus Torvalds to help manage the development of the Linux kernel.

Git Basics

git init <directory>

Create an empty Git repository in the specified directory. If run without parameters, initialize the current directory as a Git repository.

git clone <repo>

Clone the repository located at <repo> to the local machine. The original repository can be located on the local file system or on a remote machine via HTTP or SSH.

git config user.name <name>

Define the author name to be used for all commits in the current repository.

git add <directory>

Add the changes in <directory> for the next commit.

git commit -m "<message>"

Commit the staged snapshot, using <message> as the commit message.

git status

List which files are staged, unstaged, and untracked.

git log

Display the entire commit history using the default format.

git diff

Compare the differences between the working directory (not yet git added) and the staging area (after git add).

Undoing Changes

git revert <commit>

Create a new commit that undos all changes made in <commit> and apply it to the current branch.

git reset <file>

Remove <file> from the staging area but keep the working directory unchanged. This unstages a file without overwriting any changes.

git clean -n

Show which files will be removed from the working directory. Use the -f flag instead of the -n flag to perform the cleanup.

Rewriting History

git commit --amend

Modify the latest Git commit event. If the working directory has committable changes, those changes will also be committed when using the git commit –amend command.

git rebase <base>

Reset the current branch to <base>. <base> can be a commit ID, branch name, tag, or HEAD.

git reflog

View all operation records of all branches (including deleted commit records and reset operations).

Git Branching

git branch

List all branches. Add a <branch> parameter to create a new branch named <branch>.

git checkout -b <branch>

Create and check out a new branch named <branch>. Omit -b to check out an existing branch.

git merge <branch>

Merge the <branch> branch into the current branch.

Remote Repositories

git remote add <name> <url>

Add a new remote repository. After adding the remote, you can use <name> as a shortcut for <url> in other commands.

git fetch <remote> <branch>

Fetch all updates of the specified from repo, but do not merge with the local branch.

git pull <remote>

Fetch the specified remote copy of the current branch and immediately merge it into the local copy.

git push <remote> <branch>

Push the branch to <remote>, along with necessary commits and objects. If the remote repo does not exist, a named branch is created in the remote repo.

git config

git config --global user.name <name>

Define the author name for all commits by the current user.

git config --global user.email <email>

Define the author email for all commits by the current user.

git config --global alias.<alias-name> <git-command>

Create a shortcut (alias) for a Git command.

git config --system core.editor <editor>

Set the text editor used by commands for all users on the computer.

git config --global --edit

Open the global configuration file in a text editor for manual editing.

git log

git log -<limit>

Show top <limit> commit history records.

git log --oneline

Show commit history, compressing each commit to a single line.

git log -p

Show commit history and the full diff for each commit.

git log --stat

Show commit history, including which files were modified and the relative number of lines added or deleted from each file.

git log --author="<pattern>"

Search for commits by a specific author.

git log --grep="<pattern>"

Search commit history using regular expression matching.

git log <since>..<until>

Show commits that occurred between <since> and <until>.

git log -- <file>

Show only commit records containing the specified file.

git diff

git diff HEAD

Compare differences between the working directory (not yet git added) and the repository (after git commit).

git diff --cached

Compare differences between the staging area (after git add) and the repository (after git commit).

git reset

git reset

Reset the staging area to match the most recent commit, but keep the working directory unchanged.

git reset --hard

Reset both the staging area and working directory to the most recent commit, overwriting all changes in the working directory.

git reset <commit>

Move the current branch tip back to <commit> and reset the staging area, but do not change the working directory.

git reset --hard <commit>

Same as above, but resets both the staging area and the working directory. Deletes uncommitted changes and all commits after <commit>.

git pull

git pull --rebase <remote>

Fetch the remote copy of the current branch and rebase it onto the local copy. Use git rebase instead of merge to integrate branches.

git push

git push <remote> --force

Force push.

git push <remote> --all

Push all local branches to the specified remote.

git push <remote> --tags

Push all local tags to the remote repository.