DEV Community

rcblake
rcblake

Posted on

Getting the Git

Git has been developed back in 2005 and today is the most commonly used VCS that is used by almost all development teams that require to version and archive their source code. In today’s article, we are going to cover a few of the most common Git errors and discuss how you can potentially avoid or fix them.

fatal: not a git repository (or any of the parent directories)

This is one of the most common errors, especially for newcomers to Git. Let’s suppose that you want to run git status in a particular directory:

$ git status
fatal: not a git repository (or any of the parent directories): .git

The error indicates that the command cannot be executed because the current directory is not a Git directory. Usually, there are two main reasons why you might be getting the error.

  • You either forgot to initialize the repository as a Git repository using git init
  • or you are probably in the wrong directory. You can check your current work directory using pwd. It’s quite common for people to forget to change the directory when cloning a Git repository.

fatal: refusing to merge unrelated histories
Another very common error after git pull or git merge is the one shown below:

fatal: refusing to merge unrelated histories

This usually can happen mainly for two reasons

  • The .git directory got corrupted. This hidden directory contains information relevant to your project that is being version controlled (for example, information about commits, remote repositories, etc.). If the directory is corrupted, git is unaware of the local history and thus the error will be reported when trying to push or pull to/from a remote repository.

  • You are trying to pull from a remote repository that already has commits while you have also created a new repository locally and added commits as well. In this case, the error will be reported since Git is unaware of how these two are related (they are pretty much interpreted as two distinct projects).

If this is the case, then you need to provide the --allow-unrelated-histories flag. For example,

$ git pull origin master --allow-unrelated-histories

There’s also a chance of getting this error when trying to rebase and requires different treatment.

$ git rebase origin/QA
fatal: refusing to merge unrelated histories
Error redoing merge game_text_edit_pt2_pt1_final_final_final

The process of combining commits to a new base commit is called rebasing. In other words, rebasing simply changes the base of the branch to a different commit so that it appears as if you have created the branch from that particular commit.

When git rebase fails with this error rebase does not get aborted but is still in progress. Instead, you can actually intervene manually.

$ git status
interactive rebase in progress; onto game_text_edit
Last command done (1 command done):
pick pt2_pt1_final_final_final test merge commit

To solve the issue you first need to merge and commit, and finally force rebase to continue:

$ git merge --allow-unrelated ORIGINAL_BRANCH_THAT_WAS_MERGED --no-commit
$ git commit -C ORIGINAL_MERGE_COMMIT
$ git rebase --continue

fatal: Unable to create ‘.git/index.lock’: File exists

When a git process crashes in one of your Git repositories you may see the error shown below when you attempt to run a git command:

fatal: Unable to create '/path/to/.git/index.lock': File exists.

If no other git process is currently running, this probably means a git process crashed in this repository earlier.

If no other git process is running, you can quickly fix this by manually removing the file:

$ rm -f .git/index.lock

error: pathspec did not match any file(s) known to git

Usually, when you create a new branch using say the web UI of GitHub and go back to your local terminal to checkout to the branch using

$ git checkout mybranch

you may get this error

error: pathspec 'mybranch' did not match any file(s) known to git.

This usually means that your local repository is not up to date with the latest information for the remote Git repository. To fix the issue you simply need to fetch the information for the newly created branch:

$ git fetch
Now git checkout should work fine.

This is not an exhaustive list but

Top comments (0)