git add all tracked files - also those in parent directory

4.7k views Asked by At

Lets say I have a git root folder mine_git, which has a subdirectory subdir there. So I've worked a bit, and I'm in subdir - git status lists all changed files:

subdir$ git status -uno
# On branch master
# ...
#
#   modified:   mysubdirfile.txt
#   modified:   ../a-main-file.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

K, so I want to add all of these tracked and modified files to staging area (or cache? index? not sure of the name), so I can commit afterwards; so I issue:

subdir$ git add -u

... and then I check again:

subdir$ git status -uno
# On branch master
# Changes to be committed:
#   ...
#
#   modified:   mysubdirfile.txt
#
# Changes not staged for commit:
#   ...
#
#   modified:   ../a-main-file.txt
#
# Untracked files not listed (use -u option to show untracked files)

So, only those files under my current location are git added, and not those in parent/sibling folders - even if those files are tracked by this git repository, and show up in git status!

And then I usually have to manually copy paste filenames, so as to do git add ../a-main-file.txt. Obviously this is a pain - so is there some command which will add all files listed by git status -uno, regardless of if they are located below the current level or not?

1

There are 1 answers

2
suvayu On BEST ANSWER

The latest version of git (2.4.3) should do this already. From man 1 git-add:

   -u, --update
       Update the index just where it already has an entry matching <pathspec>. 
       This removes as well as modifies index entries to match the working tree, 
       but adds no new files.

       If no <pathspec> is given when -u option is used, all tracked files in the entire
       working tree are updated (old versions of Git used to limit the update to the 
       current directory and its subdirectories).

That said, you could try something like git add -u -- ../relative/path/to/project/root. I don't have an older git version handy, so I could not test it.