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:./README
A 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.