Git Cheatsheet

Discover essential Git commands and tips in our concise cheatsheet, perfect for streamlining your version control workflow.

git diagram

Configuration

Set username for commits (affects all repositories)

git config --global user.name "Your Name"

Set email for commits (affects all repositories)

git config --global user.email "you@example.com"

Set default editor for git (affects all repositories)

git config --global core.editor "editor"

List all git configurations

git config --list

Repository

Initialize a local repository in the working directory

git init

Clone a remote repository to the local machine

git clone <repo>

Branching

List branches in the local repository

git branch

Create a new branch in the local repository

git branch <name>

Switch to a different branch in the working directory

git checkout <name>

Create a new branch and switch to it in the working directory

git checkout -b <name>

Delete a branch from the local repository

git branch -d <name>

Changes

Show the working directory status

git status

Stage a file to the staging area

git add <file>

Stage all changes in the working directory

git add .

Commit staged changes to the local repository

git commit -m "message"

Amend the last commit in the local repository

git commit --amend

Remote

List all remote repositories linked to the local repository

git remote

Add a remote repository link to the local repository

git remote add <name> <url>

Remove a remote repository link from the local repository

git remote remove <name>

Fetch updates from a remote repository to the local repository

git fetch <remote>

Pull updates from a remote repository to the working directory

git pull <remote>

Push commits from the local repository to a remote repository

git push <remote> <branch>

Stashing

Save changes in the working directory temporarily

git stash

List all stashed changes

git stash list

Apply stashed changes to the working directory

git stash apply

Apply and remove the latest stash from the stash list

git stash pop

Drop a specific stash from the stash list

git stash drop

History

Show commit history in the local repository

git log

Show a condensed commit history in the local repository

git log --oneline

Show commit history as a graph in the local repository

git log --graph

Show detailed information about a specific commit

git show <commit>

Differences

Show differences in the working directory and staging area

git diff

Show differences between the staging area and the last commit

git diff --staged

Cleaning

Remove untracked files from the working directory

git clean -f

Tagging

List all tags in the local repository

git tag

Create a tag in the local repository

git tag <name>

Push a tag to the remote repository

git push <remote> <tag>

Delete a tag in the local repository

git tag -d <name>

Delete a tag in the remote repository

git push <remote> --delete <name>

Cherry-pick

Apply a commit from another branch to the current branch in the local repository

git cherry-pick <commit>

Reflog

Show a log of changes to HEAD in the local repository

git reflog

Bisect

Start a binary search to find the commit that introduced an issue

git bisect start

Submodules

Add a submodule to the working directory

git submodule add <repo> <path>

Initialize and update submodules in the working directory

git submodule update --init --recursive

Archive

Create a zipped archive of a branch

git archive --format=zip --output=<file.zip> <branch>

Blame

Show line-by-line history for a file

git blame <file>

Miscellaneous

Show a log of changes to HEAD in the local repository

git reflog

Show detailed information about a specific commit

git show <commit>

Apply a commit from another branch to the current branch in the local repository

git cherry-pick <commit>

Start a binary search to find the commit that introduced an issue

git bisect start