Skip to main content

Git Cheat Sheet

πŸ”§ Setup

Set your name and email

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

Set default branch to main

git config --global init.defaultBranch main

πŸ“ Create or Clone Repository

Initialize new repository

git init

Clone existing repository

git clone <repo_url>

πŸ“„ Staging and Committing

Check file status

git status

Stage changes

git add <file> git add . # Stage all changes

Commit staged changes

git commit -m "Your message"

Amend last commit

git commit --amend

πŸ”™ Undoing Changes

Undo Last Commit (Not Pushed)

Keep changes staged

git reset --soft HEAD~1

Β 

Keep changes unstaged

git reset --mixed HEAD~1

Β 

Discard commit and changes

git reset --hard HEAD~1

Reset to Specific Commit

Soft reset

git reset --soft <commit_id>

Β 

Mixed reset (default)

git reset --mixed <commit_id>

Β 

Hard reset

git reset --hard <commit_id>

Revert a Commit (Safe for Pushed Commits)

Create a new commit that undoes an old one


git revert <commit_id>

🌳 Branching

List branches


git branch

Β 

Create new branch

git branch <branch_name>

Β 

Switch to a branch

git checkout <branch_name>

Β 

Create and switch

git checkout -b <branch_name>

Β 

Delete branch

git branch -d <branch_name> # Safe delete git branch -D <branch_name> # Force delete

πŸ”€ Merging & Rebasing

Merge a branch into current

git merge <branch_name>

Β 

Rebase current branch onto another

git rebase <branch_name>

Β 

Interactive rebase

git rebase -i HEAD~3

πŸ“€ Pushing & Pulling

Push commits

git push

Β 

Push new branch and track it

git push -u origin <branch_name>

Β 

Pull latest changes

git pull

πŸ” Viewing History

Full commit log

git log

Β 

Condensed view

git log --oneline

Β 

Inspect specific commit

git show <commit_id>

🧹 Cleaning Up

Delete untracked files/folders

git clean -fd

Β 

Temporarily save changes

git stash

Β 

Reapply stashed changes

git stash pop

πŸ“¦ Tags

List all tags

git tag

Β 

Create a new tag

git tag <tag_name>

Β 

Push tag to remote

git push origin <tag_name>

πŸ”— Remote Repositories

View remotes

git remote -v

Β 

Add a remote

git remote add origin <url>

Β 

Remove a remote

git remote remove origin