Match the exact Git error text first, then follow the branch for that message. For “detached HEAD,” switch back to a branch or create one. For “not a git repository,” move into the repo root or run git init only if this folder is meant to become one. Ignoring the wrong branch can drop commits, and ignoring a bad repo path makes every Git command fail. This guide maps common terminal messages to the safest fix, recovery path, or stop point.

Start with the exact Git error message

The safest first move is to read the full terminal message and run git status before changing anything. That separates a local folder mistake, a detached HEAD state, and a remote push problem, which often need different fixes. If the message mentions “not a git repository,” “detached HEAD,” “merge conflict,” or “non-fast-forward,” follow that path first.

Use git status before changing anything

git status is the main diagnostic command. It tells you whether you are on a branch, in detached HEAD, or holding untracked or modified files. It also confirms the current branch name when one exists, so it is the fastest way to see whether the problem is local repository state or something farther away on the remote.

Separate local state from remote problems

Local repository state errors happen in your working folder. Remote problems appear during push, fetch, or pull, and often mention upstream branches, non-fast-forward rejection, or authentication. A local fix will not repair a rejected push, and a remote fix will not help if the terminal is sitting outside the repository root.

Suppose the prompt shows fatal: not a git repository after a cd src/app command. The folder may be one level too deep, or it may sit outside the clone entirely. In that case, confirm location with pwd and move to the folder that actually contains the .git directory.

Git error decision matrix

Use this table as the first branch in the decision tree. It maps the error text to the most likely cause, the safest first command, whether work may be at risk, and the recovery path if the first fix does not solve it.

Error text Likely cause Safest first command Work at risk? Recovery if first fix fails
detached HEAD Checked out a commit or tag instead of a branch git status Only if you made commits while detached git switch <branch> or git switch -c <new-branch>; use git reflog if the move was bad
fatal: not a git repository Wrong folder or missing .git metadata pwd No, unless you were expecting uncommitted work in another folder Move to the repo root, re-clone, or run git init only for a new repository
non-fast-forward Remote branch moved ahead of your local branch git status Possible local divergence Fetch, then rebase or merge before pushing again; force push only with clear intent
merge conflict Changes overlap in the same lines or files git status Yes, until conflicts are resolved and committed Edit conflict markers, stage files, then continue the merge or rebase
no upstream branch Local branch has no remote-tracking branch set git branch -vv No immediate loss, but pushes can confuse branch tracking Set upstream with git push -u origin <branch>
authentication failed Bad credentials, expired token, or missing access git remote -v No repository data risk Refresh credentials or permission access, then retry the remote command
pathspec did not match any file Branch or path name is wrong git branch No, unless you continue on the wrong target List valid branches or paths, then rerun with the exact name
bad object or missing object Corrupted or incomplete repository metadata git fsck Possibly yes Use git reflog for recent recovery; re-clone if metadata is broken

How do you fix detached HEAD?

Detached HEAD means Git checked out a commit, tag, or other non-branch reference instead of a named branch. The safest fix is to return to an existing branch with git switch. If you made local commits while detached, create a branch first so those commits stay reachable.

Return to an existing branch

  1. Check the current state. Run git status to confirm that HEAD is detached. The output usually names the commit rather than a branch.
  2. List available branches. Run git branch to see valid local branch names. Pick the branch you want to rejoin, such as main or develop.
  3. Switch back safely. Run git switch <branch>. This returns HEAD to that branch without rewriting history.

Create a branch before leaving detached HEAD

If you have commits that exist only in detached HEAD, create a branch from the current commit before switching away. Use git switch -c rescue-work or a branch name that fits the task. That makes the current commit reachable from a named branch, which protects the work from being left behind.

A common first-timer mistake is checking out an older commit to inspect one file, then making two or three small commits without noticing the prompt changed. If the branch is left without a name, those commits can become hard to find later. Creating a branch at that point keeps the history attached.

Use reflog if the move went wrong

git reflog records recent HEAD moves, including bad checkouts and resets. If the branch switch went wrong or the wrong commit was selected, reflog can point back to the previous branch position. It is the usual last-resort recovery path before deeper repair.

What does not a git repository mean?

A Git branch diagram labels Master and HEAD
Confirm your location and look for the .git folder in the current folder.

fatal: not a git repository usually means the terminal is in the wrong folder or the repository metadata is missing. Git looks for a .git directory in the current folder or its parents. If it cannot find that metadata, Git commands cannot identify a repository root.

Check the current folder

  1. Confirm your location. Run pwd to print the current path. On Windows, use cd with no arguments if that is the shell available.
  2. Inspect the contents. Run ls or dir and look for the .git folder. If it is not present, you are likely outside the repository root.
  3. Move to the repo root. Change into the folder that contains .git, then run git status again. That confirms Git can see the repository.

Initialize or re-clone only when needed

If the folder is meant to be a new repository, git init creates the .git directory. If the project should already exist but the metadata is missing or corrupted, re-cloning is usually safer than trying to rebuild it by hand. Re-clone when the working tree is disposable or when the repo data is absent.

Picture a project copied from a zip file into Desktop/app. The source files are there, but the .git folder is not. Git will still say “not a git repository” until the folder is initialized or re-cloned, because the commands have no repository database to read.

How do branch, switch, and checkout fit together?

git branch lists and creates branches, git switch moves between branches, and git checkout is the older command that can both switch branches and check out commits. For normal branch movement, git switch is clearer. Use git checkout mainly when older docs or older terminals still expect it.

List and create branches

  1. List local branches. Run git branch to see what branches exist. The starred branch is the one currently checked out.
  2. Create a new branch. If you are in detached HEAD and want to keep the current commit, run git switch -c new-branch. That creates the branch and moves to it in one command.
  3. Return to a named branch. Use git switch main or another valid branch name when you are ready to leave the current state. This avoids the commit-by-commit behavior that can happen with older checkout usage.

When git checkout still matters

git checkout still appears in older tutorials and many terminal autocomplete setups. It can still switch branches, but it also supports file and commit checkout in a way that makes beginners more likely to land in detached HEAD. Newer guidance should prefer git switch for branches and git restore for files, when available.

Git troubleshooting matrix

This matrix ties the most common messages to the safest first move. It is meant to stop the usual mistake of treating every Git error the same way. Start with the exact text, then use the command that matches the class of problem.

Problem class First command What to look for Next safe action
Detached HEAD git status Branch name is missing and commit ID is shown git switch <branch> or git switch -c <new-branch>
Missing repository root pwd Terminal is in the wrong folder Change into the directory that contains .git
Missing .git metadata ls .git is absent Re-clone or run git init for a new repo
Remote branch confusion git branch -vv No upstream shown for the current branch Set upstream with git push -u origin <branch>
Merge conflict git status Files listed as unmerged Open files, remove conflict markers, stage, and continue
Non-fast-forward push git fetch Remote branch moved ahead Rebase or merge, then push again
Auth or permission failure git remote -v Remote URL or access problem Refresh credentials or fix repository access
Lost commit after bad move git reflog Previous HEAD positions are listed Switch back to the commit or branch shown there

What should I do when Git says merge conflict or non-fast-forward?

For a merge conflict, keep the work in place and resolve the conflict markers in the affected files before finishing the merge or rebase. For a non-fast-forward push rejection, the remote branch likely has commits that the local branch does not have yet. Pull, fetch, and reconcile first; a force push is a separate decision, not the first fix.

  1. Inspect the status. Run git status and look for unmerged files or push rejection context. This tells you whether the problem is local conflict resolution or a branch that moved ahead on the remote.
  2. Resolve file conflicts. Open each conflicted file and remove the conflict markers. Save the file, stage it, and continue the merge or rebase. Conflicts stay unresolved until Git sees a clean file and a follow-up commit.
  3. Reconcile before pushing. If the push failed because the remote branch advanced, fetch first, then rebase or merge as your workflow requires. That keeps history aligned and lowers the chance of overwriting someone else’s work.

How do I fix permission or authentication errors in Git?

Authentication and permission errors are separate from repository-state problems. They usually mean the remote server rejected access, not that the local repository is damaged. Check the remote URL, the credential helper, and whether the account still has permission to read or push to that repository.

Check access before retrying

  1. Review the remote URL. Run git remote -v to see where Git is trying to connect. A wrong host or old repository path can cause repeated failures.
  2. Refresh credentials. Sign in again if the host uses tokens or SSH keys. An expired token or changed key often causes the same error on every push or fetch.
  3. Retry the remote command. Once access is fixed, repeat the original pull, fetch, or push. If the same message returns, the issue is likely still permission-related rather than branch-state related.

Frequently asked questions

How do I fix detached HEAD in Git?

Run git status first, then switch back with git switch <branch>. If you made commits while detached, create a branch first with git switch -c <new-branch> so those commits stay reachable. Use git reflog if the branch move went wrong.

How do I get back to my branch after checking out a commit?

List branches with git branch, then run git switch <branch> to return to the named branch. If you need to keep work made after the commit checkout, create a branch from the detached commit first. That prevents local commits from becoming hard to find.

Why does Git say fatal: not a git repository?

Git says that when it cannot find repository metadata in the current folder or its parents. The usual causes are being in the wrong directory, opening a subfolder outside the clone, or working in a folder that was copied without its .git directory. Confirm location before anything else.

How do I recover from accidentally entering detached HEAD?

If no commits were made, use git switch <branch> and move on. If commits were made, create a branch first with git switch -c <name>, then decide whether to merge or keep that line of history. If the wrong checkout changed your path, git reflog can point back.

What Git command should I run to get back to a clean state?

Start with git status to see what is dirty. If you only need to stop editing and return to a branch, use git switch <branch>. For unmerged files, finish the merge or rebase instead of forcing a reset. Clean state depends on the exact error.