Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if git repository has changes that have not been synchronized with server (origin)?

Tags:

git

scripting

cvs

I have a large number of projects setup in Git that were previously managed in CVS. Tortoise CVS as well as Eclipse both made it very easy to see (via icon overlays) if I had made changes to the repository that had not yet been sent to the central server.

Is there a convenient way to achieve this with Git? I don't really need the icon overlays -- I just need to know if I have outstanding changes when comparing my branches to those in origin. I don't mind using a script of some kind to query all the Git repos.

like image 754
Andy Avatar asked May 25 '11 15:05

Andy


2 Answers

The following examples only deal with a single repository. Use a loop in a surrounding script to run them on multiple repositories.

You will probably want to run (e.g.) git fetch origin to update your local remote-tracking branches before using the following commands (so that you are checking against the most up-to-date tips of the upstream branches).

If you are only concerned with the branch that is currently checked out:

git status -sb

If the first line reports “ahead”, then the current branch has commits that are not part of its upstream branch. The output of this command is not suitable for consumption directly by a program (it is a “porcelain” command). For programatic consumption use a “plumbing” command to list the “ahead” commits:

git rev-list HEAD@{upstream}..HEAD

You can pipe that to wc -l to get a count. If you are only interested in the “ahead” status (not the exact count), then test -n "$(git rev-list -n 1 HEAD@{upstream}..HEAD)" may be faster.

If you want to check on all the branches of a repository:

git branch -v

Again, you will see “ahead” for branches with commits that are not part of their upstream branch. This is also a “porcelain” command, so if you want to reliably detect the state in a program, then you will want to use some “plumbing” commands:

git for-each-ref --shell --format='
    b=%(refname:short)
    u=${b}'@{upstream}'
    if git rev-parse --verify --quiet "$u" >/dev/null 2>&1; then
        test -n "$(git rev-list -n 1 "$u..$b")" &&
        echo "$b: has unpushed commits"
    else
        echo "$b: no upstream configuration" >&2
    fi
' refs/heads | sh

Adjust the echo statements (or replace them with other commands) to suit your purposes.

like image 52
Chris Johnsen Avatar answered Oct 08 '22 07:10

Chris Johnsen


Something like git log origin/master..master should give you the commits that you have done and not pushed. If you fetch origin and then do git log master..origin/master you can see commits in remote master. You can also do git log origin/master...master to see new commits both locally and remote.

You can replace git log with git rev-list and easily figure out if and what is not pushed in a script if needed.

like image 40
manojlds Avatar answered Oct 08 '22 09:10

manojlds