When using git add -u I can provide a path as the last argument, e.g. git add -u .. How does one give the repositories root path?
I would like to use this to quickly add all changes to tracked files to the index, including changes to files further up the directory tree.
Example
$ git status # On branch mul # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: ../../../foo.mwb # no changes added to commit (use "git add" and/or "git commit -a")
The colon : helps here, you can refer to the root of the repository by :/:
git init test
cd test
mkdir a
touch a/a
git add a
git commit -m a
# Here comes the interesting part:
cd a
touch ../root
git add :/
git commit -m root
The script above initializes a toy repository that contains one single file a in a subdirectory a. In "the interesting part", it changes to the a directory, creates an empty file in the root of the repository and adds it using the :/ reference. The -u switch works as usual (not shown in the script).
The manual of git-rev-parse reads:
<rev>:<path>, e.g.HEAD:README,:README,master:./READMEA suffix
:followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon.:path(with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path. A path starting with./or../is relative to the current working directory. The given path will be converted to be relative to the working tree’s root directory. This is most useful to address a blob or tree from a commit or tree that has the same tree structure as the working tree.
Unfortunately, the bash autocompletion feature doesn't work for the :/ construct.
This related question asks about finding the absolute path of the repository; of course, you can also do git add $(git rev-parse --show-toplevel) as outlined in the top-voted answer.
Related question: What does "git add -A :/" do?
The recent wording of the documentation of git-add says: git add <options> <pathspec>.... And :/ in the context of git-add is a pathspec. pathspec is documented in gitglossary(7), where it is described that : has a special meaning as the first character of a pathspec. See my answer for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With