Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename file for all commits in a Git repository

Tags:

I want to rename a file for all the commits in Git repository. Here's what I have tried:

git filter-branch --index-filter 'git mv -k <old name> <new name>' HEAD

This command went through all the commits in the repository, but it ended up with the message:

WARNING: Ref 'refs/heads/master' is unchanged

which means nothing had been changed. What had been wrong here?

Note that the file which I wanted to rename doesn't exist from the first commit. Therefore if I do not use -k in git mv, I mean if I use:

git filter-branch --index-filter 'git mv <old name> <new name>' HEAD`

Git would error out while trying the first commit saying something like "bad source...".

like image 535
York Avatar asked Feb 25 '12 18:02

York


People also ask

Can we rename a file in git repository?

You can rename any file in your repository directly in GitHub or by using the command line.

Which command will rename a file and prepares it for commit?

We use the git mv command in git to rename and move files.

Which command is used to rename a file in git?

To rename any file or folder, use git mv command which takes two arguments. The first argument is the source and the second is the destination. We can easily rename any file using the git command and the new name will be assigned to that file.

How does git handle file rename?

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).


1 Answers

I finally solved my original problem by using:

git filter-branch --tree-filter '
if [ -f <old name> ]; then
  mv <old name> <new name>
fi' --force HEAD
like image 89
York Avatar answered Sep 28 '22 07:09

York