1

As a result of developing in the same branch across two different devices with lots of pushes and pulls going between each as well as merges from master (where lots of other commits come in). I'm in the following state with the Git repo on my second device:

% git status
On branch user/selbie/amazingfeature
Your branch is ahead of 'origin/user/selbie/amazingfeature' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

The problem is I don't know what those two commits could possibly be. I had recently merged the branch with master on the other repo and pulled those changes down to the second device. I would have expected the repos to be in sync at this point.

Searching around Stack Overflow, most answers like this one suggest I should just type this:

% git diff origin/user/selbie/amazingfeature user/selbie/amazingfeature

But it doesn't print any results. How can I get the diffs or commits between the local and remote before doing a git push blindly?

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Without providing a general solution, in this case you can just use `git log --graph`. The difference will be the top commit plus its parent (one of its parents if it’s a merge). – Ry- May 22 '20 at 22:25
  • 2
    Try `git log origin/user/selbie/amazingfeature..user/selbie/amazingfeature` or `git log --graph --oneline`. My guess is that the second commit reverts the first, which would explain the lack of diff. – 0x5453 May 22 '20 at 22:26

1 Answers1

0

If we add back these details (which would require knowing more about them):

... developing in the same branch across two different devices with lots of pushes and pulls going between each ...

we might be able to come up with a narrower, less-general answer. But it's easier to just answer the general question.

What ... commits ... would get pushed [if I ran git push remote name]?

In order to figure this out, the first step is to make sure that your own local repository is truly up-to-date with respect to the named remote, which we can do with git fetch remote. This may do nothing at all, or it might update origin/name in our local repository.1 If it does update origin/name, we might have to re-count the commits, as it might not be two commits any more, but in any case we can now easily the set of commits that we have, that they don't, that this git push would send:

git log [options] origin/<name>..<name>

If the current branch is name and origin/name is the upstream, we can use git log @{u}.. for short here. Add --graph and/or --oneline to get Git to draw a graph of the commits we'd send.

As 0x5453 commented, if your git diff showed nothing (and if git fetch did not / does not update origin/user/selbie/amazingfeature), it's likely that you have two commits in which the second commit simply un-does whatever the first commit does.


1If we've configured things particularly oddly, it might update some other name. Let's assume we haven't done bizarre configurations, i.e., that Git will work the way it normally would. :-)

torek
  • 448,244
  • 59
  • 642
  • 775
  • After some more more pull/fetch/merge stuff on on both devices to insure that the feature repos have everything from origin master, I'm now 4 commits ahead, but the `git log` statement reveals they are all merges. If I do this: `git checkout master; git pull; git checkout featurebranch; git merge master`, that tends to add two commits related to merging. – selbie May 22 '20 at 22:46
  • 1
    `git pull` means *run `git fetch`, then run a second Git command*. The second command is `git merge` by default, and `git merge` will often add a merge commit. Hence doing two separate `git pull` commands can indeed add two merge commits. The only tricky part here is that `git merge` sometimes does nothing, and sometimes does something that is not a merge, so that there are three possibilities for each `git pull`. Well, that, and, you can *configure* or *run* `git pull` such that it uses a *different* second Git command. – torek May 22 '20 at 22:53
  • But that's why I talk about `git fetch` and not `git pull` here. :-) – torek May 22 '20 at 22:53