Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do you perform a reset

mrjimorg October 27, 2016

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 'https://mrjimorg@bitbucket.org/alterapartners/hwlibs.git'
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

0 votes
G__Sylvie_Davies__bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 27, 2016

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
TAGS
AUG Leaders

Atlassian Community Events