Git Commands Cheat Sheet

This is a practical reference for daily Git usage. It is focused on commands used in real code work.

Setup

git --version git config --global user.name "Your Name" git config --global user.email "you@example.com"

Start a repo

git init

Clone an existing repo

git clone https://github.com/your-user/project.git

Check status

git status

Add changes

git add file.txt git add .

Commit changes

git commit -m "Add login flow"

View commit history

git log git log --oneline git log --oneline --graph --decorate --all

See what changed

git diff git diff --staged git diff HEAD

Undo changes safely

git restore file.txt git restore --staged file.txt

This is safer than deleting changes by hand.

Reset a commit carefully

git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1

Use these with caution. A hard reset drops the changes completely.

Create a branch

git checkout -b feature/login

Modern Git also supports:

git switch -c feature/login

Switch branches

git switch main git switch feature/login

Merge a branch

git checkout main git merge feature/login

Pull latest changes

git pull origin main

Push changes

git push origin feature/login git push -u origin main

Remote commands

git remote -v git remote add origin https://github.com/your-user/project.git git remote remove origin

Stash temporary work

git stash git stash list git stash pop

This is useful when work is incomplete but a branch switch is needed.

Tag a release

git tag v1.0.0 git push origin v1.0.0

Create a patch from a commit

git cherry-pick <commit-hash>

Revert a commit

git revert <commit-hash>

This is safer than reset when the commit already exists on a shared branch.

Production note

In enterprise teams, Git command discipline matters. Developers often use pull requests, branch protection, and required reviews before merging to main.

Common pitfalls

Key takeaways