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:
you will see :
fatal: bad object HEAD
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.
I assume you have a backup in github like:
So make a temp directory and then clone the backup repository in it.
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.
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/
You will see the stage becomes normal!
If you want to run all the git commands just in one command read this.
Thank you for reading! If you enjoyed this article:
Clap it ! Share it! Follow Me in Medium!
Panjeh — Medium
Read writing from Panjeh on Medium. Physicists PhD, Self-taught web developer #Learning, #laravelphp #Vuejs. Every day…
Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.
Have a very wonderful day!
Previous Stories You will Love:
Makefile git add commit push github All in One command
As a remember, the traditional way of adding a file to a repository using the command line is summarized into 3 steps…
remove last commit local undo the most recent commit Git github repository
The solution is to first remove the last commit in local and then force push to Github