Search code examples
gitgithubpull-requestgit-pullgithub-for-windows

how to update/sync a pull request?


In my local machine I cloned a repository using this command below,

git clone https://github.com/kp/ml_project.git

Now in the original project there was a pull request made and it's pull request number is 22. So I've added the pull request using these below commands.

cd ml_project
git fetch origin pull/22/head:pr22

Now after few days in the pull request 22 they were new commits added and I want those to be available to me. What can I do? How can I update/fetch new commits into my branch pr22


Solution

  • Make sure you don't have branch pr22 checked out,1 then run the same git fetch. (Then you can git checkout the branch again, if you want.)

    If they have only added commits, it will run fine. If they replaced commits (took some out, put in some different ones instead), e.g., after a rebase, you'll get a "non-fast-forward" error.

    If you would like to throw out your old pr22 value and store the new updated tip anyway, simply force the fetch to update despite the not-a-fast-forward condition. To force the fetch, you can use git fetch --force—this tells git fetch to set the force flag on every refname update—or use a plus-sign + character on the specific refspec containing a ref you wish to force:

    git fetch origin +refs/pull/22/head:refs/heads/pr22
    

    (In this case, there's only one refspec,2 so + and --force do the same thing. Here I have fully spelled out both refs in the single refspec. As you've seen, you can abbreviate the refs in common cases. Spelling them out in full is wise in scripts, though, as Git will resolve partial refs to full ones in surprising ways sometimes.)


    1For instance, run git checkout master to switch away, or just use git checkout --detach to get into "detached HEAD" mode on the current commit. Either of these avoid the pitfalls that "update my current branch name" present.

    2A refspec is, roughly, a pair of refs or references with a colon between them: pull/22/head:pr22 is an example of a refspec. Prefixing a refspec with the plus sign makes it a forced update. For more about these, see the git fetch documentation and the git push documentation.

    A ref is any branch name like master, tag name like v2.1, remote-tracking name like origin/master, or any other such name. All of these names, in Git, are refs. A branch name is simply a ref whose full name starts with refs/heads/ and a tag name is simply a ref whose full name starts with refs/tags/. Git normally abbreviates these refs by stripping off the refs/heads/ or refs/tags/ part for convenience.

    When you use the abbreviated form, Git has to guess what the full name should be. If you have a branch named master and a tag named master too, Git will guess "tag" first—so don't do that, and in scripts, spell out the full name.