I often rename files with the mv
system command, forgetting to use git mv
in order to keep the file history:
~/gitproj $ mv myfile.foo newname.foo
When I realize it before a commit, the only way I know to fix it is to do the following:
~/gitproj $ mv newname.foo myfile.foo # Coming back to the old name
~/gitproj $ git mv myfile.foo newname.foo # Moving forth again, but with git
Is there a better way to do it, like only giving to git
the info that the file was moved?
It would be something like that:
~/gitproj $ git mv --cached myfile.foo newname.foo # a pseudo git command
As AndreasGrapentin wrote:
git add .
let git (auto)detect all the files which were renamed, even if they were modified. But this command adds the whole directory to the index...
Is there a way, even in a lower level, to change this info only for one file?
Since the effect of git mv
is nothing more or less than the sequence
mv myfile.foo newname.foo
git rm --cached myfile.foo
git add newname.foo
anyway, you can simply supply the remaining two operations
git rm --cached myfile.foo
git add newname.foo
to finish the move. However, that does not save you a single command from your move-back-then-git-move workaround.
Afaik, these two commands cannot be bundled via a git alias: You need to supply two names to two different commands, and that is incompatible with alias substitution. So, if you absolutely want to be able to do this with a single command, you either need to write a shell script or function. With a shell script, you can then alias it in your ~/.gitconfig
if you absolutely want to call it as a git command, but I really wouldn't bother if I were you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.