A work-in-progress list of my favorite Git commands
A concise reference of my most-used Git commands. Updated September 2024.
Repository Setup
git init
Init repogit clone <url>
Clone repo (add--depth <n>
for shallow)
Branching
git checkout -b <branch>
New branchgit branch --list
List branchesgit branch -D <branch>
Delete branchgit branch --set-upstream-to=origin/<remote> <local>
Link remote/localgit push --set-upstream origin <branch>
Push & track
Staging & Status
git status
Statusgit add --all
Stage allgit restore <file>
Discard changesgit reset --hard
Reset allgit reset --hard origin/main
Reset to remotegit reset --mixed HEAD
Unstage, keep changes
Committing & Pushing
git commit -m "msg"
Commitgit push origin <branch>
Push branchgit push origin --delete <branch>
Delete remote branchgit pull
Pull latest
File & History
git checkout <commit> <file>
Restore filegit show <commit>:<file>
Show file at commitgit log -v
Verbose loggit log -p
Log with diffgit log <file>
File historygit blame <file>
Line authorsgit reflog
Ref log (recover)git fsck --lost-found
Find lost commits
Merging & Rebasing
git rebase main
Rebase onto maingit cherry-pick <commit>
Apply commitgit revert head
Undo last commit
Tags
git tag <tag> <commit>
Tag commitgit push origin <tag>
Push tag
Diff & Fetch
git diff-tree --no-commit-id --name-only
Changed filesgit fetch --all
Fetch all remotes