Link copied to clipboard
Git

Git Cheatsheet: Version Control Commands

Essential Git commands for version control, branching, merging, and collaboration. Quick reference for developers.

·9 min read·
Git Cheatsheet: Version Control Commands

Git Cheatsheet

Essential Git commands for version control, branching, merging, and collaboration.

Useful Git Resources


Setup & Configuration

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

Set your name for commits

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

Set your email for commits

git config --global init.defaultBranch main

Set default branch name

git config --list

Show all configuration settings

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

Set VS Code as default editor


Repository Setup

git init

Initialize a new Git repository

git clone <url>

Clone a remote repository

git clone --depth 1 <url>

Shallow clone (only latest commit)

git remote add origin <url>

Add a remote repository

git remote -v

List remote repositories

git remote remove origin

Remove a remote


Basic Workflow

git status

Show working tree status

git add <file>

Stage a file for commit

git add .

Stage all changes

git add -p

Interactively stage changes

git commit -m "message"

Commit staged changes with message

git commit -am "message"

Stage and commit all tracked files

git commit --amend

Modify the last commit

git diff

Show unstaged changes

git diff --staged

Show staged changes


Branching

git branch

List local branches

git branch -a

List all branches (local and remote)

git branch <name>

Create a new branch

git checkout <branch>

Switch to a branch

git checkout -b <name>

Create and switch to a new branch

git switch <branch>

Switch to a branch (modern)

git switch -c <name>

Create and switch to new branch (modern)

git branch -d <name>

Delete a branch (safe)

git branch -D <name>

Force delete a branch

git branch -m <old> <new>

Rename a branch


Merging & Rebasing

git merge <branch>

Merge a branch into current branch

git merge --no-ff <branch>

Merge with a merge commit

git rebase <branch>

Rebase current branch onto another

git rebase -i HEAD~3

Interactive rebase last 3 commits

git cherry-pick <commit>

Apply a specific commit to current branch

git merge --abort

Abort a merge in progress

git rebase --abort

Abort a rebase in progress


Remote Operations

git fetch

Download remote changes (don't merge)

git pull

Fetch and merge remote changes

git pull --rebase

Fetch and rebase local changes

git push

Push commits to remote

git push -u origin <branch>

Push and set upstream for branch

git push --force-with-lease

Force push with safety check

git push origin --delete <branch>

Delete remote branch


History & Inspection

git log

Show commit history

git log --oneline

Show compact commit history

git log --graph --oneline

Show history with branch graph

git log -p

Show commits with diffs

git log --author="name"

Filter commits by author

git show <commit>

Show a specific commit

git blame <file>

Show who changed each line

git reflog

Show reference log (all HEAD movements)


Undoing Changes

git checkout -- <file>

Discard changes in working directory

git restore <file>

Discard changes (modern)

git restore --staged <file>

Unstage a file

git reset HEAD~1

Undo last commit (keep changes)

git reset --hard HEAD~1

Undo last commit (discard changes)

git revert <commit>

Create a commit that undoes a commit

git clean -fd

Remove untracked files and directories


Stashing

git stash

Stash current changes

git stash save "message"

Stash with a description

git stash list

List all stashes

git stash pop

Apply and remove latest stash

git stash apply

Apply latest stash (keep it)

git stash drop

Remove latest stash

git stash clear

Remove all stashes


Tags

git tag

List all tags

git tag v1.0.0

Create a lightweight tag

git tag -a v1.0.0 -m "Release 1.0"

Create an annotated tag

git push origin v1.0.0

Push a tag to remote

git push origin --tags

Push all tags to remote

git tag -d v1.0.0

Delete a local tag

More articles