Search code examples
gitversion-controlgit-pushpull-requestgit-reset

How to "force" a pull request from a specific commit?


The scenario is the following:

  • We have around 10 commits that we want to revert from our master branch. They are causing some sort of issue and I need to revert to a specific commit 123abc;

Some things we tried here:

  • We created a new branch called feature/something in order to revert the commit and do a pull request to the master branch. However, VSTS is complainng that we don't have new changes.

  • When we do git reset --hard 123abc into master branch, my local space is reverted to the given commit, that's OK so far. However, I don't have permission to force a push to the master branch.

So, my questions are:

  • Regarding the 1st attempt, how to make git understand that I want to pull those changes to master branch and don't complain?

  • Regarding the 2nd attempt, how to force a pull request from master branch from that given commit?

Thank you all!


Solution

  • There is no way to make a branch to an old commit through pull request.

    If you have not force push permission, you should revert changes on a new branch and create PR to merge the new branch into master, or you can ask the team project administrator to reset master branch to an old commit.

    Option 1: revert the changes on master branch

    Assume the commit history on master branch as below, and the commits C1 to C10 are the commits you want to revert:

    …---A---C1---C2---…---C10   master
    
    • If master branch has no branch policies, you can directly revert on master branch and push to remote.

      # On local master branch
      git revert C1^..
      git push origin master
      

      After revert the changes, the commit history on master branch should be:

      …---A---C1---C2---…---C10---C10'---C9'---…---C1'   master
      
    • If master branch has branch policies, you can create a new branch (such as feature/something branch) from master branch, then revert commits on feature/something branch and create a PR to merge feature/something branch into master:

      # On local master branch
      git checkout -b feature/something
      git revert C1^..
      git push -u origin feature/something
      

      Then the commit history will be:

      …---A---C1---C2---…---C10   master
                              \
                               C10'---C9'---…---C1'   fearure/something
      

      You can create a PR to merge feature/something into master. And it shouldn’t complain there hasn’t new changes unless you revert/merge on the opposite way.

    Option 2: ask team project administrator to reset and force push to master branch

    If there has no branch policies on master branch, you can ask team project administrator to reset master branch and then force push (as the 2nd attempt you tried). Or you can ask the administrator to allow you to force push.