Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code 'git mv' to preserve file history?

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

like image 501
Ryan Loggerythm Avatar asked Aug 04 '20 23:08

Ryan Loggerythm


People also ask

Does Vscode have file history?

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.

How do I rename a directory in git without losing history?

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.

How to push files without preserving history in Git?

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:

Is it possible to rename a file in Git and remember history?

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.

Should I use Git MV or MV for renames?

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.

Does 'git log file name' add files to git log history?

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.


Video Answer


3 Answers

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.

like image 60
CherryDT Avatar answered Oct 11 '22 01:10

CherryDT


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.

like image 23
DLight Avatar answered Oct 11 '22 03:10

DLight


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.

like image 23
Jeroen Wiert Pluimers Avatar answered Oct 11 '22 03:10

Jeroen Wiert Pluimers