git

Rename file for all commits in a Git repository


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


Solution

  • 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