Git Commands

Setup & Init

git initInitialize new repo
git clone <url>Clone remote repo
git config user.nameSet username
git config user.emailSet email
git config --listView all config
git config --globalSystem-wide setting

Stage & Commit

git add <file>Stage file
git add .Stage all changes
git add -pStage interactively
git commit -m "msg"Commit staged changes
git commit --amendModify last commit
git reset HEAD <file>Unstage file
git rm <file>Remove & stage deletion
git mv <old> <new>Rename & stage

Branching

git branchList branches
git branch <name>Create branch
git checkout <branch>Switch branch
git checkout -b <name>Create & switch
git switch <branch>Switch (modern)
git switch -c <name>Create & switch (modern)
git branch -d <name>Delete merged branch
git branch -D <name>Force delete branch
git branch -m <new>Rename current branch
git branch -aList all (incl. remote)

Merging & Rebasing

git merge <branch>Merge branch into current
git merge --no-ffMerge with commit (no fast-forward)
git rebase <branch>Rebase onto branch
git rebase --continueContinue after conflict
git rebase --abortCancel rebase
git cherry-pick <hash>Apply specific commit
git merge --squashSquash merge into one commit

Remote

git remote -vList remotes
git remote add origin <url>Add remote
git push origin <branch>Push to remote
git push -u origin <branch>Push & set upstream
git pullFetch & merge
git fetchDownload remote changes
git push --tagsPush all tags
git pull --rebaseRebase instead of merge

Inspect & Compare

git statusWorking tree status
git log --onelineCompact commit history
git log --graphVisual branch graph
git diffUnstaged changes
git diff --stagedStaged changes
git diff A..BCompare two branches
git show <hash>Show commit details
git blame <file>Who changed each line

Undo & Reset

git checkout -- <file>Discard file changes
git restore <file>Discard changes (modern)
git reset --soft HEAD~1Undo commit, keep staged
git reset --mixed HEAD~1Undo commit, keep changes
git reset --hard HEAD~1Undo commit, discard all
git revert <hash>Create undo commit
git clean -fdRemove untracked files
git reflogHistory of HEAD changes

Stash

git stashSave working changes
git stash popApply & remove stash
git stash applyApply, keep stash
git stash listList all stashes
git stash dropDelete top stash
git stash show -pShow stash diff

Tags

git tag v1.0Lightweight tag
git tag -a v1.0 -m "msg"Annotated tag
git tagList all tags
git tag -d v1.0Delete local tag
git push origin v1.0Push tag to remote
git checkout v1.0Checkout a tag
allprintabledoc.com