Choose interactively which parts of your changes to stage.
git commit -m "message"
Record the staged changes as a new commit.
git commit --amend
Replace the last commit with a new message or extra changes.
Discards work or rewrites history
git diff
Show changes that are not staged yet.
git diff --staged
Show changes that are staged for the next commit.
git branch
List local branches; the current one is marked with *.
git switch -c <branch>
Create a new branch and switch to it.
git switch <branch>
Switch to an existing branch.
git merge <branch>
Merge another branch into the current branch.
git rebase <branch>
Replay your commits on top of another branch.
Discards work or rewrites history
git branch -d <branch>
Delete a local branch that has been merged.
git remote add origin <url>
Connect a remote repository under the name origin.
git fetch --prune
Download remote changes and remove references to branches deleted on the remote.
git pull
Fetch remote changes and integrate them into the current branch.
git push
Upload your local commits to the remote branch.
git push -u origin <branch>
Push a new branch and link it to the remote branch.
git restore <file>
Discard unstaged changes in a file.
Discards work or rewrites history
git restore --staged <file>
Unstage a file but keep its changes.
git revert <commit>
Create a new commit that undoes an earlier commit.
git reset --soft HEAD~1
Undo the last commit but keep its changes staged.
Discards work or rewrites history
git reset --hard <commit>
Move the branch to a commit and discard all local changes.
Discards work or rewrites history
git log --oneline --graph
Show compact history with a branch graph.
git show <commit>
Show the message and changes of a commit.
git blame <file>
Show who last changed each line of a file.
git reflog
List recent HEAD positions to recover lost commits.
git stash
Save uncommitted changes and clean the working tree.
git stash pop
Re-apply the latest stash and remove it from the list.
git stash list
List all saved stashes.
The Everyday Git Workflow
Most work follows the same loop: check status, stage changes with git add, record them with git commit, and share them with git push. Branches keep unfinished work separate from the main line.
Commands marked with a warning discard changes or rewrite history. Use them carefully, especially on branches that other people have already pulled.
Undoing Mistakes Safely
Git rarely loses committed work for good.
Use git revert to undo a commit that is already pushed.
Use git restore --staged to unstage without losing edits.
git reflog lists recent HEAD positions, so you can recover commits after a reset.
Frequently Asked Questions
What is the difference between git switch and git checkout?+
git switch (added in Git 2.23) only changes branches, while git checkout also restores files. The newer switch and restore commands split those jobs to make mistakes less likely.
Should I merge or rebase?+
Merge keeps the exact history and is safest on shared branches. Rebase creates a cleaner, linear history but rewrites commits, so only rebase branches nobody else uses.
What does git fetch --prune do?+
It downloads new commits from the remote and removes local references to branches that were deleted on the remote.