1

I have a git repository with a few files and subdirectories. If I have a terminal window open in one of these subdirectories and execute git ls-files, I get a list of the files within my current working directory. If I create the following alias and run it in the same directory:

[alias] lsAlias = "!f () { params=($GIT_PREFIX); cd \"${params[0]}\"; /home/user/some-script.sh; }; f" 

some-script.sh calls pwd and git ls-files. pwd Correctly reports the current directory as expected, but git ls-files is behaving as if I am calling the command from the root of the git repository. If I adjust the alias to cut out the shell script:

[alias] lsAlias = "!f () { params=($GIT_PREFIX); cd \"${params[0]}\"; pwd; git ls-files; }; f" 

I get the same behavior.

Here is the transcript of the second alias:

+ f + params=($GIT_PREFIX) + cd TestApp/Training/ + pwd + git ls-files 

This is running in git-bash on Windows 10.

How do I have git ls-files respect the current working directory?

5
  • 1
    Add set -x; at the front of the alias to log execution, and add the transcript to the question. Commented Jan 10, 2024 at 20:01
  • 1
    Mind, in general, params=($GIT_PREFIX) has a lot of undefined behavior in it, so I'd typically suggest avoiding it. See BashPitfalls #50 -- and it only has the behavior described there if it's actually executed with bash. git aliases use /bin/sh, not bash, not bash, so array support isn't there at all. Commented Jan 10, 2024 at 20:02
  • Interesting. I'm assuming you're on a distro where /bin/sh is provided by bash -- somewhere it was dash or ash you'd have a more explicit error from that code. Commented Jan 10, 2024 at 20:12
  • Anyhow -- declare -p "${!GIT_@}" >&2 to list the environment variables named starting with GIT_ set during the alias's execution. One of the results listed there will be responsible for the difference in behavior, so you can reverse that behavior change with an appropriate unset. Commented Jan 10, 2024 at 20:14
  • BTW, on MacOS with git 2.42.0, I can't reproduce the stated behavior -- I get ls-files output local to the cwd, not the repository root. Commented Jan 10, 2024 at 21:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.