Reputation: 37799
I want a command that will fail (with code 1) unless the HEAD points at master.
This kind of works:
git diff master --quiet || (echo 'Not master' && false)
But it's too strict – it also fails if there are any uncommitted changes in the working tree.
How can I assert that HEAD
points at master
, discounting any unstaged or uncommitted changes in the working tree?
Upvotes: 2
Views: 162
Reputation: 86
Considering that HEAD can point directly to a commit (detached head) or point to some branch that happens to match with the content of commit at the tip of master, comparing it using diff will not be 100% accurate. On the other hand, the HEAD is just a file containing a symbolic reference, according to docs, which could be compared directly.
test "$(cat "$(git rev-parse --git-path HEAD)")" == "ref: refs/heads/master"
Or
test "$(cat "$(git rev-parse --git-path HEAD)")" == "ref: refs/heads/master" && echo "HEAD points to master" || echo "HEAD does not point to master"
Upvotes: 5
Reputation: 12017
You can compare with HEAD
:
git diff HEAD master --quiet || (echo 'Not master' && false)
If you want to allow empty commits:
[ $(git show --oneline HEAD..master master..HEAD | wc -l) -eq 0] || (echo 'Not master' && false)
Or:
[ "$(git rev-parse HEAD)" = "$(git rev-parse master)" ] || (echo 'Not master' && false)
Upvotes: 1