'Renaming a file in Git Bash on Windows

I'm new to Git and Linux/Bash. I have Git Bash installed on Windows 10. How do I rename a file using an absolute path?

For instance the Windows folder path is:

D:\GitHub\Datsville\datsville_inlined

The old file name is:

datsville_rev493_inlined_n_boxed_f.xmpd

The new file name is:

datsville_sf_rev493_inlined_n_boxed_f.xmpd

Bonus: How do I put multiple such commands in a script file? Thanks.



Solution 1:[1]

For a git repository, you can use git mv for renaming/moving files, but only within the same repository. Also avoid doing this on a folder which has been created using git submodule.

Another option is to create a bash script for rename/move files and then git add thay again to the repo. Alternatively, you can do this using windows cmd or create a batch file, just add path to git.exe to the path environment variable, if you haven't already done so.

Also, since you are new to using git, I recommend that you use TortoiseGit - this should help you understand what git is.

Solution 2:[2]

In Linux, the commands are case sensitive.

mv is the command you're looking for. It's the shorten for "move", but you can rename files. Like mv oldfilename newfilename. I think the main problem is that you can't access files on the C:, D: etc. drive. Linux has a different directory structure than Windows. There is a root directory (/), and everything else is mounted to a sub-directory. Like you can access your pendrive on /mnt/pendrive (just an example, does not work). On Git Bash, the Windows drives are mounted to /c, /d etc. So it you want to rename it, you can do like this:

mv /c/somepath/oldfilename /c/somepath/newfilename

Solution 3:[3]

Use mv command in windows

As mentioned in above answer, mv command can be used in windows as well to rename the filename. I tried rename and ren commands s suggested, but I was getting error: bash: ren: command not found.

Use below to change the filename:

mv filename new_filename

Solution 4:[4]

This does not use git bash but if you want to use the linux "rename" command you can download Windows Subsystem for Linux (WSL) 2 here https://docs.microsoft.com/en-us/windows/wsl/install

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mr NAE
Solution 2 CoderCharmander
Solution 3 Alisha Raju
Solution 4 Jake