git ahead/behind info between master and branch?

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

I have created a branch for testing in my local repo (test-branch) which I pushed to Github.

If I go to my Github account and select this test-branch it shows the info:

This branch is 1 commit ahead and 2 commits behind master 

My questions are:

  1. How can I display this info locally (ie: a command that shows this on the terminal, rather than having to open Github to see it)?
  2. I know I can see the diffs between branches using:

    git diff master..test-branch 

    or using Meld (which I prefer):

    git difftool master..test-branch 

    but I was wondering if there's a way to see the ahead and behind commits separately. I.E.: is there a way to show that 1 commit ahead by itself and then those 2 commits behind by themselves?

回答1:

Here's a trick I found to compare two branches (any two) locally and show how much commits each branch is ahead of the other (a more general answer on your question 1):

git rev-list --left-right --count master...test-branch

which gives you output like

1 7

which means as much as: compared to master, test-branch is 7 commits ahead and 1 commit behind

You can also compare branches like origin/master...master to find out how many commits a branch is ahead/behind of its remote branch



回答2:

First of all to see how many revisions you are behind locally, you should do a git fetch to make sure you have the latest info from your remote.

The default output of git status tells you how many revisions you are ahead or behind, but usually I find this too verbose:

$ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 2 and 1 different commit each, respectively. # nothing to commit (working directory clean) 

I prefer git status -sb:

$ git status -sb ## master...origin/master [ahead 2, behind 1] 

In fact I alias this to simply git s, and this is the main command I use for checking status.

To see the diff in the "ahead revisions" of master, I can exclude the "behind revisions" from origin/master:

git diff master..origin/master^ 

To see the diff in the "behind revisions" of origin/master, I can exclude the "ahead revisions" from master:

git diff origin/master..master^^ 

If there are 5 revisions ahead or behind it might be easier to write like this:

git diff master..origin/master~5 git diff origin/master..master~5 

UPDATE

To see the ahead/behind revisions, the branch must be configured to track another branch. For me this is the default behavior when I clone a remote repository, and after I push a branch with git push -u remotename branchname. My version is 1.8.4.3, but it's been working like this as long as I remember.

As of version 1.8, you can set the tracking branch like this:

git branch --track test-branch 

As of version 1.7, the syntax was different:

git branch --set-upstream test-branch 


回答3:

With Git 2.5+, you now have another option to see ahead/behind for all branches which are configured to push to a branch.

git for-each-ref --format="%(push:track)" refs/heads 

See more at "Viewing Unpushed Git Commits"



回答4:

After doing a git fetch, you can run git status to show how many commits the local branch is ahead or behind of the remote version of the branch.

This won't show you how many commits it is ahead or behind of a different branch though. Your options are the full diff, looking at github, or using a solution like Vimhsa linked above: Git status over all repo's



回答5:

This may not answer your question directly, but why not using SourceTree



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