Abandoning Git commits on Github for rejected pull requests

后端 未结 2 803
挽巷
挽巷 2021-02-01 22:12

I\'ve forked someone\'s project and made some commits. I filled out a pull request, and it wasn\'t accepted (either the change was not desirable, or the author rewrote the func

相关标签:
2条回答
  • 2021-02-01 22:50

    You can reset your repo state to an earlier commit. First figure out which commit you want to reset your repo to:

    git log
    

    To reset your repo to that state:

    git reset --hard <commit_hash>
    

    If you have a forked remote repo, you can push these changes back to it:

    git push -f <remote> <branch>
    

    You might want to change your workflow to make things easier in the future though.

    When I fork a repo and am making my own changes to it, I first set up two remotes. One remote will point to my forked repo (ex: origin), and add another remote points to the original repo that was forked from (ex: original_repo). So I might have something like:

    $ git remote
    origin
    original_repo
    

    I create a branch to do all my work in, ex: feature. When making a pull request, I do it from the feature branch to the original_repo master branch. If the pull request is rejected, like your example, you can just abandon this branch. If you want to work on more modifications, just create another branch from master and use that to work in.

    I also don't commit or merge any local changes to the master branch. I only use the master branch to sync up with the original_repo master branch. ex:

    git checkout master
    git fetch original_repo
    git merge original_repo/master
    

    This ensures the master branch is always synched up with the original repo's master branch. For example, if the pull request was accepted and merged in, when the fetch and merge happens, local master would also have all the 'approved' code used in the original repo.

    Basically use master to sync up with the original repo's master and always branch from master when you want to make edits. Use those branches for your pull requests to the original repo.

    0 讨论(0)
  • 2021-02-01 23:05

    Back your master branch a few steps (assuming you want to skip three commits back) and rewrite your github repo by pushing with -f.

    git reset --hard HEAD~3
    git push -f origin master
    
    0 讨论(0)
提交回复
热议问题