Olivier Lacan
Olivier Lacan

Reputation: 2639

How can you test that your local git history matches a remote's

I'm often in this situation when I've forked an open source repository under olivierlacan/ossreponame.

I always set my own forked repo as the origin remote, and the canonical repository as the upstream remote.

Regularly, I run git pull --rebase upstream origin to update my local copy and push it back to olivierlacan/ossreponame on GitHub if necessary.

Yet sometimes, I have made some experimental changes on the master branch within my local fork (I know, it's bad, I should be using branches, but it happens). In doubt I often check git log for a recent commit authored by me and then I roll back these changes. But I always have a lingering feeling that I might be sneaking my stray commits by rebasing like that.

Is there a simple way to compare my git log history with the one on upstream/master in order to be certain they're not discrepant. Please feel free to tell me if this is a terrible idea.

Upvotes: 1

Views: 643

Answers (3)

bitmask
bitmask

Reputation: 34618

I have this alias in my ~/.gitconfig:

[alias]
    lg = log --stat --graph --decorate

I find --decorate very useful for quickly checking where the remote is compared to my local copy.

Upvotes: 0

Olivier Lacan
Olivier Lacan

Reputation: 2639

While Joseph K. Strauss presented an interesting path with git log diffs between origin/master and local master, there is a simple git command that covers my use case entirely: git cherry.

Its manual defines it as:

git-cherry - Find commits yet to be applied to upstream

I'm going to edit my question's title to be more specific since it turns out my question was poorly phrased and was already answered in Viewing Unpushed Git Commits.

Running git cherry -v will display any local commits on master that hasn't yet been pushed to origin/master. Technically it's not specific to master, so any branch with a set upstream remote branch tracking will work as well.

You can of course manually specify which branches you want to compare:

git cherry -v origin/master mybranch

I recommend reading the git help cherry manual. There's also a potentially useful limit option.

The output looks like this:

+ xaxf038fdab3eb626c7c3d38dfab00c59896ff96 Extract API controller methods into Base.

The + indicates that the commit does not have an equivalent on the upstream branch (origin/master). Any commit preceded by - would mean that it has an equivalent on the upstream branch.

Upvotes: 1

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4903

It sounds like you need to learn about git fetch. git pull does 2 things: It fetches the changes from the remote to your local remote tracking branch or to FETCH_HEAD, and then merges or rebases the remote changes into your local branch. Until you fetch you cannot really do any diffs.

What you should do is the following:

git fetch origin master
git log origin/master..master

The second command will show you all commits on your local master branch that are not in your remote branch.

If you want to see commits from the remote branch as well you can change the two dots to three, as in

git log origin/master...master

If you want to see the last commit that both branches share, as well, try

git log origin/master....master --boundary

Finally, when you are ready to bring in the remote changes, if any, just do

git rebase origin/master master

The second argument, master, is only necessary if you are not currently on the master branch.

Upvotes: 3

Related Questions