Panjeh
Panjeh
Sep 2, 2018 · 2 min read

Git status fatal: bad object HEAD


First of all, note that using git itself is not enough to keep data safe. You still need to back it up and have a remote repository in cases of corruption.

There are some reasons when a git repository becomes badly damage and when you do:

git status

you will see :

fatal: bad object HEAD

Reason:

More likely a disk error or an unclean PC or Laptop shutdown or disconnect suddenly and unsafely a hard drive from laptop could have left the repository in this state.

The consequence problem is that the ~/main_repo/.git directory of your repository is damaged.

There are several ways and methods to fix it but if you have a backup in GitHub or other online git repositories or another remote server, you can fix such errors easily.


Solution:

I assume you have a backup in github like:

https://github.com/your_account/backup_repo

So make a temp directory and then clone the backup repository in it.

Like:

cd ~git clone https://github.com/your_account/backup_repo.git

Note: Be careful for cloning the repository with suffix .git like above. If you put backup_repo instead of backup_repo.git then you can not find .git directory inside what you have cloned.

Then replace the broken .git folder with the one from the clone by mv or cp -r commands.


Warning:

If you delete the damaged .git directory by:

rm -rf ~/main_repo/.git

and try to copy the clone (backup) ~/backup_repo/.git directory to ~/main_repo/ by :

cp -r ~/backup_repo/.git ~/main_repo/

you will lose all the things that are not in your working directory (local branches, stash, etc).

So first of all just use cp -r or mv try to fix the problem.

mv ~/backup_repo/.git ~/repo/orcp -r ~/backup_repo/.git ~/repo/

Then

git status

You will see the stage becomes normal!

If you want to run all the git commands just in one command read this.


Panjeh

Written by

Panjeh

Hire me! Web geek, Self-taught full-stack web developer, Physicist PhD, Learning Laravel, Vuejs, UX/UI design

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade