I've moved a file manually and then I've modified it. According to Git, it is a new file and a removed file. Is there any way to force Git into treating it as a file move?
|
Git will automatically detect the move/rename if your modification is not too severe. Just additionally, for moves around directories, you may need to:
If git status still shows "new file" and not "renamed" you need to follow Hank Gay’s advice and do the move and modify in two separate commits. |
|||||||||||||||||||||
|
|
Do the move and the modify in separate commits. |
|||||||||||||||||||||
|
|
It's all a perceptual thing. Git is generally rather good at recognising moves, because GIT is a content tracker All that really depends is how your "stat" displays it. The only difference here is the -M flag. git log --stat -M
git log --stat
git help log
|
|||||||||||||||||||||
|
|
to reduce it from the default 50% to 20%. |
|||||
|
|
This is a quick solution if you've renamed a file, made some changes to it, Git doesn't realize it's a rename, and you haven't committed your changes. Let's say the file was named
This last step is what gets your changed content back into |
|||||||||||||||||||||
|
|
If you're using TortoiseGit it's important to note that Git's automatic rename detection happens during commit but the fact that this is going to happen isn't always displayed by the software beforehand. I had moved two files to a different directory and performed some slight edits. I use TortoiseGit as my commit tool and the Changes made list showed the files being deleted and added, not moved. Running git status from the command line showed a similar situation. However after committing the files, they showed up as being renamed in the log. So the answer to your question is, as long as you haven't done anything too drastic, Git should pick up the rename automatically. Edit: Apparently if you add the new files and then do a git status from the command line, the rename should show up before committing. Edit 2: In addition, in TortoiseGit, add the new files in the commit dialog but don't commit them. Then if you go into the Show Log command and look at the working directory, you'll see if Git has detected the rename before committing. The same question was raised here: https://tortoisegit.org/issue/1389 and has been logged as a bug to fix here: https://tortoisegit.org/issue/1440 It turns out it's a display issue with TortoiseGit's commit dialog and also kind of exists in git status if you haven't added the new files. |
|||||||||
|
|
If you're talking about |
|||||||||
|
|
For me it worked to stash save all the changes before the commit and pop them out again. This made git re-analyze the added / deleted files and it correctly marked them as moved. |
|||
|
|
|
There is a probably a better “command line” way to do this, and I know this is a hack, but I’ve never been able to find a good solution. Using TortoiseGIT: If you have a GIT commit where some file move operations are showing up as load of adds/deletes rather than renames, even though the files only have small changes, then do this:
The new commit will now properly show the file renames… which will help maintain proper file history. |
||||
|
|
old_file.txt, thengit mv old_file.txt new_file.txtis equivalent togit rm --cached old_file.txt,mv old_file.txt new_file.txt,git add new_file.txt. – Jarl Sep 4 '12 at 8:18git mvwill not add them to the cache, butgit addwill. I prefer to move the file back so that I can usegit mv, thengit add -pto review my change set. – dhardy Feb 10 '14 at 15:59