Better Programming

Advice for programmers.

Follow publication

Recovering From Common Git Errors

To get back to your development faster

Parul Dhingra
Better Programming
Published in
5 min readJun 27, 2022

We often come across some common errors when working with something as intricate as Git. But as humans, we’re going to make mistakes and after all, the most effective way of learning is from a mistake.

So, instead of panicking and without any further ado, here I am sharing all those git mistakes I made and how I fixed those mistakes.

Error 1: Inadvertently Deleted a Branch

So, there can be two scenarios: either you realize the mistake right away(i.e., instead of the intended one, deleted another branch) or you don’t realize initially that you deleted the wrong branch but later you realize it.

In the first scenario, you can simply check the message in the terminal after the deletion of the branch. It may look something like this:

$git branch -d branch1
warning: deleting branch 'branch1' that has been merged to 'refs/remotes/origin/branch1', but not yet merged to HEAD.
Deleted branch branch1(was e9498ae).

Here, we see the SHA value of the commit to which the branch pointed when it was deleted. Using this SHA value we can create a new branch from that commit itself. You just need to do the following:

git checkout [SHA]

And once you're at that commit, you can recreate the branch from there by using:

git checkout -b [branchname]

In the second case, if you’ve lost the message, to find the SHA of the commit that was at the tip of the deleted branch. Do git reflog, which is actually the record of all commits that are or were referenced in your repo at any time.

Then, to proceed, you may perform the same commands as mentioned in the first case above.

Error 2: You Deleted a File or Directory

Here’s a scenario we all dread!

All of us have definitely deleted the wrong file from our project at least once. It can either be an abruptly executed rm -rf command, or an absent-minded select and delete, or maybe the result of any erroneous script!

Whatever the reason, deleting an important file can be exasperating if not fixed immediately.

Again, there can be many possible scenarios:

  1. If you immediately realized it was a mistake, you need to run the following command:

$ git checkout HEAD <filename>

2. Or, another case can be you deleted the file and committed the deletion as well. The following command will take you back to the state before your commit.

So, there’re three ways you can do so:

$ git reset --soft HEAD~1

Here, we’re saying let’s roll back in time, but let’s hold onto those changes and let’s keep them in the staging area and in the working directory as if they’re just not committed yet. It’s a little bit similar to git commit amend.

$ git reset --mixed HEAD~1

A mixed reset allows us to return to an old state, just like a soft reset does, and it leaves code changes in the working directory but not in the staging area.

$ git reset --hard HEAD~1

Now the head pointer is pointing back to one commit, just like the other ones did. But, if you’ll get the status, your changes aren’t there. They’re just gone!

Using hard reset, git doesn’t try to keep track of them. It doesn’t put them in my staging area or my working directory. It just says, “you know what, forget about them.”

3. Now, let’s deal with the scenario where you committed the deletion and then did more commits.

First, to find the right commit, check the history for the deleted file:

$ git log -- <filename>

You can either work with the last commit that still had the file or the commit that deleted the file. In the first case, just check out the file from that commit:

$ git checkout <commit hash> -- <filename>

In the second case, check out the file from one commit before that:

$ git checkout <deletion commit hash>~1 -- <filename>

4. Another possible mistake we make is that we deleted a file, committed it, and pushed it.

If you’ve already pushed your commit or commits to a remote, resetting and pushing again will cause problems, as the history of the local repository has essentially been rewritten. In this case, it is probably better to record a new commit that undoes the work of the one deleting the file. To do this, run the following command:

$ git revert --no-commit <commit>

Above, <commit> is the commit deleting the file. Following this, create your new commit as desired. The “— no-commit option prevents the command from creating a new commit right away, instead of allowing you to choose exactly which of the changes introduced in the old commit you want to revert in your new commit.

Error 3: You Added a File You Didn’t Want in the Repo

Damn! I recently faced this issue, and it’s a pain!

I accidentally pushed the file, and I just don’t want it there anymore. So, this is the command you can use to delete a file from your git repo:

git rm --cached <filename>

This will not delete it locally, so it is safe on your computer if you want to keep it in there for reference without sharing it on Git.

Note: To prevent it from being pushed to Git again, just add the file to your .gitignore.

Of course, if you just don’t need the file anymore at all, simply delete it from your system as well.

Wait!

But what if the file had sensitive data like passwords or secret keys?

As you might be aware, the commits can still be found in the repository’s history. So, apart from this, you’ll need to do a little bit more.

If the pushed file has sensitive data like passwords

The above method will not completely remove the file from the commit history.

To remove the sensitive file from your history as well, you can use an open source tool called the BFG Repo-Cleaner or use git’s git filter-branch.

BFG Repo-Cleaner makes it much easier and faster to get the job done as compared to filter-branch. It allows you to delete files or, alternatively, replace the passwords or keys within the file with the text ***REMOVED***. You can also remove huge files that you accidentally pushed.

You can check out BFG Repo-Cleaner’s documentation and download page to see how to use it. The instructions are pretty forthright.

Also, to learn about the git filter-branch, check out git's Using filter-branch article on their help site and the git filter-branch documentation.

Error #4: You Forgot to Add a File to That Last Commit

Another common Git pitfall is committing too early.

You missed a file, forgot to save it, or need to make a minor change for the last commit to make sense. --amend is your best friend.

Add that missed file and then run that trusty command:

git add missed-file.txt
git commit --amend

At this point, you can either amend the commit message or just save it to keep it the same.

So, these were some common fixes to get out of the most common Git errors.

If you have some git tips of your own, let me know in the comments below, I’d love to hear them.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Parul Dhingra
Parul Dhingra

Written by Parul Dhingra

Engineering @Microsoft | Tech Blogger | A brain ambidextrous geek

Responses (1)

Write a response