Git – Programmatic git status


Does anyone know of a git plumbing (definitely not porcelain) means of determining whether:

  • there are edits in the repo since the last commit, and
  • whether local HEAD is ahead of origin/HEAD

I am looking to determine this programmatically, hence the desire to not solve this with porcelain and various sed-fu.

Best Solution

Update: as mentioned below by toupeira, you can use the --porcelain option of git status (since commit 6f15787, September 2009, git 1.7.0).

I mentioned in my answer "What does the term porcelain mean in Git?" that:

Perhaps the meaning of --porcelain here is "produce output suitable for consumption by porcelain scripts"

However, that won't show the ahead/behind information: see "What to add to “git status --porcelain” to make it behave like “git status”?": for that, you would still need to use other commands: see "How to know if git repository has changes that have not been synchronized with server?"

Initial answer March 2009

In porcelain command, a:

$ git diff HEAD

gives you the changes since the last commit (what you would be committing if you run "git commit -a").

A possible equivalent in plumbing command would be:

$ git ls-files -m

for listing all modified (working directory or index) files

If you create your repository by cloning someone else's repository, the remote "master" branch is copied to a local branch named "origin". You get your own "master" branch which is not tied to the remote repository.

There is always a current head, known as HEAD. (This is actually a symbolic link, .git/HEAD, to a file like refs/heads/master.)

run "git status" and analyze the output:

# On branch master
# Your branch is ahead of 'origin/master' by 11 commits.

More details in the SO question "Why is Git telling me “Your branch is ahead of ‘origin/master’ by 11 commits.” and how do I get it to stop?"

Possible equivalent in plumbing command:

* git-for-each-ref

for listing all commits, but requires analyzing the output as well...

Again, git ls-files could be used to produced the same result than a git status.

git ls-files --exclude-per-directory=.gitignore --exclude-from=.git/info/exclude \
                    --others \
                    --modified \