TetSharing

Git commands cheat sheet

Every example was run by our test suite in a fresh repository that already contains one commit (readme.txt containing "hello", message "Add readme", branch main). Commit hashes in your repository will differ. Commands chained with ; run one after another. For explanations, follow the Git track.

Setup commands run before each example
git init -q
printf "hello\n" > readme.txt
git add readme.txt
git commit -q -m "Add readme"

Setup and statusStaging and committingHistoryBranches and mergingUndoing and stashingRemotes

Setup and status

SyntaxWhat it doesExample → result
git config user.nameShow (or set) your name for commitsgit config user.name
→ Ethan Cole
git status --shortCompact status: ?? untracked, A added, M modifiedecho x > new.txt; git status --short
→ ?? new.txt
git ls-filesFiles Git is trackinggit ls-files
→ readme.txt
git check-ignore pathIs this path ignored by .gitignore?echo "*.log" > .gitignore; git check-ignore app.log
→ app.log

Staging and committing

SyntaxWhat it doesExample → result
git add .Stage every change below this folderecho x > b.txt; git add .; git status --short
→ A b.txt
git commit -am "msg"Stage tracked files and commitecho more >> readme.txt; git commit -qam "Update"; git rev-list --count HEAD
→ 2
git mv old newRename a tracked filegit mv readme.txt README.md; git status --short
→ R readme.txt -> README.md
git diff --shortstatOne-line summary of unstaged changesecho more >> readme.txt; git diff --shortstat
→ 1 file changed, 1 insertion(+)

History

SyntaxWhat it doesExample → result
git log --onelineOne line per commitgit log --oneline
→ 1a2b3c4 Add readme
git log -1 --format=%sSubject of the latest commitgit log -1 --format=%s
→ Add readme
git log -1 --format=%anAuthor of the latest commitgit log -1 --format=%an
→ Ethan Cole
git rev-list --count HEADNumber of commitsgit rev-list --count HEAD
→ 1
git cat-file -t refType of a Git objectgit cat-file -t HEAD
→ commit

Branches and merging

SyntaxWhat it doesExample → result
git branch --show-currentName of the current branchgit branch --show-current
→ main
git switch -c nameCreate a branch and switch to itgit switch -c feature
→ Switched to a new branch 'feature'
git branchList branches (* = current)git branch
→ * main
git merge nameMerge a branch into the current onegit switch -q -c f; echo x > f.txt; git add f.txt; git commit -qm "Add f"; git switch -q main; git merge -q f; git log -1 --format=%s
→ Add f
git tag nameMark the current commitgit tag v1.0; git tag
→ v1.0

Undoing and stashing

SyntaxWhat it doesExample → result
git restore fileDiscard uncommitted edits to a fileecho oops > readme.txt; git restore readme.txt; cat readme.txt
→ hello
git restore --staged fileUnstage, keep the editecho x > b.txt; git add b.txt; git restore --staged b.txt; git status --short
→ ?? b.txt
git reset --soft HEAD~1Undo the last commit, keep it stagedecho b >> readme.txt; git commit -qam "Two"; git reset -q --soft HEAD~1; git status --short
→ M readme.txt
git revert --no-edit refNew commit that reverses refecho b >> readme.txt; git commit -qam "Two"; git revert --no-edit HEAD >/dev/null; git log -1 --format=%s
→ Revert "Two"
git stashSet uncommitted work asideecho wip >> readme.txt; git stash -q; git stash list
→ stash@{0}: WIP on main: 1a2b3c4 Add readme

Remotes

SyntaxWhat it doesExample → result
git remote add name urlConnect a remotegit remote add origin https://example.com/demo.git; git remote
→ origin
git remote get-url nameAddress of a remotegit remote add origin https://example.com/demo.git; git remote get-url origin
→ https://example.com/demo.git

Official docs

Compiled by Ethan Cole (pen name) with AI-assisted drafting; every example in the table is executed by our test suite. Related lessons: staging and commits, branches, undoing changes.