Read Before You Fix
When a Git command fails, it is tempting to reach for something drastic:
git reset --hard, a forced push, or deleting the folder and cloning again.
Those can destroy work, and they are almost never needed. Git's messages
usually say exactly what is wrong and often suggest the fix.
Use the same routine every time:
- Read the whole message. The first line says what failed. Lines starting
with
hint:suggest what to do. - Run
git status. It shows which branch you are on, whether a merge or rebase is in progress, and what is staged, changed or untracked. - Name the cause in one sentence before running anything else.
- Choose the smallest fix that deals with that cause and keeps everyone's work.
Six Messages You Will Meet
| Message contains | Usual cause | Safe fix |
|---|---|---|
not a git repository | You are outside the project folder | cd into the folder that contains .git |
pathspec '...' did not match any files | A typo, or the file is in another folder | Check the path with git status or ls, then repeat |
Your local changes to the following files would be overwritten | Uncommitted edits to a file the command must change | Commit, stash or discard the edits first |
HEAD detached at or detached from | You are on a commit, not a branch | git switch -c <name> to keep new commits, or switch to a branch |
[rejected] on push, or Need to specify how to reconcile divergent branches on pull | Someone else pushed first | Pull with --rebase or --no-rebase, then push; never force a shared branch |
untracked working tree files would be overwritten | An untracked file has the same name as a file Git must create | Move or rename your file, then repeat |
Two of these protect your work on purpose. Git refuses to overwrite uncommitted edits and untracked files, because it cannot get them back once they are gone. The refusal is the safe outcome; the fix is to put your file somewhere safe first.
Exercise: Diagnose and Fix Six Failures
Each drill creates a small repository with one problem. For every drill: run the failing command, read the message, write down the cause, then fix it. The first three include the fix; the last three are your independent task.
Starting state: Git configured as in the configuration lesson.
-
Drill 1. Create the repository, then run
git statusone folder too high:mkdir -p ~/git-practice/diagnosis-drills/drill-1/cafe cd ~/git-practice/diagnosis-drills/drill-1/cafe git init -b main cd .. git statusThe message starts
fatal: not a git repository. Cause: the repository is incafe, and you are in its parent folder. Fix:ls cd cafe git statusgit statusnow reportsOn branch main. -
Drill 2. A guide lives in a subfolder:
mkdir -p ~/git-practice/diagnosis-drills/drill-2 cd ~/git-practice/diagnosis-drills/drill-2 git init -b main mkdir docs cat > docs/guide.md <<'EOF' # Guide EOF git add guide.mdGit says
fatal: pathspec 'guide.md' did not match any files. Cause: no file calledguide.mdis in this folder.git statuslists the untrackeddocs/folder, which is where it is. Fix:git status git add docs/guide.md git commit -m "Add guide" -
Drill 3. An uncommitted edit blocks a switch:
mkdir -p ~/git-practice/diagnosis-drills/drill-3 cd ~/git-practice/diagnosis-drills/drill-3 git init -b main cat > notes.md <<'EOF' Meeting at 10. EOF git add notes.md git commit -m "Add notes" git switch -c update cat > notes.md <<'EOF' Meeting at 11. EOF git commit -am "Move the meeting" git switch main cat > notes.md <<'EOF' Meeting at 10. Bring snacks. EOF git switch updateGit refuses:
Your local changes to the following files would be overwritten by checkout. Cause:notes.mdhas an uncommitted edit, andupdatehas a different version of that file. The snack reminder is wanted, so commit it onmain, then switch:git diff git commit -am "Remind everyone about snacks" git switch update
Success: for each drill you wrote the cause before fixing it, and each
repository ended in the intended state: a working git status in cafe, the
guide committed, and the snack reminder committed on main with update
checked out.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| A different message from the one described | A setup command failed or was skipped | Remove that drill's folder and run its setup again |
not a git repository inside a drill folder | You are one level too high or too low | pwd, then cd to the drill folder |
| The setup commands fail in drill 5 | The one clone was created somewhere else | Start drill 5 again from its first command |
-
Clean up when you have finished the independent task:
cd ~/git-practice ls rm -rf diagnosis-drills ls
Independent task: run each setup, then the failing command it ends with. Write down the cause and the safe fix before you fix anything.
Drill 4. Ends on a detached HEAD with a new commit that must be kept:
mkdir -p ~/git-practice/diagnosis-drills/drill-4
cd ~/git-practice/diagnosis-drills/drill-4
git init -b main
cat > menu.md <<'EOF'
Soup of the dya
EOF
git add menu.md
git commit -m "Add menu"
cat >> menu.md <<'EOF'
Bread
EOF
git commit -am "Add bread"
git switch --detach HEAD~1
cat > menu.md <<'EOF'
Soup of the day
EOF
git commit -am "Fix typo in the menu"
git status
Drill 5. Ends with a rejected push from the clone one:
mkdir -p ~/git-practice/diagnosis-drills/drill-5
cd ~/git-practice/diagnosis-drills/drill-5
git init --bare -b main shared.git
git clone shared.git one
cd one
cat > plan.md <<'EOF'
# Plan
EOF
git add plan.md
git commit -m "Start the plan"
git push -u origin main
cd ..
git clone shared.git two
cd two
cat >> plan.md <<'EOF'
Tuesday: design
EOF
git commit -am "Plan Tuesday"
git push
cd ../one
cat > wednesday.md <<'EOF'
Wednesday: build
EOF
git add wednesday.md
git commit -m "Plan Wednesday"
git push
Drill 6. Ends with a refused switch to the prices branch:
mkdir -p ~/git-practice/diagnosis-drills/drill-6
cd ~/git-practice/diagnosis-drills/drill-6
git init -b main
cat > index.md <<'EOF'
# Bakery
EOF
git add index.md
git commit -m "Add home page"
git switch -c prices
cat > prices.md <<'EOF'
Bread: 3.00
EOF
git add prices.md
git commit -m "Add price list"
git switch main
cat > prices.md <<'EOF'
Draft: bread 2.80?
EOF
git switch prices
In drill 6, keep your draft: it holds a price you are still deciding on.
Check Your Understanding
Match each message to its cause and a safe fix.
fatal: pathspec 'report.md' did not match any files! [rejected] main -> main (fetch first)error: The following untracked working tree files would be overwritten by checkoutHEAD detached from 4c1e2a0
Answer and Reasoning
Independent task
Drill 4. git status says HEAD detached from and the commit you started
from: you are on a commit, not a branch, and you have committed since. If you
switched away now, the typo fix would be left behind. Keep it on a branch:
git switch -c typo-fix
You could then merge typo-fix into main as in the branching module.
Drill 5. The push is [rejected] because two pushed "Plan Tuesday"
first. A plain git pull then stops with
Need to specify how to reconcile divergent branches. "Plan Wednesday" has
not been shared, so rebasing it is safe:
git pull --rebase
git push
git log --oneline origin/main shows "Plan Wednesday", "Plan Tuesday" and
"Start the plan". No force was needed.
Drill 6. untracked working tree files would be overwritten by checkout:
your untracked draft prices.md has the same name as the file on prices.
Move the draft aside, then switch:
mv prices.md prices-draft.md
git switch prices
You are on prices, prices.md holds the committed price list, and your
draft is safe in prices-draft.md.
Understanding check
- No file called
report.mdexists where Git looked: a typo, or the file is in another folder. Check withgit statusorls, then repeat with the right path. - The remote has commits you have not fetched, usually because a teammate pushed. Pull, resolve any conflict, then push again; never force a shared branch.
- An untracked file in your folder has the same name as a file the other branch contains. Move or rename your file, then switch.
- You are on a detached HEAD and have committed since detaching. Create a
branch with
git switch -c <name>to keep those commits before switching anywhere else.