Codementor Events

5 Errors Every Git Professional Can Fix

Published Nov 01, 2021

Cover Photo by Sigmund on Unsplash

Often, while developing applications, irrespective of the scope and size, we use version control systems like Git to track the changes we perform in the files of our codebase. It helps us by storing a record of all the changes that were done. If there is a necessity to revert to a specific version, we can always revert to that version with just a few git commands. With the help of online code hosting platforms like GitHub or GitLab, we can collaborate with other developers in our team, work together on a project and store all the changes made by the team without worrying about the loss of changes made by your teammates.

Source

Though Git is an amazing version control system, we often encounter various errors we make while trying to clone a repo or make a commit. Mainly, these errors are caused by some minor mistake we may have made while committing our changes or while pushing our changes to the remote branch.

These errors can sometimes be simple, which can be solved in a few minutes, but without any knowledge about the fix required to solve the error, it can be challenging. And if you're a beginner, who has just started working with Git, some of the errors can be quite daunting indeed. In this article, we are going to discuss some of the most common errors I have come across while using Git for my projects along with their fixes. Let's go through them.

1. Wrote an illogical commit message and regret about it

Imagine a scenario where you had to add a new feature in a very short time and commit the changes and push it to the remote repository because it was or to give a hotfix for a bug. You were able to complete the feature, tested it locally, and it worked well. Thus you decided to commit the changes consisting of the features to your remote repository. While committing the changes in the local branch, you accidentally typed an inappropriate message in the commit message and were tense about finding a way to edit this message. Well, to solve your problem, we can use the git commit --amend command to get your work done.  But it is a best practice not to use it to modify the commits in remote repositories.

2. Committed an irrelevant file to your local repository and don't know how to undo it

Consider you are working on a particular solution, where you use a couple of images in our project, which is used in some of the web pages of your applications. These images are the logos of your partner organizations that you are highlighting on your web page. You had earlier downloaded the images to your local system. You took all those images and pasted them inside the image folder of your solution, and committed the images in your local repo, but while you were verifying the commit again before pushing the changes to the remote repository, you realized that one of your images was also committed. You might have moved it along with the rest of the images. Now, you want to remove the image and are wondering how you can remove the file from your local repository. You can do this using the below-mentioned command.

git rm <enter-file-location-here>

This command is going to remove the file both from the repository and the local file system. Since we already have a copy of the file, it is not a point of concern for us. But, if it was of concern to you, you could use the below-mentioned command to solve your problem:

git rm <enter-file-location-here> --cached

3. fatal: not a git repository

Fatal: not a git repository is one of the infamous errors that you usually get when you are trying to clone a repository to your local system. There can be multiple reasons behind this particular issue. One common reason can be typo errors in the repo name. The repositories are case sensitive.

Source

If the repo you are trying to clone is git@github.com:user/myRepo.git, but your repository name is actually MyRepo, then you are going to face this particular error as the user does not have any repository named myRepo to begin with. We can avoid these issues from arising by always copying the clone URL from the repository page whenever we want to clone it.

One more reason can be the lack of permission. A person will need to be the owner or collaborator on the repository to access it. If you are working on a repository that belongs to an organization, you will have to be a member of that organization to access it. Thus, you will have to check if you have required the permissions to access the repository.

But even after having permission and the correct repo name, the issue can be caused due to invaild credentials. While working on a separate repository, you may have logged in with different credentials, which don't have the permission to work with our current repo. We can resolve it by removing all the GitHub-related credentials in our credential manager, and then try cloning it. Then it will ask us to enter our credentials again, and we should be able to work with our repository.

4. Misspelled branch name while creating a feature branch

Sometimes, we can mess up the branch name while creating a new branch to work on some specific feature. Let's consider a scenario where you created a new branch called feature-1 to work on features for sprint 1. However, in a hurry to create this branch, you created the new branch as featured-1 instead of feature-1. Is there a way to work around it and change the created branch name? Yes! We can rename this branch by using the below-mentioned command:

git branch -m <old-branch-name> <new-branch-name>

Since we wanted to rename the branch named featured-1 to feature-1, let's replace <old-branch-name> as featured-1 and <new-branch-name> to feature. This should do the trick for us.

5. fatal: refusing to merge unrelated histories

Sometimes, you feel like experimenting with your branches to see if you can meld two projects together. While doing that, you've probably experienced the fatal: refusing to merge unrelated histories error. This error can pop up in three different scenarios: you're trying to merge repos where the histories for branch and remote pull are different, there's something wrong with one of the git directories, or the branches are at different HEAD positions. The simplest fix for this is "-allow-unrelated-histories" -- this one line command can help resolve more circumstances when this error emerges.

Conclusion

Git is an amazing tool that helps us efficiently manage our codebase and track their changes. Git helps us manage multiple versions of our codebase and helps us revert to a particular version if we have a faulty commit in the repository. Along with that, we can also collaborate with team members to develop our applications by working on our respective versions of the remote repository, and after completing our respective works, commit the changes back to the remote repository. Though it can be difficult to work with Git as a beginner and you may encounter errors, you will eventually get a hang of it.

Discover and read more posts from Josh Robins
get started