Git Cheatsheet

A practical reference for everyday Git commands.


Installation & Setup

Check Git Version

git --version

Set Global Username & Email

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

View Config

git config --list

Repository Initialization

Initialize a New Repository

git init

Clone an Existing Repository

git clone <repository-url>

Basic Workflow

Check Status

git status

Add Files to Staging

Add Single File

git add filename

Add All Files

git add .

Commit Changes

git commit -m "Your commit message"

Add & Commit in One Step

git commit -am "Your commit message"

Viewing Changes

Show Unstaged Changes

git diff

Show Staged Changes

git diff --staged

View Commit History

git log

Compact Log

git log --oneline

Graph View

git log --oneline --graph --all

Branching

List Branches

git branch

Create New Branch

git branch branch-name

Switch Branch

git checkout branch-name
git switch branch-name

Create & Switch in One Step

git checkout -b branch-name

Modern Way

git switch -c branch-name

Delete Branch

git branch -d branch-name

Merging

Merge Branch into Current Branch

git merge branch-name

Remote Repositories

Add Remote

git remote add origin <repository-url>

View Remotes

git remote -v

Push to Remote

git push origin branch-name

Push First Time (Set Upstream)

git push -u origin branch-name

Pull Latest Changes

git pull origin branch-name

Fetch Without Merging

git fetch

Undo & Reset

Unstage File

git restore --staged filename

Discard Changes in File

git restore filename

Amend Last Commit

git commit --amend

Reset to Previous Commit (Soft)

git reset --soft HEAD~1

Reset to Previous Commit (Hard)

git reset --hard HEAD~1

Hard reset deletes changes permanently.


Stashing

Save Changes Temporarily

git stash

List Stashes

git stash list

Apply Stash

git stash apply

Apply & Remove Stash

git stash pop

Tags

Create Tag

git tag v1.0.0

Push Tag

git push origin v1.0.0

List Tags

git tag

Cleanup

Remove Untracked Files

git clean -f

Remove Untracked Files & Directories

git clean -fd

Helpful Shortcuts

Show Who Changed a Line

git blame filename

Show Specific Commit

git show commit-hash

Rename Branch

git branch -m new-branch-name

Best Practices

  • Commit small, meaningful changes
  • Use clear commit messages
  • Pull before pushing
  • Use branches for features
  • Never force push to shared branches

Happy Coding