How do I clean up my Github fork so I can make clean pull requests?

后端 未结 3 1221
终归单人心
终归单人心 2020-12-22 14:55

I forked a repository on Github. I\'ve made some minor changes and submitted pull requests to the upstream, but along the way my fork has become so mangled I\'m unable to ge

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 15:53

    Step 1: Pull upstream changes
    It's recommended to add the upstream repo as "upstream" as explained on the Fork a Repo page:

    git pull --rebase upstream master
    

    The --rebase option places your changes on top of the latest commit without merges.

    Step 2: (Optional) Merge your commits into 1 commit

    git reset --soft upstream/master
    

    This command will "undo" all your commits, but won't change the files. So you can commit all your changes in a single commit.

    git commit -a
    

    Step 3: Check & test your changes

    To show the changes use a GUI like the built-in gitk, Sourcetree, TortoiseGit or Tower (paid), etc.

    Step 4: Push

    git push will throw an error, because the push would change the target repository's history.
    If you're confident the changes shown in step 3 are correct then push with "-f"

    git push -f origin master
    


    Additional information
    The command to add a remote is:

    git remote add upstream git://github.com/[username]/[project].git
    

    You can also also pull from a direct URL:

    git pull --rebase  git://github.com/[username]/[project].git
    

    But then you'll need the hash of the latest upstream commit instead of "upstream/master" in the other steps.

提交回复
热议问题