Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git print paths with back slashes instead of forward slashes in a Windows console

Tags:

git

windows

Is it possible to make git print back slashes instead of forward slashes when it prints out paths in a Windows console? I'd like to be able to copy the paths that it outputs and paste it into my future console commands.

Example output:

C:\vna>git status
On branch herpderp
Your branch is up-to-date with 'origin/herpyderp'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    Java/HerpDerp/src
    Java/HerpDerp/src/main/java/com/derp/messaging/
    Java/HerpDerp/src/main/java/com/derp/controller/event/
    Java/HerpDerp/src/main/java/com/derp/controller/domain/
    Java/HerpDerp/src/main/java/com/derp/controller/monitor/


nothing added to commit but untracked files present (use "git add" to track)

Instead of

Java/HerpDerp/src/main/java/com/derp/messaging/

I'd like to see

Java\HerpDerp\src\main\java\com\derp\messaging\

or even

Java\\HerpDerp\\src\\main\\java\\com\\derp\\messaging\\

Edit: I should have clarified that this was for working with git in a Windows console. Also, it seems the correct answer for my case is to use Git Bash.

like image 659
Lance Clark Avatar asked Jan 16 '17 18:01

Lance Clark


People also ask

How do I change backslash to forward slash in Windows?

Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.

Does Windows use backslash for paths?

Windows uses backslashes for paths, while everything else seems to use forward slashes. Modern software tries to automatically correct you when you type the wrong type of slash, so it doesn't matter which type of slash you use most of the time.

What is the difference between forward slash and backslash in file path?

If I recall correctly, backslash is used to denote something within the current computer or network such as C:\Windows or \\172.12. 1.34, while forward slashes are used to denote something that is outside or external to the current computer or network such as http://www.google.com/.


1 Answers

I know it's a little old, but you can use environment variable substitution to replace the / with \ or \\:

C:\> SET FILE=Java/HerpDerp/src
C:\> ECHO %FILE:/=\%
Java\HerpDerp\src

C:\> ECHO %FILE:/=\\%
Java\\HerpDerp\\src

C:\>

To take it a step further, you can write a batch file that does it for a command that outputs a bunch of paths: (git diff --name-only HEAD~1 in the following example)

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq delims=" %%i IN (`git diff --name-only HEAD~1`) DO (
  SET FILE=%%i
  ECHO !FILE:/=\!
)
ENDLOCAL
like image 135
aspoon Avatar answered Oct 07 '22 23:10

aspoon