Skip to content

Git Cheats

A categorized compendium of the most useful, commonly referenced git commands, workflows, and tips for daily use and troubleshooting.

Hand-crafted with ❤️ for rapid reference.

Not intended as a replacement for the official documentation


Quick Command Reference

Setup & Configuration

Check Git Version

Verify your Git installation

git --version

Set Identity

Configure your name for commits

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

Configure your email for commits

git config --global user.email "[email protected]"

Editor Configuration

Set VS Code as default editor

git config --global core.editor "code --wait"

Set Vim as default editor

git config --global core.editor "vim"

Set Notepad++ as default editor

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Set Sublime Text as default editor

git config --global core.editor "'subl' -w"

View Configuration

Display all configurations

git config --list

View a specific config value

git config user.name

Configuration Scopes

System-wide configuration (all users)

git config --system <key> <value>

User configuration (current user)

git config --global <key> <value>

Repository configuration (current repo only)

git config --local <key> <value>

Repository Operations

Create New Repository

Initialize a new Git repository in current directory

git init

Clone Existing Repository

Clone via HTTPS

git clone https://github.com/username/repository.git

Clone via SSH

git clone [email protected]:username/repository.git

Clone to specific directory

git clone https://github.com/username/repository.git my-project

Clone specific branch

git clone -b branch-name https://github.com/username/repository.git

Clone with limited history (shallow clone)

git clone --depth=1 https://github.com/username/repository.git

Ignore Files

Create gitignore file to specify ignored files

touch .gitignore

Basic Workflow

Check Status

View repository status

git status

View condensed status output

git status -s

Stage Changes

Stage a specific file

git add filename.txt

Stage all changes

git add .

Stage all changes (including deleted files)

git add -A

Stage changes interactively

git add -p

Commit Changes

Commit with a message

git commit -m "Description of changes"

Add tracked files and commit in one step

git commit -am "Description of changes"

Amend the previous commit

git commit --amend

View Changes

Show unstaged changes

git diff

Show staged changes

git diff --staged

Show changes by filename only

git diff --name-only

Undo Changes

Restore file from HEAD

git restore filename.txt

Unstage changes (keep in working directory)

git restore --staged filename.txt

Restore file from a specific commit

git restore --source=HEAD~1 filename.txt

File Operations

Remove files from Git and filesystem

git rm filename.txt

Remove files from Git but keep in filesystem

git rm --cached filename.txt

Remove directory recursively

git rm -r directory

Move or rename files

git mv oldname.txt newname.txt

Branching & Merging

Branch Management

Create a new branch

git branch branch-name

Create and switch to new branch

git checkout -b branch-name

Alternative to create and switch to new branch (Git 2.23+)

git switch -c branch-name

List local branches

git branch

List all branches (local and remote)

git branch -a

Switch to an existing branch

git checkout branch-name

Alternative to switch branches (Git 2.23+)

git switch branch-name

Delete a branch (safe, prevents deletion of unmerged branches)

git branch -d branch-name

Force delete a branch

git branch -D branch-name

Rename current branch

git branch -m new-name

Rename specific branch

git branch -m old-name new-name

Push and track branch

git push -u origin branch-name

Compare branches

git diff branch1..branch2

Merging

Merge a branch into current branch

git merge branch-name

Merge with no fast-forward (always creates merge commit)

git merge --no-ff branch-name

Merge and squash commits into a single one

git merge --squash branch-name

Abort an in-progress merge

git merge --abort

Rebasing

Rebase current branch onto another

git rebase branch-name

Interactive rebase for editing commits

git rebase -i commit-hash

Continue rebase after resolving conflicts

git rebase --continue

Cherry-picking

Apply a specific commit to current branch

git cherry-pick commit-hash

Handle Conflicts

Get a specific file from another branch

git checkout branch-name -- filename

List conflicting files

git diff --name-only --diff-filter=U

Remote Operations

Remote Management

Add a remote repository

git remote add origin https://github.com/username/repo.git

List all remotes with URLs

git remote -v

Sync with Remotes

Download remote data without merging

git fetch origin

Download and merge from remote

git pull origin branch-name

Download and rebase instead of merging

git pull --rebase origin branch-name

Upload local commits to remote

git push origin branch-name

Push and set up tracking

git push -u origin branch-name

Delete a remote branch

git push origin --delete branch-name

List remote branches

git branch -r

Stash & Clean

Stash Management

Save working changes temporarily

git stash

List all stashes

git stash list

Show stash details

git stash show -p

Apply stash without removing it

git stash apply

Apply and remove stash

git stash pop

Remove a stash

git stash drop

Remove all stashes

git stash clear

Stash specific files only

git stash push file1.txt file2.txt

Stash with a descriptive message

git stash push -m "Work in progress on feature X"

Working Directory Cleanup

Preview what would be removed

git clean -n

Remove untracked files

git clean -f

Remove untracked files and directories

git clean -fd

History & Inspection

View History

Show commit history

git log

Show compact history

git log --oneline

Show history with branch graph

git log --graph --oneline

Show history for a specific file

git log -- filename.txt

Show history with changes

git log -p

Inspect Changes

See who changed each line in a file

git blame filename.txt

Show changes between working directory and HEAD

git diff

Show changes between staged and HEAD

git diff --staged

Compare two commits

git diff commit1 commit2

Compare two branches

git diff branch1 branch2

Show details of a specific commit

git show commit-hash

Search for specific content in history

git log -S"search string"

Tags & Releases

Tag Management

Create a lightweight tag

git tag v1.0.0

Create an annotated tag

git tag -a v1.0.0 -m "Version 1.0.0"

List all tags

git tag

Show tag details

git show v1.0.0

Push specific tag to remote

git push origin v1.0.0

Push all tags to remote

git push --tags

Delete a local tag

git tag -d v1.0.0

Delete a remote tag

git push origin --delete v1.0.0

Checkout a tagged version

git checkout v1.0.0

Create a tag for a previous commit

git tag v0.9.0 commit-hash

Troubleshooting

Fix Mistakes

Modify the last commit

git commit --amend

Undo last commit but keep changes staged

git reset --soft HEAD~1

Undo last commit and unstage changes

git reset HEAD~1

Discard all local changes

git reset --hard HEAD

View reference history log

git reflog

Recover deleted work from reflog

git branch recover-branch HEAD@{1}

Cancel current merge operation

git merge --abort

Create branch when in detached HEAD state

git checkout -b new-branch

Force push with safety mechanism

git push --force-with-lease

Optimize repository

git gc --aggressive

Advanced Usage

Git Hooks

Path to pre-commit hook

.git/hooks/pre-commit

Object Inspection

Examine Git objects

git cat-file -p HEAD

External Repositories

Add a submodule

git submodule add https://github.com/user/repo

Add a subtree

git subtree add --prefix=lib https://github.com/user/repo main

Debugging

Find the commit that introduced a bug

git bisect start
git bisect bad  # Mark current commit as bad
git bisect good v1.0  # Mark a known good commit

History Rewriting

Interactive history editing

git rebase -i HEAD~5

Rewrite complete history

git filter-repo --path path/to/remove --invert-paths

Patch Management

Create a patch file

git format-patch -1 HEAD

Apply a patch file

git am < patch-file.patch

Repository Management

Create a portable repository bundle

git bundle create repo.bundle --all

Search code in repository

git grep -n "TODO"

Create additional working directory

git worktree add ../feature feature-branch

Partial repository checkout

git sparse-checkout set path/to/dir

Development Workflows

Feature Development

Start a new feature branch

git checkout -b feature/new-feature

Push new feature branch to remote

git push -u origin feature/new-feature

Working with Forks

Add original repository as upstream

git remote add upstream https://github.com/original/repo.git

Sync fork with original repository

git fetch upstream && git merge upstream/main

GitFlow Operations

Start a new feature using GitFlow

git flow feature start feature-name

Finish a feature using GitFlow

git flow feature finish feature-name

Start a new release using GitFlow

git flow release start 1.0.0

Finish a release using GitFlow

git flow release finish 1.0.0

Create a hotfix using GitFlow

git flow hotfix start 1.0.1

Exploring Git Topics

Git Fundamentals

Branch Management

Remote Collaboration

Project History & Versions

Customization & Troubleshooting

Advanced Usage