Two months ago, I wrote my first list of Git commands, and said that I didn’t know how to use Git commands to view the changes.1
Now, I can understand how one can “use ‘--
’ to separate paths from
revisions [or branches]”.
For example, if a developer relies on this Git cheatsheet for blogging with Octopress, then he/she will learn some Git commands, for example:
git diff <branch>
to view the uncommitted changes;git diff <path>
to show the uncommitted changes in files under<path>
.
Those commands should be enough for most cases. However, if he/she blogs with Octopress, then he/she will encounter the some problems:
git diff source
can’t view the uncommitted changes onsource
branch; (Click the linked post in footnote 1 for the error thrown by Git.)git diff source
can’t show the uncommitted changes in files undersource
folder.
In order to use git diff
to do the intended task, one has to avoid
ambiguity.
- If necessary, one can use
--
to separate branch name(s) from file/path names; - One can use
./source
to meansource
folder.
$ git diff origin/source source
fatal: ambiguous argument 'source': both revision and filename
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ git diff origin/source source -- # correct command
$ git diff ./source # correct command
If one doesn’t want to type in shell commands, one may consider
using fugitive.vim: in a window invoked by :Gst
, press D
at the
line where the modified file is shown.
-
Tam, V. 2014, Jun 16. My Git Command List (1). Retrieved from https://vincenttam.github.io/blog/2014/06/16/my-git-command-list-1/#comparing-branches ↩