2

I pushed a branch based off another branch to GitHub. It has one commit, but shortly after I realized I pushed the wrong version of that branch. I immediately deleted the branch using git push origin --delete <branch_name> but saw in one of my project tracker programs that the commit I made is still there.

I unfortunately did not think to delete the commit locally and push, then delete the branch locally and push.

Now that the remote branch is deleted, how do I also delete the remote commit?

If I understand correctly, through my research here: Does deleting a branch in git remove it from the history? my commit should be an unreachable/dangling commit but I have not been able to find any answers where I can delete a remote commit.

I have saw that you can use git gc but is that enough to get rid of the remote commit? Is it possible to keep the local branch and local commit but delete the remote commit?

My end result is that I'd like the remote branch to be deleted (seems good) and the remote commit to be deleted (needs help). Thank you!

1 Answers1

0

Deleting the remote branch is one step.

Locally, you need to reset your branch to some past commit (which represents the right version) and push again.

git checkout myBranch
git reset --hard <pastCommitSHA1>

On the remote side, make sure none of your commits are still referenced by:

  • other branches
  • a tag
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for answering! I'm new to git so just to make sure I understand what you mean. So with my remote branch being deleted, the remote commit is dangling. I need to checkout the local branch, git reset --hard , push the local branch with this change so now the local is pushed to remote again. This will then show the remote branch no longer having this commit so now I will have to delete the branch again? If you're able to clarify that would be awesome, thank you so much! – angelicsunflower Mar 30 '19 at 08:03
  • @angelicsunflower if you push your reset branch again, you don't need to delete it (again): the remote branch will reflect the reset local history. – VonC Mar 30 '19 at 17:31