Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a local git branch when it can't look up commit object in 'refs/heads'?

Tags:

git

branch

Lately every git pull I do gives me an error message like:

error: refs/heads/TestBranch123 does not point to a valid object!

These are old branches and I don't care, so I'd just like to delete them so I don't see the error anymore. When I try to delete the branch I can't for a similar error.

git branch -D TestBranch123

error: Couldn't look up commit object for 'refs/heads/TestBranch123'

like image 225
heidi123p Avatar asked Dec 20 '13 01:12

heidi123p


People also ask

How do I delete a local branch remotely?

You'll often need to delete a branch not only locally but also remotely. To do that, you use the following command: git push <remote_name> --delete <branch_name>.

How do I remove a git commit from a local repository?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Can we delete a branch in git?

Deleting a branch LOCALLYDelete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.


1 Answers

Those error messages sound worrying to me. Your repository might be corrupt. Git should not garbage collect commits that branches are pointing to. You might want to try git fsck, as torek suggests, to verify your repository.

However, you could delete the file representing the branch from your git repository:

rm .git/refs/heads/TestBranch123

You should also clean up any references to the branch from your git config files.

like image 197
Klas Mellbourn Avatar answered Sep 18 '22 20:09

Klas Mellbourn