Git Commands Cheat Sheet
Essential version control recipes for undoing commits, branching, stashing, and syncing.
What is Git Version Control?
Git is a distributed version control system designed for tracking changes in source code across collaborative software development. Commands manipulate files across three core trees: the Working Directory, the Staging Area (Index), and the Git Repository (HEAD). It allows developers to branch, merge, and revert commits without centralized network bottlenecks.
Undo last commit (keep changes staged)
Rewinds the HEAD pointer by 1 commit. All changed files remain staged in the index ready to re-commit.
git reset --soft HEAD~1Undo last commit (keep changes unstaged)
Undoes the last commit and unstages files, keeping all edits in your local working directory.
git reset HEAD~1Discard all uncommitted local changes
Completely wipes all uncommitted modifications in tracked files to match the latest commit.
git reset --hard HEADUnstage a specific file
Removes a file from the staging area without discarding its contents.
git restore --staged <file>Discard edits to a specific file
Reverts unstaged changes in a file back to the version in the current commit.
git restore <file>Amend last commit message or staged files
Updates the most recent commit with newly staged changes or a revised message without creating a new commit.
git commit --amend -m 'New message'Safely revert a pushed commit
Creates a new inverse commit that undoes the target commit's changes. Safe for shared remote branches.
git revert <commit-hash>Delete untracked files and directories
Recursively deletes untracked files (-f) and directories (-d) from the working tree.
git clean -fdCreate and switch to a new branch
Modern replacement for 'git checkout -b'. Creates the branch and checks it out immediately.
git switch -c <branch-name>Delete a merged local branch
Deletes a local branch if it has already been merged into the upstream branch.
git branch -d <branch-name>Force delete an unmerged local branch
Forces deletion of a branch even if it contains unmerged commits.
git branch -D <branch-name>Delete a remote branch on GitHub/GitLab
Removes the branch reference from the remote server.
git push origin --delete <branch-name>Rename current local branch
Renames the branch you are currently on.
git branch -m <new-name>Stash changes including untracked files
Shelves modified tracked files and new untracked files (-u) with a descriptive message.
git stash -u -m 'wip: feature description'Restore and remove latest stash
Reapplies changes from stash@{0} to your working tree and deletes it from the stash stack.
git stash popList all saved stashes
Displays all stashed working directory states with their index and message.
git stash listPull remote changes with rebase
Fetches upstream commits and plays local commits on top of them, avoiding cluttering merge commits.
git pull --rebase origin mainPrune stale remote-tracking branch references
Cleans up local tracking references for branches that have been deleted on the remote server.
git fetch --pruneApply a specific commit from another branch
Copies the changes introduced by a specific commit into your current active branch.
git cherry-pick <commit-hash>Pretty one-line commit history graph
Displays a compact, colorized visual branch tree of all commits across all branches.
git log --oneline --graph --decorate --allView staged changes ready to commit
Shows line-by-line differences between files in the staging area and the latest commit.
git diff --stagedFrequently Asked Questions
How do I undo the last Git commit without losing my work?+
Run 'git reset --soft HEAD~1'. This undoes the commit but leaves all modified files staged in your working index, allowing you to edit files or re-commit with an updated message.
What is the difference between 'git reset --soft' and 'git reset --hard'?+
'git reset --soft' moves the HEAD pointer backward while keeping all file modifications staged. 'git reset --hard' moves HEAD backward and discards all uncommitted modifications in tracked files permanently.
How do I delete both a local branch and its remote tracking branch?+
To delete the local branch, run 'git branch -d <branch-name>' (or -D to force). To delete the remote copy on GitHub or GitLab, run 'git push origin --delete <branch-name>'.
When should I use 'git revert' instead of 'git reset'?+
Use 'git revert' if the commit has already been pushed to a shared remote repository, as it creates a new safe commit that inverts the changes without rewriting history. Use 'git reset' only on private local branches.
Need to review code differences before committing?
Use our client-side Diff Checker to generate unified git patch files and line-by-line comparisons.