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