Git Cheatsheet

Essential Git commands reference with examples for everyday use.

32 commands

Initialize repo

Setup

Create a new Git repository in the current directory.

git init

Clone repo

Setup

Copy a remote repository to your local machine.

git clone <url>

Set user name

Setup

Set your name for all commits globally.

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

Set user email

Setup

Set your email for all commits globally.

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

Check status

Basics

Show the state of the working directory and staging area.

git status

Stage all changes

Basics

Stage all modified and new files for the next commit.

git add .

Stage specific file

Basics

Stage a specific file.

git add <file>

Commit

Basics

Record staged changes to the repository.

git commit -m "message"

View log

Basics

Show a compact, visual commit history.

git log --oneline --graph

Show diff

Basics

Show unstaged changes. Use --staged for staged changes.

git diff

Create branch

Branching

Create and switch to a new branch.

git checkout -b <branch>

Switch branch

Branching

Switch to an existing branch.

git checkout <branch>

List branches

Branching

List all local and remote branches.

git branch -a

Delete branch

Branching

Delete a merged branch locally. Use -D to force.

git branch -d <branch>

Merge branch

Branching

Merge the specified branch into the current branch.

git merge <branch>

Rebase

Branching

Reapply commits on top of another base branch.

git rebase <branch>

Fetch

Remote

Download changes from remote without merging.

git fetch origin

Pull

Remote

Fetch and merge changes from the remote branch.

git pull origin <branch>

Push

Remote

Upload local commits to the remote repository.

git push origin <branch>

Push new branch

Remote

Push a new local branch and set upstream tracking.

git push -u origin <branch>

Stash changes

Stash

Temporarily save uncommitted changes.

git stash

Apply stash

Stash

Restore the most recent stash and remove it from the list.

git stash pop

List stashes

Stash

Show all saved stashes.

git stash list

Undo last commit

Undo

Undo the last commit, keeping changes staged.

git reset --soft HEAD~1

Discard changes

Undo

Discard unstaged changes to a specific file.

git checkout -- <file>

Amend last commit

Undo

Modify the last commit message or add staged changes.

git commit --amend -m "new message"

Revert commit

Undo

Create a new commit that undoes a previous commit.

git revert <commit-hash>

Tag a commit

Tags

Create a lightweight tag at the current commit.

git tag v1.0.0

Push tags

Tags

Push all local tags to the remote repository.

git push origin --tags

Cherry-pick

Advanced

Apply a specific commit from another branch.

git cherry-pick <commit-hash>

Interactive rebase

Advanced

Interactively rewrite the last 3 commits.

git rebase -i HEAD~3

Bisect

Advanced

Binary search through commits to find when a bug was introduced.

git bisect start
git bisect bad
git bisect good <hash>

All processing happens locally in your browser. No data is sent to any server.