Preferred Github workflow for updating a pull request after code review

旧时模样 提交于 2019-11-26 17:59:57

Just add a new commit to the branch used in the pull request and push the branch to GitHub. The pull request will automatically be updated with the additional commit.

#2 and #3 are unnecessary. If people want to see only where your branch was merged in (and not the additional commits), they can use git log --first-parent to only view the merge commit in the log.

AD7six

To update a pull request

To update a pull request (point #1), the only thing you need to do is checkout the same branch the pull request is from and push to it again:

cd /my/fork
git checkout master
...
git commit -va -m "Correcting for PR comments"
git push

Optional - Cleaning commit history

You may be asked to squash your commits together so that the repository history is clean, or yourself want to remove intermediary commits which distract from "the message" in your pull request (point #2). For example if your commit history looks like this:

$ git remote add parent git@github.com:other-user/project.git
$ git fetch parent
$ git log --oneline parent/master..master
e4e32b8 add test case as per PR comments
eccaa56 code standard fixes as per PR comments
fb30112 correct typos and fatal error
58ae094 fixing problem

It's a good idea to squash things together so they appear as a single commit:

$ git rebase -i parent/master 

This will prompt you to choose how to rewrite the history of your pull request, the following will be in your editor:

pick 58ae094 fixing actual problem
pick fb30112 correct typos
pick eccaa56 code standard fixes
pick e4e32b8 add test case as per PR comments

For any commit you want to be part of the previous commit - change pick to squash:

pick 58ae094 fixing actual problem
squash fb30112 correct typos
squash eccaa56 code standard fixes
squash e4e32b8 add test case as per PR comments

And close your editor. Git will then rewrite the history and prompt you to provide a commit message for the one combined commit. Amend accordingly and your commit history will now be concise:

$ git log --oneline parent/master..master
9de3202 fixing actual problem

Push that to your fork:

$ git push -f
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 978 bytes, done.
Total 11 (delta 9), reused 7 (delta 6)
To git@github.com:me/my-fork.git
   f1238d0..9de3202  HEAD -> master

and your pull request will contain a single commit, incorporating all changes previously split into several commits.

Changing history on public repos is a bad thing

Rewriting history and using git push -f on a branch that, potentially, someone else has already cloned is a bad thing - it causes the repository's history and that of the checkout to diverge.

However, amending the history of your fork to correct the change you are proposing to be integrated into a repository - is a good thing. As such have no reservations squashing "noise" out of your pull requests.

A note on branches

In the above I show the pull request as having come from the master branch of your fork, there's nothing wrong with that necessarily but it does create certain limitations such as, if this is your standard technique, only being able to have one PR open per repository. It's a better idea though to create a branch for each individual change you wish to propose:

$ git branch feature/new-widgets
$ git checkout feature/new-widgets
...
Hack hack hack
...
$ git push
# Now create PR from feature/new-widgets
Clay Bridges

My opinion on best practice: once you are ready to package up a pull request, it should get its own unique topic branch, specifically for that purpose, at the outset. You start by pushing that branch to your github repository, e.g.

git push origin name-of-pull-request-branch

and basing the pull request off that branch. Having done that, any commits you push to that branch will automatically be appended to the pull request. You use that branch for nothing else.

Some prefer that you namespace such branches with your github userid. That way they can freely check it out locally to try it out, with benefits like

  • less fear of branch name collision
  • easier to remember what it is

I usually name my pull request branches something like

claybridges-do-the-things
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!