$ tpt --version 1.0
tpt
About
online
01 Jan 1970
00:00:00
Development
/git-cheatsheet

Git Cheatsheet

Quick reference for common Git commands

all tools
Init / Clone
git init

Initialize a new repository

git init my-project

git clone

Clone a remote repository

git clone https://github.com/user/repo.git

git remote add

Add a remote

git remote add origin https://github.com/user/repo.git

git remote -v

List remotes

git remote -v

Branch
git branch

List branches

git branch

git branch <name>

Create a branch

git branch feature-x

git checkout <name>

Switch to branch

git checkout main

git checkout -b <name>

Create and switch

git checkout -b feature-x

git switch <name>

Switch branch (new)

git switch main

git switch -c <name>

Create and switch

git switch -c feature-x

git branch -d <name>

Delete branch

git branch -d old-branch

git branch -D <name>

Force delete branch

git branch -D unwanted

git merge <name>

Merge branch into current

git merge feature-x

Commit
git add <file>

Stage a file

git add src/index.ts

git add .

Stage all changes

git add .

git commit -m

Commit with message

git commit -m 'fix: typo'

git commit -am

Add+commit tracked files

git commit -am 'update'

git commit --amend

Amend last commit

git commit --amend -m 'new msg'

git status

Show working tree status

git status

Push / Pull
git push

Push to remote

git push origin main

git push -u <remote> <branch>

Set upstream and push

git push -u origin main

git push --force

Force push (careful!)

git push --force

git pull

Pull from remote

git pull origin main

git fetch

Fetch without merging

git fetch origin

Merge / Rebase
git merge <branch>

Merge branch

git merge feature-x

git merge --abort

Abort a merge

git merge --abort

git rebase <branch>

Rebase onto branch

git rebase main

git rebase -i HEAD~n

Interactive rebase

git rebase -i HEAD~3

git rebase --abort

Abort a rebase

git rebase --abort

git rebase --continue

Continue after resolving

git rebase --continue

Stash
git stash

Stash working changes

git stash

git stash pop

Apply and drop stash

git stash pop

git stash list

List stashes

git stash list

git stash apply

Apply without dropping

git stash apply stash@{0}

git stash drop

Drop a stash

git stash drop stash@{0}

git stash clear

Clear all stashes

git stash clear

Reset
git reset <file>

Unstage a file

git reset src/index.ts

git reset --soft HEAD~1

Undo commit, keep changes

git reset --soft HEAD~1

git reset --mixed HEAD~1

Undo commit, unstage

git reset --mixed HEAD~1

git reset --hard HEAD~1

Undo commit, discard changes

git reset --hard HEAD~1

git revert <commit>

Revert a commit (safe)

git revert abc123

Diff
git diff

Show unstaged changes

git diff

git diff --staged

Show staged changes

git diff --staged

git diff <branch>

Compare with branch

git diff main

git diff <commit> <commit>

Compare commits

git diff abc123 def456

Log
git log

Show commit history

git log

git log --oneline

Compact log

git log --oneline -10

git log --graph

Show branch graph

git log --oneline --graph --all

git log -p

Show patches

git log -p file.ts

git blame <file>

Show who changed each line

git blame src/index.ts

Config
git config --global user.name

Set user name

git config --global user.name 'Alice'

git config --global user.email

Set user email

git config --global user.email 'alice@example.com'

git config --list

List all config

git config --list

git config --global alias.<name>

Create alias

git config --global alias.co checkout

.gitignore

Ignore files

node_modules/\n.env