Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would a simple `git mv` fail?

Tags:

git

I am trying to rename a file in a local .git directory:

git mv MyProj/src/ts0621/foobar.c NewProjName/src/ts0629/foobar.c

Yet, git fails with:

fatal: renaming 'MyProj/src/ts0621/foobar.c' failed: No such file or directory

I could swear that I had git mv working for me when renaming a subdirectory, but for some reason a simple rename of a file doesn't work. Why?

(I, of course, verified that the file MyProj/src/ts0621/foobar.c exists before attempting to issue this command)

like image 952
WinWin Avatar asked Jul 11 '11 19:07

WinWin


People also ask

What does mv mean in git?

git-mv - Move or rename a file, a directory, or a symlink.

Does git mv do anything?

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.

Is there a git mv?

Git move or mv helps us to rename or move files within a git repository without deleting its history. If we move or rename a file within a git repository then it thinks that a file is deleted and a new file is added.

How do I rename a directory in git without losing history?

No. The short answer is NO. It is not possible to rename a file in Git and remember the history.


2 Answers

Probably, because NewProjName/src/ts0629 doesn't exist

Try

mkdir -pv NewProjName/src/ts0629
git add NewProjName/src/ts0629
like image 51
sehe Avatar answered Sep 20 '22 13:09

sehe


add -f , if the path is relative, to increase git mv -f "originPath" "newPath"

git mv -f MyProj/src/ts0621/foobar.c NewProjName/src/ts0629/foobar.c
like image 39
iHTCboy Avatar answered Sep 21 '22 13:09

iHTCboy