Home > Blockchain >  git diff A…HEAD plus index and unstaged files?
git diff A…HEAD plus index and unstaged files?

Time:10-20

How can i get the equivalent of git diff A...HEAD but also including all changes (staged and unstaged) from my local tree?

The various forms of git diff seem to be centered around one or the other, but not both.

CodePudding user response:

To git diff, A...HEAD means:

  1. run git merge-base --all A HEAD;
  2. take one of the output commit hash IDs at random;1
  3. run git diff hash HEAD.

The git diff that runs in step 3 completely ignores Git's index and your working tree.

Other forms of git diff can consult the index and/or the working tree, but the three-dot A...HEAD syntax never will.

If you want to compare the, or one of the, merge base commit(s) against the current working tree—i.e., staged and unstaged changes—you'll have to find the hash ID(s) of the merge base(s), pick one, and run git diff yourself. For instance:

git diff $(git merge-base A HEAD)

will do the job in any sh/bash compatible shell. The git merge-base command will pick one of the merge bases in the same sort of way (see footnote 1) and Git will then run the diff from that particular commit to the working tree.


1Technically it's not random, but rather algorithmic, but the algorithm is unspecified. Ideally, git merge-base here produces only one hash ID, so that there's no need to be concerned about the fact that it picks one of those at random. As long as there is only one hash ID in the set to choose from, it does not matter what choice the algorithm ends up using: you'll get just the one merge base commit.

Modern git diff notices if there are multiple merge bases and produces a warning message for this case, so that you'll know it picked a merge base on its own.

  •  Tags:  
  • git
  • Related