31

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

0

4 Answers 4

30

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.

11
  • 5
    when I run git status, I see "Changes not staged for commit:..." "deleted: {old name}" and under the untracked file, I see the newly renamed file Aug 4, 2020 at 23:33
  • 15
    Yes but once you stage them, it'll look differently
    – CherryDT
    Aug 4, 2020 at 23:34
  • 4
    What if you move them to another folder?
    – Corey Alix
    Sep 6, 2020 at 2:46
  • 4
    I'm not sure what you are referring to... It works fine, just keep in mind that you have to have it staged under both the old and new name/location before it can be seen as rename action (because the index is based on filenames and until the file is also added to the index under the new name, what you get is a deletion of a file that's indexed, and an untracked new file not yet in index).
    – CherryDT
    Jan 18, 2021 at 8:51
  • 17
    There is a reason to ask for an automatic git mv when moving a file through an IDE. git will detect movements in "most cases", but not all. Typical case is when you when a file AND modify it in a consequent amount in the same commit. git can clearly fail to detect the movement in these cases. Unfortunately these cases are not that rare. That's why multiple other IDEs (notably IntelliJ and Netbeans, according to my experience) explicitly perform a git mv when moving or renaming files. Then it is perfectly clear to git that the file was moved, there's no possible ambiguity. Jun 1, 2021 at 12:35
8

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.

3
  • Not true. "git mv" clearly does more than the attempt and automatically detecting renamed/moved files. "git mv" will lead to a reliable file history where as automatic detection does not.
    – Christoph
    Nov 27, 2022 at 22:06
  • 1
    @Christoph I don't think so, please see the Git FAQ: "Git has a rename command git mv, but that is just for convenience. The effect is indistinguishable from removing the file and adding another with different name and the same content."
    – DLight
    Dec 3, 2022 at 10:39
  • Hmm, unfortunately you seem to be right. My TortoiseGit client does show the moves done with "mv" in the commit dialog. That's why I thought Git was aware of them. But once committed, the history does not indicate the file move if file name AND content changed too much. Very unfortunate...
    – Christoph
    Dec 5, 2022 at 22:33
5

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.

1
  • 2
    What a time saver. I tried to switch the F2 "rename" binding to this command but unfortunately that didn't work. VS Code would show an error message, something about a theme.
    – Christoph
    Nov 27, 2022 at 22:11
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.