Why does `git update-index` not affect files not in the current directory?

103 Views Asked by At

I'm trying to use git update-index --again to re-add modified files to the index. This works fine, but only for files/directories in the current directory. Is this expected behavior for this command or is there any option to make it consider the whole repository?

2

There are 2 best solutions below

2
On BEST ANSWER

This appears (in my tests) to work just like any typical git command. That is, it defaults to the current working directory and subdirectories (recursively), and you can specify paths if you want something different. If that's not the behavior you're seeing, please clarify with more specifics and I can take another look at it.

So for example

# setup a repo with some files
git init
touch file1
touch file2
mkdir dir
touch dir/file3
touch dir/file4
git add .
git commit -m "1"

# stage changes for a couple of the files, and make some additional changes
echo foo >file1
git add file1
echo bar >> file1
echo foo > file2
cd dir
echo foo >> file3
git add file3
git echo bar >> file3
git echo foo > file4

# now try it...
git update-index --again

Becuse we're in the dir directory, the default (like with most git commands that take paths) is to default to the current directory. So only file3 is re-staged - not file1. But if we said

git update-index --again :/:

this will apply the command from the root of the worktree.

0
On

As a note, one way to run git commands from the top of the work tree is to add an alias that gets it done there:

git config --global alias.attop '!git'

and then you can git attop update-index --again, I also have !true; as an alias so I can git exec anycommand to run find or whatever at the top of the work tree.