Tools › Cheat sheets › Containers & DevOps › Git
🌿

Git cheat sheet

Commands for the daily Git workflow. Commands marked with a warning discard work and cannot always be undone, so check git status and consider git stash first.

Click any command to copy it.

Set up and get code

CommandWhat it does
git config --global user.name "Your Name"Set the name on your commits.
git config --global user.email "you@example.com"Set the email on your commits.
git initCreate a repository in the current folder.
git clone URLCopy a remote repository.
git clone --depth 1 URLClone only the latest commit (fast, small).

Stage and commit

CommandWhat it does
git statusShow changed, staged and untracked files.
git add -pStage changes interactively, hunk by hunk.
git add -AStage all changes, including deletions.
git commit -m "message"Commit the staged changes.
git commit --amendChange the last commit (only if you have not pushed it).
git diffShow unstaged changes.
git diff --stagedShow staged changes.

Branches and merging

CommandWhat it does
git branchList local branches.
git switch -c new-branchCreate and switch to a new branch.
git switch mainSwitch to an existing branch.
git merge branchMerge a branch into the current one.
git rebase mainReplay your branch's commits on top of main.
git branch -d branchDelete a merged branch.
git branch -D branchDestructive: Delete a branch even if it has unmerged commits.
git cherry-pick SHAApply one commit onto the current branch.

Remotes

CommandWhat it does
git remote -vList remotes and their URLs.
git fetchDownload remote changes without merging.
git pull --rebaseFetch and replay your local commits on top.
git push -u origin branchPush a branch and set its upstream.
git push --force-with-leaseDestructive: Force push, but refuse if someone else pushed in the meantime.
git push origin --delete branchDestructive: Delete a remote branch.

History and inspection

CommandWhat it does
git log --oneline --graph --decorate --allCompact graph of all branches.
git show SHAShow one commit's changes.
git blame fileShow who last changed each line.
git log -p fileHistory of changes to one file.
git reflogEvery position HEAD has been at; recovers 'lost' commits.
git bisect startBegin a binary search for the commit that introduced a bug.

Undo

CommandWhat it does
git restore fileDestructive: Discard unstaged changes to a file.
git restore --staged fileUnstage a file but keep the changes.
git reset --soft HEAD~1Undo the last commit and keep its changes staged.
git reset --hard HEAD~1Destructive: Undo the last commit and discard its changes.
git revert SHACreate a new commit that undoes an earlier one (safe for shared branches).
git clean -fdDestructive: Delete untracked files and folders. Preview with -n first.
git stashShelve uncommitted changes.
git stash popRestore the most recent stash.
git stash listList stashes.

Tags

CommandWhat it does
git tag -a v1.0 -m "Release 1.0"Create an annotated tag.
git push origin v1.0Push one tag.
git push --tagsPush all tags.
git tag -d v1.0Delete a local tag.

Frequently asked questions

How do I undo my last commit?

Use git reset --soft HEAD~1 to keep the changes staged, or git revert HEAD to add a new commit that reverses it if it is already pushed.

How do I recover a commit I lost?

Run git reflog to find its SHA, then git switch -c recovered SHA to bring it back on a branch.

More Containers & DevOps cheat sheets

🐳 Docker☸️ kubectl🅰️ Ansible🏗️ TerraformAll Containers & DevOps cheat sheets →
Copied to clipboard