1

I want to move a typescript file in VSCode such that:

  1. Git acknowledges the file continuity.
  2. VSCode updates the parent files' import statements.

I can achieve either, but not both.

Using git mv: VSCode doesn't update the parent files' import statements.

Using manual drag & drop: git regards this as a new file.

How can I do both?

Ryan Tipps
  • 53
  • 6
  • Does this answer your question? [Can VS Code automatically update JavaScript and TypeScript import paths on file rename/move?](https://stackoverflow.com/questions/43542247/can-vs-code-automatically-update-javascript-and-typescript-import-paths-on-file) – SwissCodeMen Sep 27 '22 at 21:26
  • @SwissCodeMen no, git regards this as a new file - i.e. as though i deleted the file and created a new one. – Ryan Tipps Sep 27 '22 at 21:37
  • Wait, nvm - it turns out git doesnt actually track file renames anyways, even when git status shows renamed: someFileName.ts -> newFileName.ts – Ryan Tipps Sep 27 '22 at 21:39
  • 1
    Does this answer your question? [VS Code 'git mv' to preserve file history?](https://stackoverflow.com/questions/63256354/vs-code-git-mv-to-preserve-file-history) – CherryDT Sep 30 '22 at 09:41

1 Answers1

2

After doing more research, it turns out git does not actually track file renames.

Compare:

After using git mv, your git status will return something like:

renamed: someFileName.ts -> newFileName.ts

After using mv, your git status will return something like:

deleted:    someFileName.ts
untracked:  newFileName.ts

^despite the apparent acknowledgement of a rename, git handles both scenarios identically.

After running git add -- someFileName.ts newFileName.ts, you will get the same:

renamed: someFileName.ts -> newFileName.ts

Interestingly, this is not the norm for most SCMs. Linus explains the design decision here.

So to answer the original question: Use the VSCode integrated file explorer for file moves/renames - this updates import paths AND there is no detriment to version control.

Ryan Tipps
  • 53
  • 6
  • Are you sure? What happens if you do `git log newFileName.ts` in each case? – bruceg Sep 27 '22 at 22:35
  • 1
    @bruceg: yes,git does not keep track of renames. It also has guessing rules to detect renamings after the facts -- namely: if two files have enough lines in common, some commands in git will treat this as a rename. For example : `git log newFileName.ts` will stop at the first commit, but `git log --follow newFileName.ts` will also follow the history of `someFileName.ts`. – LeGEC Sep 28 '22 at 03:19
  • 1
    `git mv old new` is just a shortcut for `git rm --cached old; mv old new; git add new` (it is a bit more elaborate because it also preserves "staged content" and "worktree content", but that's the gist of it) – LeGEC Sep 28 '22 at 03:20
  • @LeGEC Excellent point. Yes. The git log --follow usually works to show the history of renamed files. – bruceg Sep 28 '22 at 17:31
  • how do I bulk move mutliple source files from one directory as a github rename (not a delete and created file) AND have imports in all my source files updated? Right now I move them using VSCode's file explorer, selecting update imports on the prompt. I then move the files back (this time not accepting the prompt), and then I git mv them so it is tracked a rename. – friartuck Jun 18 '23 at 14:36