Git Foundations / Recover and Diagnose

Diagnose common Git errors

Lesson 30 of 33. Open to read. No account needed.

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:

  1. Read the whole message. The first line says what failed. Lines starting with hint: suggest what to do.
  2. 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.
  3. Name the cause in one sentence before running anything else.
  4. Choose the smallest fix that deals with that cause and keeps everyone's work.

Six Messages You Will Meet

Message containsUsual causeSafe fix
not a git repositoryYou are outside the project foldercd into the folder that contains .git
pathspec '...' did not match any filesA typo, or the file is in another folderCheck the path with git status or ls, then repeat
Your local changes to the following files would be overwrittenUncommitted edits to a file the command must changeCommit, stash or discard the edits first
HEAD detached at or detached fromYou are on a commit, not a branchgit 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 pullSomeone else pushed firstPull with --rebase or --no-rebase, then push; never force a shared branch
untracked working tree files would be overwrittenAn untracked file has the same name as a file Git must createMove 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.

  1. Drill 1. Create the repository, then run git status one 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 status
    

    The message starts fatal: not a git repository. Cause: the repository is in cafe, and you are in its parent folder. Fix:

    ls
    cd cafe
    git status
    

    git status now reports On branch main.

  2. 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.md
    

    Git says fatal: pathspec 'guide.md' did not match any files. Cause: no file called guide.md is in this folder. git status lists the untracked docs/ folder, which is where it is. Fix:

    git status
    git add docs/guide.md
    git commit -m "Add guide"
    
  3. 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 update
    

    Git refuses: Your local changes to the following files would be overwritten by checkout. Cause: notes.md has an uncommitted edit, and update has a different version of that file. The snack reminder is wanted, so commit it on main, 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 seeLikely causeNext step
A different message from the one describedA setup command failed or was skippedRemove that drill's folder and run its setup again
not a git repository inside a drill folderYou are one level too high or too lowpwd, then cd to the drill folder
The setup commands fail in drill 5The one clone was created somewhere elseStart drill 5 again from its first command
  1. 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.

  1. fatal: pathspec 'report.md' did not match any files
  2. ! [rejected] main -> main (fetch first)
  3. error: The following untracked working tree files would be overwritten by checkout
  4. HEAD 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

  1. No file called report.md exists where Git looked: a typo, or the file is in another folder. Check with git status or ls, then repeat with the right path.
  2. 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.
  3. An untracked file in your folder has the same name as a file the other branch contains. Move or rename your file, then switch.
  4. 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.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy