2

I reset my local develop branch to a few commits behind and would like to push the local develop branch to its remote so that the remote's HEAD is also now reset to the few commits behind.

In other words, I did like so:

UserName path/ (develop)
$ git reset --hard {CommitIdOfAFewCommitsBehind}
Your branch is behind 'origin/develop' by 14 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working tree clean


$ git push origin develop
To https://github.devtools.merrillcorp.com/Javelin/wopi-poc.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'https://github/repo.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.

How do I make the remote also go to the commit ID I want?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336

1 Answers1

4

When you did hard reset of your develop branch, you rewrote the history of that branch. One side effect of this (another side effect discussed later) is that Git can no longer just accept a push from you, because it doesn't know how to link your new truncated branch with the version on the remote. So, you need to force push to overwrite:

git push --force origin develop

I mentioned another side effect, which is that rewriting the history of develop will also cause problems for anyone else on your team, who happens to be sharing this branch, when they pull.

Most of the time, for shared branches it is preferable to do a git revert instead of nuking commits with a hard reset. Check the documentation or Stack Overflow to see how to revert a series of commits.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360