Common mistakes and practice
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Showing steps for Windows. They are marked “On Windows” below.
Most branch mistakes are a label in the wrong place: a commit on the wrong branch, a merge in the wrong direction, or HEAD on a commit instead of a branch. Because branches are only labels, each one has a short, safe fix.
Mistakes and fixes at a glance
The mistake beginners fear most: you forgot to create a branch and committed straight on main. If nothing is pushed and git status is clean, label the commit with a new branch, then move main back.
Example 1 · Move a commit from main to a new branch
Runs in: Your terminal
PS C:\Users\Priya\price-list> git statusPS C:\Users\Priya\price-list> git branch add-staplerPS C:\Users\Priya\price-list> git reset --hard HEAD~1PS C:\Users\Priya\price-list> git log --oneline --all --graphOutputOn branch main nothing to commit, working tree clean HEAD is now at d504b31 Add README * 0a3b19d (add-stapler) Add stapler to the price list * d504b31 (HEAD -> main) Add README * 9d23448 Add price list
What this code does
- The status is clean, so
reset --hardcannot wipe any uncommitted work here. Never use it with unsaved changes. git branch add-staplerputs a label on the stapler commit first; thengit reset --hard HEAD~1movesmainback one commit.- The stapler commit now lives only on
add-stapler.git switch add-staplercontinues the work there.
Review: Undoing changes safely (opens in a new tab) for git reset. The other common mistakes, each with the message and the fix:
Your local changes ... would be overwritten by checkout→ commit orgit restore, then switch. Review: Branches (opens in a new tab)fatal: invalid referenceora branch named '...' already exists→ check the name withgit branch; drop-cfor an existing branch. Review: Branches (opens in a new tab)- An editor opens during
git mergeand you are stuck → accept the message (Windows: close Notepad or the VS Code tab; Mac vim: Esc,:wq, Return). Review: Merging branches (opens in a new tab) error: the branch '...' is not fully merged→ merge it first, or-Donly to throw it away. Review: Merging branches (opens in a new tab)illegal start of expressionat<<<<<<<→ markers left in the file; delete them. Review: Merge conflicts (opens in a new tab)Committing is not possible because you have unmerged files→git addthe resolved files. Review: Merge conflicts (opens in a new tab)- The merge worked but the program is broken → Git only joins text; print the file after every merge. Review: Merging branches (opens in a new tab)
Detached HEAD
Detached HEAD is the state where HEAD points straight at a commit instead of at a branch, for example after git checkout <commit-id>. You can look around safely, but new commits there belong to no branch.
Priya wanted to see the very first price list, so she went to its commit. Git calls that detached HEAD. It is safe for looking; just switch back to a branch before real work.
Example 2 · Into detached HEAD and back
Runs in: Your terminal
PS C:\Users\Priya\price-list> git switch 9d23448PS C:\Users\Priya\price-list> git switch --detach 9d23448PS C:\Users\Priya\price-list> git statusPS C:\Users\Priya\price-list> git switch mainOutputfatal: a branch is expected, got commit '9d23448' hint: If you want to detach HEAD at the commit, try again with the --detach option. HEAD is now at 9d23448 Add price list HEAD detached at 9d23448 nothing to commit, working tree clean Previous HEAD position was 9d23448 Add price list Switched to branch 'main'
What this code does
git switchwants a branch, so it refuses a commit ID. With--detachit goes there;git checkout 9d23448does the same and prints a long note about "detached HEAD".git statussaysHEAD detached at 9d23448: the files show the first price list, but you are on no branch.git switch mainreturns to the latest work. Had Priya committed here,git switch -c <name>before leaving would have kept those commits on a new branch.
Merged in the wrong direction
Priya wanted add-ruler in main, but she was still on add-ruler and ran git merge main. That brought main into add-ruler (commit Merge branch 'main' into add-ruler); main itself did not change. Nothing is broken: switch to main and merge the feature, now a fast-forward.
On Windows
In PowerShell:
PS C:\Users\Priya\price-list> git switch mainPS C:\Users\Priya\price-list> git merge add-rulerOutputSwitched to branch 'main' Updating 8a9843c..c20ba37 Fast-forward prices.txt | 1 + 1 file changed, 1 insertion(+)
- Rule of thumb: stand on the branch that should receive the work. Check the
*ingit branchbeforegit merge.
Mistakes during merges
Git stops most merge mistakes before anything changes. Read the first line of the message: it says what to do.
On Windows
Deleting the branch you are on fails. Here Priya is still on add-ruler; Git names the folder where that branch is in use.
PS C:\Users\Priya\price-list> git branch -d add-rulererror: cannot delete branch 'add-ruler' used by worktree at 'C:/Users/Priya/price-list'
- Fix:
git switch main, thengit branch -d add-rulerworks.
Example 3 · Merging with uncommitted changes
Runs in: Your terminal
PS C:\Users\Priya\price-list> git merge add-glue-stickPS C:\Users\Priya\price-list> git restore prices.txtPS C:\Users\Priya\price-list> git merge add-glue-stickOutputerror: Your local changes to the following files would be overwritten by merge: prices.txt Please commit your changes or stash them before you merge. Updating c20ba37..fb5c0ca Aborting Updating c20ba37..fb5c0ca Fast-forward prices.txt | 1 + 1 file changed, 1 insertion(+)
What this code does
- Priya had changed the notebook price in
prices.txtwithout committing. The merge would overwrite it, so Git aborted before changing anything. - The edit was a slip, so she threw it away with
git restore; to keep it, she would commit it first. Then the merge ran as a fast-forward.
Finish up
Mark complete & continue
Next: Put your project on GitHub