1

I need to change a remote git branch to point to a previous commit in the commit tree.

I have four branches: master, ui, test, release. Master and UI are parallel development branches that are merged often. Test and release branches point to previous commits on master.

A coworker screwed up (don't ask) and did:

git checkout release
git reset --hard origin/ui
git push

And now I have been tasked with fixing it. My googlefu is failing me and while I can fix the release branch back to how it was on my local repo, I can't figure out how to push the change to the remote server (origin). I can't do revert because of all the merging between master and ui. I tried

branch -f release <SHA1> 

and if I push I get

! [rejected]  release -> release (non-fast-forward)

and it tells me I need to fast forward before I can push. I also tried

git reset --hard <SHA1>

with the same result. I found lots of help on removing previous commits to a remote repository but I am failing to find directions on how to move the pointer backwards on a commit tree. Any help would be appreciated.

Bakanekobrain
  • 423
  • 6
  • 14

2 Answers2

3

The next step is a simple git push --force (communicating fist to your colleagues they will have to fetch and then reset their own master branch)

Note sure a force-with-lease is needed here, considering it interacts badly with any fetch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

The answer to the question is git push --force, which is why I marked it as the answer. But in my case there was one additional step. On force pushing to bitbucket I got the error:

remote: permission denied to force push branch release
To https://user@bitbucket.org/user/repo.git
 ! [remote rejected] release-> release (pre-receive hook declined)
error: failed to push some refs to 'https://user@bitbucket.org/user/repo.git'

(For some reason I missed reading the "permission denied" line when I did it the first time.)

To fix this, in the bitbucket repo you need to go to Setting -> Branch permissions -> Edit branch permission (little pencil icon) -> Check "Allow rewriting branch history" -> Save

This fixed the permission issue.

Bakanekobrain
  • 423
  • 6
  • 14