Git guides
Quick answers to the Git questions everyone runs into. Each guide gives you the commands, then lets you practice them in an interactive sandbox.
- How to undo the last commit in Git
Undo your last Git commit and keep your changes with git reset --soft HEAD~1, or undo a commit you already pushed with git revert.
- How to undo a pushed commit with git revert
Safely undo a commit that is already on GitHub: git revert creates a new commit that does the opposite, so your teammates' history stays intact.
- git reset --soft vs --mixed vs --hard
The difference between git reset --soft, --mixed and --hard, explained with examples: what happens to your branch, your staging area and your files.
- How to recover a lost commit with git reflog
Lost a commit after git reset --hard or a bad rebase? git reflog lists every place HEAD has been, so you can find the commit and get it back.
- How to change the last commit message (git commit --amend)
Fix a typo in your last commit message, or add a forgotten change to it, with git commit --amend.
- How to resolve a merge conflict in Git
Step by step: what the <<<<<<<, ======= and >>>>>>> conflict markers mean, how to pick the right version, and how to finish the merge with git add and git commit.
- Fix "Updates were rejected because the remote contains work that you do not have locally"
Why git push is rejected with "fetch first" or "non-fast-forward", and how to fix it with git pull, then push again.
- Fix "fatal: The current branch has no upstream branch"
Your new branch isn't on GitHub yet. Push it once with git push -u origin <branch>; after that, plain git push and git pull work.
- Fix "Your local changes would be overwritten by checkout"
Git refuses to switch branches because you have uncommitted changes. Commit them, or put them aside with git stash and bring them back later.
- git stash: save unfinished work for later
How to use git stash, git stash list and git stash pop to put unfinished changes aside, switch tasks, and bring them back.
- git fetch vs git pull: what's the difference?
git fetch downloads new commits without touching your files; git pull fetches and then merges them into your branch.
- What is a fast-forward merge in Git?
A fast-forward merge just moves your branch forward to the other branch's commit, without creating a merge commit. Here is when it happens and why.
- How to create and switch branches in Git
Create a branch and switch to it with git switch -c, list branches with git branch, and switch back with git switch.
- How to delete a Git branch (local and remote)
Delete a merged local branch with git branch -d, force-delete with -D, and delete the branch on GitHub with git push origin --delete.
- What is a pull request? How to open your first one
A pull request asks your team to review a branch and merge it into main. How to push a branch, open the pull request, and update main after it's merged.
- What is the staging area in Git? (git add explained)
Git saves changes in three steps: your files, the staging area, and commits. How git add, git status and git commit move changes between them.
- How to discard local changes or unstage a file (git restore)
Throw away uncommitted changes to a file with git restore, or take it out of the staging area with git restore --staged.
- git clone: how to download a repository
git clone downloads a project and its entire history from GitHub, and sets up origin so you can pull and push.
- git status and git diff explained
git status tells you which files changed and what's staged; git diff shows the exact lines that changed.