Free Radical
Free Radical

Reputation: 434

Detached Heads, Delete Pushed Merge, Etc

I am trying to fix a git repository that has gotten out of whack. Here's the issue.

What needs to be accomplished:

  1. Delete the merge. I attempted to do a hard reset to the previous commit on master, but it ends up squashing the detached head's commits into the master branch.
  2. If possible, make the detached head and its commits the master branch
  3. Make all commits in master after the head was detached a separate branch

In the picture below, blue is master and red is the detached head.

enter image description here

Here's a second picture for reference.

enter image description here

Upvotes: 0

Views: 306

Answers (1)

SpoonMeiser
SpoonMeiser

Reputation: 20417

So long no one else has cloned your repository, you should be able to do this.

Making a branch from the current master:

git branch <branch name> <commit hash for last commit on master before the merge>

Make master the detached head branch

git checkout master
git reset --hard <commit hash of last detached head commit before merge>

Now locally, you should be in the state you desire, but local master has different history to your remote master. You might be abe to just force push it to disregard the remote master

git push -f

However, services like gitlab and github will prevent you from doing this by default.

If anyone else has a local clone of the repository, this will cause them problems, making a fresh clone would be the easiest fix, if you're able to communicate with everyone affected.

Upvotes: 1

Related Questions