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 
5
  • 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. Commented Jan 6, 2017 at 18:33
  • Why do you need to change it? git bash is unix while cmd is windows. Commented Jan 7, 2017 at 2:41
  • windows commands such as erase or rmdir do not work with '/' delimited file paths Commented Jan 19, 2017 at 18:22
  • - I want to use the windows command line Commented Jan 19, 2017 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. Commented Feb 7, 2017 at 11:11

2 Answers 2

3
+50

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Just to nitpick, I don't believe git runs everything in bash -- it's just that git.exe is built against mingw.
Coloration is lost, and since it is just a regex, it matches any / in the output on non-path elements :(
@PeterNied Yes, it is a workaround: Git remains at its core a Linux tool.
@PeterNied Do you have a specific issue which would require for you to get Git output with \ instead of /?
@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
|
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.