How to remove commits from a pull request

前端 未结 5 1169
既然无缘
既然无缘 2020-12-22 16:38

I did a pull request but after that I made some commits to the project locally which ended polluting my pull request, I tried to remove it but without any luck.

I fo

相关标签:
5条回答
  • 2020-12-22 17:24

    If you're removing a commit and don't want to keep its changes @ferit has a good solution.

    If you want to add that commit to the current branch, but doesn't make sense to be part of the current pr, you can do the following instead:

    1. use git rebase -i HEAD~n
    2. Swap the commit you want to remove to the bottom (most recent) position
    3. Save and exit
    4. use git reset HEAD^ --soft to uncommit the changes and get them back in a staged state.
    5. use git push --force to update the remote branch without your removed commit.

    Now you'll have removed the commit from your remote, but will still have the changes locally.

    0 讨论(0)
  • 2020-12-22 17:24

    This is what helped me:

    1. Create a new branch with the existing one. Let's call the existing one branch_old and new as branch_new.

    2. Reset branch_new to a stable state, when you did not have any problem commit at all. For example, to put it at your local master's level do the following:

      git reset —hard master git push —force origin

    3. cherry-pick the commits from branch_old into branch_new

    4. git push
    0 讨论(0)
  • 2020-12-22 17:31

    You have several techniques to do it.

    This post - read the part about the revert will explain in details what we want to do and how to do it.

    Here is the most simple solution to your problem:

    # Checkout the desired branch
    git checkout <branch>
    
    # Undo the desired commit
    git revert <commit>
    
    # Update the remote with the undo of the code
    git push origin <branch>
    

    The revert command will create a new commit with the undo of the original commit.

    0 讨论(0)
  • 2020-12-22 17:34

    So do the following ,

    Lets say your branch name is my_branch and this has the extra commits.

    1. git checkout -b my_branch_with_extra_commits (Keeping this branch saved under a different name)
    2. gitk (Opens git console)
    3. Look for the commit you want to keep. Copy the SHA of that commit to a notepad.
    4. git checkout my_branch
    5. gitk (This will open the git console )
    6. Right click on the commit you want to revert to (State before your changes) and click on "reset branch to here"
    7. Do a git pull --rebase origin branch_name_to _merge_to
    8. git cherry-pick <SHA you copied in step 3. >

    Now look at the local branch commit history and make sure everything looks good.

    0 讨论(0)
  • 2020-12-22 17:43

    People wouldn't like to see a wrong commit and a revert commit to undo changes of the wrong commit. This pollutes commit history.

    Here is a simple way for removing the wrong commit instead of undoing changes with a revert commit.

    1. git checkout my-pull-request-branch

    2. git rebase -i HEAD~n // where n is the number of last commits you want to include in interactive rebase.

    3. Replace pick with drop for commits you want to discard.
    4. Save and exit.
    5. git push --force
    0 讨论(0)
提交回复
热议问题