Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Rename or move all files at once

Tags:

git

I want to rename all erb files in my Git project to haml.(like index.html.erb to index.html.haml)

If I rename each file, I have to type the following command more than thirty times.

$ git mv app/views/pages/index.html.erb app/views/pages/index.html.haml

I tried the command below, but it did not work.

$ git mv app/views/**/*.erb app/views/**/*.haml

usage: git mv [options] <source>... <destination>

    -n, --dry-run         dry run
    -f, --force           force move/rename even if target exists
    -k                    skip move/rename errors

How can I rename them at once?

like image 536
Junichi Ito Avatar asked Feb 05 '12 18:02

Junichi Ito


People also ask

What is the most efficient way to rename a file in a repository?

In your repository, browse to the file you want to rename. In the upper right corner of the file view, click to open the file editor. In the filename field, change the name of the file to the new filename you want. You can also update the contents of your file at the same time.

When should I use git mv?

We use the git mv command in git to rename and move files. We only use the command for convenience. It does not rename or move the actual file, but rather deletes the existing file and creates a new file with a new name or in another folder.

How does git rename work?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).


2 Answers

for i in $(find . -iname "*.erb"); do
    git mv "$i" "$(echo $i | rev | cut -d '.' -f 2- | rev).haml";
done

For each .erb file, git mv it to itself with the extension ".erb" replaced by ".haml".

like image 99
Borealid Avatar answered Oct 19 '22 22:10

Borealid


A one liner using powershell

Dir *.erb -Recurse | foreach { git mv $_.FullName $_.FullName.replace("erb", "haml")}
like image 38
chris31389 Avatar answered Oct 19 '22 21:10

chris31389