7

When working inside a windows command prompt, all of my paths indicate director separators with a backslash \, when using GIT commands, all of the paths are instead using forwardslash /. How do I change GIT's output to mirror my command line output?

Example inconsistent directory indicators;

D:\git\demo>git status --s
A  test/subdir/foo.txt
Peter Nied
  • 1,820
  • 1
  • 13
  • 22
  • Adding the tools within git\usr\bin to my path and using them from the command line is a work around, but I still run into problems when I attempt to copy paths from the command line into other windows application's open file dialog. – Peter Nied Jan 06 '17 at 18:33
  • Why do you need to change it? git bash is unix while cmd is windows. – CodeWizard Jan 07 '17 at 02:41
  • windows commands such as erase or rmdir do not work with '/' delimited file paths – Peter Nied Jan 19 '17 at 18:22
  • - I want to use the windows command line – Peter Nied Jan 19 '17 at 18:25
  • Most Windows commands are quite okay with slash-delimited paths, just as long as they don't *begin* with a slash. The underlying APIs are fully compatible with forward slashes. I recommend using forward slashes, and then seeking help the other way ("Such-and-such can't use file names like test/subdir/foo.txt - how can I work around this?"). Otherwise you're going to spend all your time fighting against git and bash. – rosuav Feb 07 '17 at 11:11

2 Answers2

3

How do I change GIT's output to mirror my command line output?

First, all git commands are executed in a git bash sub-shell, which explains why you see '/'. A '\' is an escape character for a bash session, which is why it is not used in any bash command output.

You would need to use a git wrapper (a git.pat set in your PATH) in order to replace any / by \.

git.bat:

C:\prgs\git\latest\bin\git.exe %*|C:\prgs\git\latest\usr\bin\sed.exe -e 's:/:\\\\:'

Make sure git.bat is set before git.exe in your %PATH%: type where git to check the order in which git(s) are discovered.
And replace C:\prgs\git\latest by the path your Git is installed.

By specifying the full path for git.exe and for sed.exe, you are sure to use the right executable.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Just to nitpick, I don't believe git runs everything in bash -- it's just that git.exe is built against mingw. – Josh Lee Feb 01 '17 at 20:26
  • Coloration is lost, and since it is just a regex, it matches any `/` in the output on non-path elements :( – Peter Nied Feb 03 '17 at 00:43
  • @PeterNied Yes, it is a workaround: Git remains at its core a Linux tool. – VonC Feb 03 '17 at 05:12
  • @PeterNied Do you have a specific issue which would require for you to get Git output with \ instead of /? – VonC Feb 03 '17 at 19:46
  • @VonC windows commands such as erase or rmdir do not work with '/' delimited file paths, which I would like to be taking from the ouput of git log --stat or git status – Peter Nied Feb 03 '17 at 22:36
  • @PeterNied Then I would advise to put first in your `PATH` the `usr\bin` folder: it include `rm` (or `rm -Rf` which does work nicely with Unix-style paths. Plus, even in a regular CMD session, a `git-xxx` script will be executed in a git bash session when called as `git xxx`. Ie, you can execute bash scripts from your CMD shell. – VonC Feb 03 '17 at 23:23
  • @VonC, thanks for attempting to answer my question, it sounds like git just doesn't have this level of flexibility, and if I want to workaround the issue, I am going to be creating more alternative problems for myself. - I might see if I can submit a PR to add this flexibility for full stack windows users that like the git command line – Peter Nied Feb 06 '17 at 19:19
  • @PeterNied I agree, but again, you can use bash script. – VonC Feb 06 '17 at 19:56
1

Since what you're looking for seems to be not specifically "how do I make Git use \ in file paths" but rather "how do I make Git generate file paths with \", you can pipe the output through sed (which is packaged in Git Bash) like so:

$ git status --s | sed 's/\//\\/g'
 M dir\file.py
?? dir\input001.txt
?? dir\output001.txt

and to avoid typing sed every single time you can configure a Git alias to do it for you:

[alias]
    ws = "!ws() { : git status ; git status --short $@ | sed 's/\\//\\\\/g' ; } && ws"

which will let you do this:

$ git ws
 M dir\file.py
?? dir\input001.txt
?? dir\output001.txt
Pockets
  • 1,186
  • 7
  • 12