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.

2
  • push --force or better yet push --force-with-lease
    – Whymarrh
    Jan 25, 2018 at 1:39
  • Do note that force push is a destructive action and that you should be 110% sure you're doing the right thing first.
    – Whymarrh
    Jan 25, 2018 at 1:40

2 Answers 2

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.

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.

1
  • Good feedback, which does complete my answer.+1
    – VonC
    Jan 25, 2018 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.