Is there any way in VS Code to change a file's name so that the file history is preserved in git, and it doesn't think the tracked file's been deleted?
I'm hoping for a GUI implementation of the command:
git mv oldName.abc newName.xyz
Thanks
Visual Studio Code allows us to check the history of navigated files in Navigation History lists. You can open this window from “Goto–> Navigation History” or by just simply pressing Ctrl + Tab. This will bring list of all previously navigated files with in Visual Studio Code.
emiller/git-mv-with-history. git utility to move/rename file or folder and retain history with it. # git-mv-with-history -- move/rename file or folder, with history. # Git has a rename command git mv, but that is just for convenience.
The easiest option is to push the files without preserving history. Based on the example in the graph above, add relevant remotes and fetch their current state: Now, move to a clean branch that is identical to projectX/master. Then, add your new files: You can repeat for more files and commit:
The short answer is NO. It is not possible to rename a file in Git and remember the history. And it is a pain. Rumor has it that git log --follow --find-copies-harder will work, but it does not work for me, even if there are zero changes to the file contents, and the moves have been made with git mv.
Git detects renames rather than persisting the operation with the commit, so whether you use git mv or mv doesn't matter. The log command takes a --follow argument that continues history before a rename operation, i.e., it searches for similar content using heuristics. I suspect this is a performance consideration.
It does add the files, however it doesn't update the history so that 'git log file name' shows the full history. It only shows the full history if you use the --follow option still.
There is no need for it. Just rename the file. Git will detect renames regardless of whether git mv
was used or not.
Try it: rename it in the normal way, stage the file under both the old and the new name (or just do git add .
) and then run git status
in the console, it should show up as rename and not as creation and deletion, so history is always preserved.
Git does not store the information that files are renamed. It detects it only when it does a diff (in git diff
/ status
/ log
/ etc.).
There is an option -M
to control the level of this rename detection.
-M[<n>], --find-renames[=<n>]
Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file’s size).
For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed. Without a % sign,
the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05
is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.
Try it with:
git status -M10%
For VS Code, you can use the GitLens extension, it provides an option to control this threshold, the setting is called similarityThreshold
.
gitlens.advanced.similarityThreshold Specifies the amount (percent) of similarity a deleted and added file pair must have to be considered a rename
Try to set it to 10
and check the history through GitLens, it should detect the file rename better.
Since git sometimes does not detect rename operations, I have installed this extension in Visual Studio Code which so far seems to work fine to rename files and doing a git mv
underneath:
https://marketplace.visualstudio.com/items?itemName=ambooth.git-rename
The cool thing from the README.md is that during rename a new directory location can be specified:
Usage
Right-click on a file in the File Explorer and choose "Rename (git-mv)". Using the input text field that appears, enter the new name and press Enter. Note: Directory location can also be altered by adjusting the path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With