Setup
git config --global user.name "Name" Set username git config --global user.email "email" Set email git config --list Show config git init Initialize repo git clone <url> Clone repository git clone --depth 1 <url> Shallow clone Basic Commands
git status Show status git add <file> Stage file git add . Stage all changes git add -p Stage interactively git commit -m "message" Commit with message git commit --amend Amend last commit git commit --amend --no-edit Amend without edit Branches
git branch List branches git branch -a All branches git branch <name> Create branch git checkout <branch> Switch branch git checkout -b <branch> Create & switch git switch <branch> Switch (new) git switch -c <branch> Create & switch (new) git branch -d <branch> Delete branch git branch -D <branch> Force delete git branch -m <old> <new> Rename branch Merging & Rebasing
git merge <branch> Merge branch git merge --no-ff <branch> Merge with commit git rebase <branch> Rebase onto branch git rebase -i HEAD~3 Interactive rebase git cherry-pick <commit> Cherry pick commit git merge --abort Abort merge git rebase --abort Abort rebase Remote
git remote -v List remotes git remote add origin <url> Add remote git fetch Fetch changes git fetch --all Fetch all remotes git pull Pull changes git pull --rebase Pull with rebase git push Push changes git push -u origin <branch> Push new branch git push --force-with-lease Safe force push History
git log Show history git log --oneline Compact history git log --graph Graph view git log -p <file> File history git show <commit> Show commit git diff Unstaged changes git diff --staged Staged changes git diff <branch1>..<branch2> Compare branches git blame <file> Line-by-line blame Undo Changes
git checkout -- <file> Discard changes git restore <file> Discard (new) git restore --staged <file> Unstage file git reset HEAD~1 Undo last commit (keep) git reset --hard HEAD~1 Undo last commit (discard) git revert <commit> Revert commit git clean -fd Remove untracked Stash
git stash Stash changes git stash save "message" Stash with message git stash list List stashes git stash pop Apply & remove git stash apply Apply (keep stash) git stash drop Remove stash git stash clear Clear all stashes Tags
git tag List tags git tag v1.0.0 Create tag git tag -a v1.0.0 -m "msg" Annotated tag git push origin v1.0.0 Push tag git push --tags Push all tags git tag -d v1.0.0 Delete local tag