3

Need to do this in a script. Thank you.

Alvin Cao
  • 557
  • 2
  • 5
  • 11
  • Possibly a duplicate of [this](http://stackoverflow.com/questions/1800783/compare-local-git-branch-with-remote-branch). – ajp15243 Mar 22 '13 at 01:56
  • [`git-wtf`](http://git-wt-commit.rubyforge.org/git-wtf) is a cool handy script which does this already. It is written in ruby. You could try to reverse-engineer it. – Tuxdude Mar 22 '13 at 02:02

4 Answers4

2

I use the following command (as an alias) to list all local commits not pushed to the remote repository.

log --oneline --decorate=short --graph --all --branches --not --remotes

This will need to be modified for use in a script, and there are probably plumbing commands that do the same thing, but I find it useful.

The key is in the last few arguments:

  • --all includes all refs in refs/ -- you may not want this if you are only wanting the commits for the current branch.
  • --branches includes all refs in refs/heads -- leave it off for current or specify a branch explicitly to limit the list of commits.
  • --not --remotes contains the magic -- making sure none of the commits listed are in refs/remotes.

Edit > It looks like the git rev-list ... plumbing command has similar arguments, but I have not tested it to know if it works the same. Plumbing commands are recommended for scripting, so have a look at it -- man-page for reference.

Hope this helps.

David Culp
  • 5,354
  • 3
  • 24
  • 32
1

If the situation is like this :

      /--a--b--c  myBranch [local branch]
     /
----x <- [closest common ancestor]
     \
      \--e--f  origin/myBranch [tracked remote]
  • git status will display a message myBranch..origin/myBranch [ahead 3, behind 2] - ahead 3 means your local branch is 3 commits ahead of the closest common ancestor, behind 2 means the remote branch is 2 commits ahead of the closest common ancestor
  • git log origin/myBranch..myBranch will display the log about commits a-b-c - it will display the log for the commits ranging from the common ancestor of the two branches up to myBranch
  • git log myBranch..origin/myBranch will display the log about commits e-f - it will display the log for the commits ranging from the common ancestor of the two branches up to origin/myBranch

You can add any option you like to these commands, e.g : git log --oneline --decorate origin/myBranch..myBranch

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

git status indicates how far the current branch is behind the tracked branch. You can run this in a script and parse the output. There might also be a plumbing command that gives you the information a little more directly.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

You can pull the latest change information using the 'git fetch' command and then if you do a 'git status' on a tracked branch, it should show you the number of commits that's not pushed to the server or how far the current branch is behind.

Using fetch command wouldn't apply the changes in you local branch,for this, you have to merge it using 'git merge origin/branch_name'.

ranendra
  • 2,492
  • 2
  • 19
  • 29