Why does HEAD~394 not work if I have 394 unpushed git commits?
I have the following alias:
unpushed== !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) &&
git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline
when I execute this with |wc -l
, it tells me that I have 394 unpushed commits.
I use this number to run git diff somecommitid HEAD~394
.
This fails with the following error:
fatal: ambiguous 开发者_StackOverflow社区argument 'HEAD~394': unknown revision or path not in the working tree.
Strangely, it works right up until the number 358. In another clone, I have 478 commits, and git commands including git show
work up until HEAD~411 and then fail. Any clues? I am using git 1.7.5.2 on debian linux.
The best explanation I can think of is that the ~
operator doesn't follow branches. It only includes the first parents. If you want to refer to the last pushed commit, just use origin/$GIT_CURRENT_BRANCH
.
If you want to see the diff between the remote branch and the common ancestor of the remote branch and your local branch (the common ancestor is the commit you were looking for with the HEAD~394 trick), you must obtain this common ancestor by using the merge-base subcommand. Then, you can see the diff using:
git diff $(git merge-base $GIT_CURRENT_BRANCH origin/$GIT_CURRENT_BRANCH) $GIT_CURRENT_BRANCH
The command will work in both cases when the branches diverges and when origin/$GIT_CURRENT_BRANCH is ancestor of $GIT_CURRENT_BRANCH
Also, if both branches diverges, you can see the diff of the "unpulled" changes by doing
git diff $(git merge-base $GIT_CURRENT_BRANCH origin/$GIT_CURRENT_BRANCH) origin/$GIT_CURRENT_BRANCH
精彩评论