1

I pushed some code to a remote branch, but I quickly found a bug.

I wanted to 'un-commit", so I ran

git reset hard~~ (extra tilde was by accident)

I probably messed up because after I fixed the bug I commited and pushed, but when I try to push I get an error.

running

git status

shows that "my branch and the remote branch have diverged, and have 1 and 2 different commits each"

I think this is what it looks like now:

o ---- o ---- A ---- B -- D  origin/master (2 commits ahead of A)
               \
                C  myBranch (my work (1 commit ahead of A)

How do I change my repository so that it looks like

o ---- o ---- A ---- C

2 Answers2

2

Force push to update origin/master to commit C.

git push -f

Caution: A forced push changes the history of a branch and requires other developers who've pulled the branch to perform manual recovery steps.

The commit graph will end up as:

o <--- o <--- A <--- C  myBranch, origin/master
              ^
              |
              +--- (B) <--- (D)

With no branch pointing at D, commits B and D will be orphaned. They point to A, but no commits and no branches point at them, so they're effectively gone. They'll be visible in your reflog but nowhere else. Eventually, if you do nothing else with them, the garbage collector will delete them.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Will this cause trouble in the future if someone else has already merged with this branch (the version with B-D)? Can I safely merge in the future? – person314314314 Jun 04 '19 at 15:51
  • The B-D commits will be orphaned and eventually garbage collected, as will anything merged into that line of the commit graph. The commits will be in your reflog for a few weeks and will eventually be deleted if you don't resurrect them. Yes, you can safely merge on top of C going forward. – John Kugelman Jun 04 '19 at 15:53
  • Wait, when you say I can merge safely on top of C, does that mean the person who's already merged with this branch can merge with my branch in the future? Doesn't that person already have commits B and D in their branch? What happens to their history if they try to merge again after I run git push -f? – person314314314 Jun 04 '19 at 16:02
0

You should execute this from master branch to remove B and D commits (last 2 commits)

Note: Be very sure what you are doing here because it will remove your B and D commits with history. If you are not sure, take a backup first.

git reset --hard HEAD~2
git push --force

After B and D are removed, you can merge branch with C commit on top of A.

user
  • 867
  • 9
  • 21
  • Will this cause trouble in the future if someone else has already merged with this branch? Can I safely merge in the future? – person314314314 Jun 04 '19 at 15:50
  • If a new branch X is created from before deleting commit B and D, X branch will contain changes done in B and D. When user will try to merge Branch X in master, it will look like Branch X has introduced B and D. so it is important to rebase branch X to keep it updated with changes done in master. – user Jun 05 '19 at 09:54