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
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
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.). (Source)
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.
I believe this is the correct answer (probably a feature VSCode added since previous versions). In Source Control panel, after you rename, open File History panel. Click the three dots ..., Toggle Follow Renames should be set to On.
I also used the command line to rename, stage, and commit (instead of clicking the VSCode UI) but I am thinking that wasn't actually necessary.