0

so I have a git repo that I had made commits and pushed the repo to the server. Then I rolled back a few commits with git revert to a previous commit on local. Now when I try to push to the server I get the error

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.

I understand that I reverted on my local branch, so now the remote branch has commits that aren't on the local branch. I just want to also remove those commits on the server. I just want to be able to push my local branch the way it is and "replace" the head on the server. How do I do that? I don't want to pull the changes from the server since I want to trash those anyways.

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160
  • Use the [`--force`](https://git-scm.com/docs/git-push#Documentation/git-push.txt---force), Luke. – Romain Valeri May 26 '20 at 15:12
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd May 26 '20 at 15:20

1 Answers1

1

This is the time you need to execute a --force-with-lease push:

git push --force-with-lease <remote> <branch>

For example:

git push --force-with-lease origin 1234-my-feature

Warning: --force and its variant "with lease" allow you to change the history of a branch, effectively deleting one or multiple commits. Therefore relying on force-pushes in distributed environments should remain the exception, or only be performed if you are sure nobody is relying on 1234-my-feature when creating their own branches. See git push --force-with-lease vs. --force for an explanation of their difference.

One common convention is: feature-branches may be force-pushed to by their respective owners/authors, but once they are merged to a master or shared development branch, their history may no longer be modified. But your mileage may vary, depending on how loose/strict your development environment is organised.

ojdo
  • 8,280
  • 5
  • 37
  • 60