13

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.

Paul Beckingham
  • 14,495
  • 5
  • 33
  • 67

2 Answers2

12

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 \
                    -t
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

git status now has a --porcelain argument for scripting purposes (as well as an alternative -z for machine parsing), this is preferred to git ls-files which doesn't show files added to the index.

toupeira
  • 283
  • 4
  • 7
  • From [the documentation for `--porcelain`](https://www.kernel.org/pub/software/scm/git/docs/git-status.html): "Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across git versions and regardless of user configuration." –  Jul 07 '13 at 23:45
  • It's not clear from the release notes about when the `--porcelain` flag was added, however. –  Jul 08 '13 at 00:03
  • 2
    I found it! I searched the Git log with `git log --grep "--porcelain" --date-order`, and they show that the flag was added in commit [`6f15787181a163e158c6fee1d79085b97692ac2f`](https://github.com/git/git/commit/6f15787181a163e158c6fee1d79085b97692ac2f) on September 5th, 2009. Incidentally, `git tag --contains 6f15787 | sort -V | less` reveals that this wasn't actually released in a stable version until v1.7.0, which was on [Febuary 12th, 2010](https://github.com/git/git/commit/e923eaeb901ff056421b9007adcbbce271caa7b6). –  Jul 08 '13 at 03:28