305

I'm trying to review a pull request on GitHub to a branch that isn't master. The target branch was behind master and the pull request showed commits from master, so I merged master and pushed it to GitHub, but the commits and diff for them still appear in the pull request after refreshing. I've doubled checked that the branch on GitHub has the commits from master. Why are they still appearing in the pull request?

I've also checked out the pull request locally and it only shows the un-merged commits.

Mateusz Piotrowski
  • 6,866
  • 9
  • 49
  • 75
lobati
  • 7,751
  • 5
  • 36
  • 57
  • 1
    Does this affect the behavior of merging the PR? – Nathan Hinchey Apr 10 '18 at 15:51
  • Nope, just the diff on Github. – lobati Apr 10 '18 at 17:13
  • Does anyone know if self-hosted Gitlab suffers the same behavior? – Jeff Welling Sep 23 '19 at 18:06
  • 13
    I suggest that we all contact GitHub to express our interest in having this behaviour changed (https://support.github.com/contact). If they don't hear from us then they won't know how important this is and it will be this way forever. – steinybot Apr 02 '20 at 20:50
  • Squash-merging can cause this (and requires a rebase). This answer covers how to fix it: https://stackoverflow.com/a/70994400/1759443 – Kyle Venn Feb 06 '22 at 18:33
  • I just wanna say cheers for wording this question so well. I didn't know it was possible to describe the problem in 11 words.. – rymanso Feb 25 '22 at 11:06

13 Answers13

223

Here's a good workaround. Use the Edit button when viewing the PR in GitHub to change the base branch to something other than master. Then switch it back to master and it will now correctly show only the changes from the most recent commits.

hexsprite
  • 2,970
  • 1
  • 19
  • 18
  • 7
    I'm surprised more people don't acknowledge this solution. I suspect the original out of date pull request would be just fine, but I didn't like the GitHub shows all the out of date files, so I used this and it both keeps the commentary and only shows the actual changes about to be merged. Thanks! – taranaki Jan 31 '20 at 00:41
  • 27
    This doesn't seem to work for me, it just adds the info that I changed the base branch to something else and then back to master. cfr: https://github.com/europeana/search/pull/30 – Calvin Aug 13 '20 at 12:32
  • 2
    For what it's worth, I checked this solution again just now, and it still works. Calvin's problem is that the target repo is squashing his PR, so the commit that gets added to the target repo is different to the one in his. He should be resetting his `master` branch each time, rather than merging in their upstream squashed commits, effectively duplicating all the commits. – Adam Millerchip Nov 12 '20 at 14:48
  • I added a comment to that PR with more details. – Adam Millerchip Nov 12 '20 at 14:58
  • 2
    Thanks, this worked for me. Ideally Github should do that automatically , or should provide an update option as done on Azure Devops SCM – Jay J Apr 01 '21 at 13:05
  • Any idea if this would become automatic on Github's end? Seems like a nice feature, but this solution definitely worked – EntryLevelR Jul 22 '21 at 20:09
  • This worked for me with Gitlab. Edited MR > Changed Branch to Merge to > Save > Edit > Changed Branch to Merge to back to Master > Save > Profit. – Dean Meehan Oct 14 '21 at 08:46
  • This doesn't work for me. I end up back in the same position is started. – Brad Mar 09 '22 at 22:40
  • This has not worked to me, I have checked that the commit was effectivelly merged into the base branch (which is not master) but the commits is still in the Commits tab, although their changes are not displayed in the Files Changed tab, since it was already merged. – Sergio Prats Apr 11 '22 at 07:46
  • Best one so far! – voiddrum May 19 '22 at 18:22
165

It looks like the Pull Request doesn't keep track of changes to the target branch (I contacted GitHub support, and received a response on 18 Nov 2014 stating this is by design).

However, you can get it to show you the updated changes by doing the following:

http://githuburl/org/repo/compare/targetbranch...currentbranch

Replace githuburl, org, repo, targetbranch, and currentbranch as needed.

Or as hexsprite pointed out in his answer, you can also force it to update by clicking Edit on the PR and temporarily changing the base to a different branch and back again. This produces the warning:

Are you sure you want to change the base?

Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.

And will leave two log entries in the PR:

enter image description here

Adam Millerchip
  • 16,151
  • 5
  • 41
  • 56
  • 6
    Yeah, I contacted support a while back and got the same response. They don't optimize for this situation. It is a bit frustrating. Our way of getting around it is to just rebase and force push up, or to close the pull and create a new one. – lobati Nov 18 '14 at 15:54
  • 8
    This answer doesn't fix the problem, but just allows the user to see the true diff. – FearlessFuture Jun 28 '16 at 03:14
  • 8
    The question was "Why are they still appearing in the pull request?". It answers that question. – Adam Millerchip Jun 28 '16 at 03:21
  • 1
    In our case, compare URL still shows changes already present in target branch. – Jared Jan 23 '17 at 11:39
  • fyi, for others I think this is fixed now. But it depends now on when you create the 2 PRs. If you create PR1 with commits A, B, C, and then start a new branch off that, which then has new commits D, E, then if you create a new PR2, then PR2 will have all the commits, A,B,C,D,E if you haven't merged PR1 yet (and will continue to have all the commits, even after you merge PR1) but will only have commits D,E if you have already merged PR1 when you create PR2. Just fyi, since I had normally created it the second way, and then wondered why the first situation was happening. – JSoet Oct 06 '17 at 13:10
  • 2
    @JSoet The behaviour has not changed, I've just reproduced it. Please see https://github.com/amillerchip/merge-test/pull/1. Commit 07d3349 "Add more text" is showing up in the PR, even though it exists in the target branch: https://github.com/amillerchip/merge-test/commits/first_branch however the correct diff can be seen using the compare URL: https://github.com/amillerchip/merge-test/compare/first_branch...second_branch – Adam Millerchip Oct 11 '17 at 03:37
  • 18
    Is there a **dear-github** request to get this changed? – vossad01 Mar 16 '18 at 18:24
  • 2
    When using the workaround @hexsprite described above make sure to pick a branch to switch to that has history in common with your current branch otherwise Github will automatically close the PR and will not let you reopen it because "The XX branch has no history in common with YY" – Lars Francke May 20 '19 at 22:14
  • 1
    This question is 6 years old. Has there been any upgrades on the github side, that anyone knows, to change this behavior? This happens when there are several PRs that are descendants of each other and the reviewer hasn't merged any of them yet. Merging them in chronological order does not update the PR diffs of the child branches. – JavNoor Jun 14 '19 at 15:20
  • 2
    I'm disappointed that this is unresolved after 6 years. I've opened a ticket with Github Support requesting a status update (Ticket ID: 398652), and I encourage anyone else who hopes to see this resolved to continue doing the same - politely and professionally. It makes me sad to suggest that as someone who's worked in Tech Support but here we are. – Jeff Welling Sep 23 '19 at 18:14
  • 3
    I rebased the branch with other branch and after that the changes are still being shown when opening another PR. Anyone? – BartusZak Mar 02 '20 at 13:43
74

To sum up, GitHub does not rebase the commit history automatically in pull requests. The simplest solutions are:

Solution 1: Rebase

Suppose that you want to merge into master from feature-01:

git fetch origin
git checkout feature-01
git rebase origin/master
git push --force-with-lease

If you are working on a fork then you might need to replace origin above with upstream. See How do I update a GitHub forked repository? to learn more about tracking remote branches of the original repository.

Solution 2: Create a new pull request

Suppose that you want to merge intro master from feature-01:

git checkout feature-01
git checkout -b feature-01-rebased
git push -u origin feature-01-rebased

Now open a pull request for feature-01-rebased and close the one for feature-01.

Mateusz Piotrowski
  • 6,866
  • 9
  • 49
  • 75
  • 5
    A rebase changes the commit hashes, so would it end up trashing the existing review comments? – haridsv Nov 14 '18 at 16:59
  • @haridsv Probably yes. – Mateusz Piotrowski Nov 14 '18 at 20:57
  • 1
    Anything to watch out for when doing push --force? – Paul Bendevis Aug 19 '19 at 12:29
  • 1
    @haridsv that's not my experience. I frequently rebase mid-review and comments aren't lost. Though I do lose the history of changes in the PR – joel Aug 22 '19 at 13:01
  • @JoelBerkeley I actually experienced a couple of reviews meanwhile in which the author rebased and observed that the comments are in tact. However, we lose the ability to review incrementally and for large reviews it is a huge penalty on the reviewers. We now have a few guidelines for our PRs and one of those is never to rebase after the review is open (unless one is sure that nobody started reviewing yet). Merging is fine but our guideline is to not mix the merge change with any other changes other than the conflict resolutions. – haridsv Aug 23 '19 at 08:52
  • regarding things to watch out for when doing --force, watch out that other commits from someone else are not on that branch (the remote branch) as you will likely end up removing them if you don't have those same commits locally. --force-with-lease is safer in the case that multiple people are commiting to the same feature branch – ChristianF Jul 28 '20 at 01:16
  • 2
    Or instead of --force use --force-with-lease as I think this will bail out safely if someone has altered your branch in the repo. – bailey86 Jan 19 '22 at 16:47
29

This happens with GitHub when you squash commits merged in from the target branch.

I had been using squash and merge with Github as the default merge strategy, including merges from the target branch. This introduces a new commit and GitHub doesn't recognize that this squashed commit is the same as the ones already in master (but with different hashes). Git handles it properly but you see all the changes again in GitHub, making it annoying to review. The solution is to do a regular merge of these pulled in upstream commits instead of a squash and merge. When you want to merge in another branch into yours as a dependency, git merge --squash and revert that single commit before pulling from master once that other branch has actually made it to master.

EDIT: another solution is to rebase and force push. Clean but rewritten history

achille
  • 441
  • 4
  • 9
27

For anyone else coming across this and confused by GitHub Pull Request behavior, the root cause is that a PR is a diff of the source branch tip against the common ancestor of the source branch and the target branch. It will therefore show all changes on the source branch up to the common ancestor and will not take into account any changes that may have occurred on the target branch.

More information available here: https://developer.atlassian.com/blog/2015/01/a-better-pull-request/

Common ancestor based diffs seem dangerous. I wish GitHub had an option to make a more standard 3-way merge-based PR.

David K. Hess
  • 15,632
  • 2
  • 45
  • 66
  • 2
    The reason you mentioned seems correct, but I am confused because generally whenever the master gets updated, i back-merge the changes to my branch ... **git checkout my-branch -> git merge master**. The pull request gets refreshed immediately. Does the common ancestor also get updated? – G.One Dec 30 '16 at 15:23
  • 2
    This behavior seems to occur when doing rebase instead of merge – G.One Dec 30 '16 at 15:28
  • 3
    @G.One, really late reply, but yes – if you merge from that master branch to your source branch, you've by definition updated the common ancestor. It's the commit on master you merged from. – David K. Hess May 01 '19 at 16:15
23

You need to add the following to your ~/.gitconfig file:

[rebase]
    autosquash = true

This will automatically achieve the same as what this answer shows.

I got this from here.

Mateusz Piotrowski
  • 6,866
  • 9
  • 49
  • 75
Elena
  • 1,441
  • 17
  • 23
  • 3
    @RonaldRey git rebasing is not a universal solution, because it involves editing the history. If everybody is working on private forks that are never cloned by others, then it's fine, but as soon as somebody clones your repository and you then edit the history, you end up with different histories. – Adam Millerchip Mar 03 '18 at 03:02
14

One way to fix this is to git rebase targetbranch in that PR. Then git push --force targetbranch, then Github will show the right commits and diff. Be careful with this if you don't know what you are doing. Maybe checkout a test branch first to do the rebase then git diff targetbranch to make sure it is still what you want.

Elijah Lynn
  • 11,153
  • 10
  • 55
  • 80
  • 1
    Or instead of --force use --force-with-lease as I think this will bail out safely if someone has altered your branch in the repo. – bailey86 Jan 19 '22 at 16:49
8

Instead of the 3-dot url use 2-dot url to compare

Instead of

http://githuburl/org/repo/compare/targetbranch...currentbranch

Use

http://githuburl/org/repo/compare/targetbranch..currentbranch
user3755282
  • 723
  • 2
  • 8
  • 13
4

What I tried and why it's happening?

None of the solutions worked for me. When I used two dots i.e. .. instead of ... the diff on GH was much closer to what I changed. But still not exactly all my changes.

This happens because a problem in how GitHub shows squash merged changes. Best explained here

It basically happens if:

  1. Push up changes of featureBranch
  2. Squash merge it into main
  3. Locally while I'm still on featureBranch I merge it with main. Make more changes and push it up again.
  4. But then on GitHub I see more changes than I expect.

It's worth noting that this isn't a git problem. Instead it's a GitHub problem. GitHub can't tell that the squashed commit is identical the sum of the non-squash commits and causes an extra diff.


Solution

On your local main, undo and stash all changes that haven't been merged with main. To do that I did. Example if I had 4 commits that weren't in my PR that got squash merged then I'd do:

  • git reset HEAD~4
  • git stash save "last 4 commits"

Then create a new branch with what you've just stashed. Steps:

  • git checkout main
  • git checkout -b newBranch
  • git stash apply
  • git add --all
  • git commit -m "some message"
  • git push
mfaani
  • 28,843
  • 15
  • 145
  • 252
0

I'm not exactly sure about the theory behind this. But I got this several times and able to fix this by doing the following.

git pull --rebase

This will fetch and merge the changes from your original repo master branch (If you have point to that)

Then you push your changes forcefully to your github cloned repository (target)

git push -f origin master

This will make sure your github clone and the your parent repo are at the same github commit level and you don't see any unnecessary changes across branches.

Chanaka udaya
  • 4,644
  • 4
  • 24
  • 33
0

I found a way to get the right behavior (tested in November 2020).

After git merge and resolving conflicts need to use git merge --continue instead of git commit ....

iKBAHT
  • 586
  • 4
  • 16
0

The issue is not happening for me once i started doing the following before starting a new change or creating a PR.

git pull --rebase origin <target-branch>

This basically ensures that whatever new changes are added from the local is stacked on top of what is there currently in the remote branch. Hence our local branch is always on top of the current remote head and only the new commits are there in the PR.

Arun Sasi
  • 156
  • 12
-1

Fail safe approach if you are too worried about messing things up: go to the file and manually remove the changes, then squash with your last commit using

git add .  && git commit -a --allow-empty-message -m '' && git reset --soft HEAD~2 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

I there are no conflicts, you are good to go!

Ishan Srivastava
  • 1,042
  • 1
  • 10
  • 28