The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,266 other subscribers

For vscode: git-rename – Visual Studio Marketplace

Posted by jpluimers on 2024/03/26

In vscode, I have installed [Wayback/Archive] git-rename – Visual Studio Marketplace (with source code at [Wayback/Archive] adam8810/vscode-git-rename: Move or rename a file, a directory, or a symlink using git-mv).

Many people assume that git does recognise rename (or mv) operations by itself. Often it does, but it fails, and when it fails it usually is in a complex situation where it is easy to overlook it did not recognise the rename.

Failing complex situations I have encountered in the past (combined they get worse):

  • rename across several directories
  • first edit, then rename
  • first rename, then edit

So it is better to proactively perform an IDE-assisted git mv operation that informs git of the rename.

Many IDE environments support a built-in rename that keeps git mv in the loop, but Visual Studio Code does not, hence the need for this extension.

It means I agree with the question, disagree with the answer, and agree with the comment in [Wayback/Archive] VS Code ‘git mv’ to preserve file history? – Stack Overflow:

Q

(by [Wayback/Archive] Ryan Loggerythm)

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

A

(by [Wayback/Archive] CherryDT) which I disagree with

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.

C

(by [Wayback/Archive] nicolas-van)

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.

Both via [Wayback/Archive] vscode git rename file – Google Search.

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.