How do you perform a reset

We have a branch pointing to the wrong place. We need to change that. The new CL is NOT a descendant of the old one. On the local repo I was able to do this using a "git checkout mybranch; git reset mybranch-new". However, it won't let me push this:

error: failed to push some refs to ''
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


1 answer

The error you're getting is just Bitbucket telling you that you forgot to include the "–force" flag in your push.   And so the simple answer here is to just make sure to include that flag when you push!

For a more complicated answer, keep reading.  smile

I wouldn't bother doing anything locally.  Instead I would just update the remote branch directly using "git push --force", like so:

git push -f origin <commitId>:refs/heads/[BRANCH-TO-UPDATE]

That command basically says "make [BRANCH-TO-UPDATE] point to <commitId> NOW!"  You need to add the "–force" flag if origin/[BRANCH-TO-UPDATE]'s previous commit-id is not an ancestor of the new <commitId> you're replacing it with (in other words, if you're rewriting history).

Often admins setup Bitbucket repos to reject history rewriting.  If your repo is setup that way you will temporarily need to disable the pertinent hooks or branch permissions before you can proceed.

Note:  if your branch is called "feature/TKT-123" then the push goes to: "refs/heads/feature/TKT-123".   Sort of annoying that you have to include "refs/heads/" when specifying the target branch, but it seems to be necessary when using the "git push" command in this way.

Here's a concrete example:

git push --force origin 828dc7694fac1a5ded:refs/heads/testBranch


Of course there's still the whole problem of what do with any local versions of the old branch people have lying around.  I would recommend they do "git fetch; git stash; git reset --hard origin/[BRANCH-TO-UPDATE]; git stash pop" before continuing any work in their local copies of the branch.   


p.s. I invite you to try my Bitbucket and JIRA add-ons:  Bit-Booster Commit Graph and More (for Bitbucket Server), and Git Graph for JIRA.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Thursday in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder :  #!/bin/bash source "$(dirname "$0")/" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables ROLLBAR_ACCESS_TOKEN...

188 views 0 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you