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?

4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure? What happens if you do git log newFileName.ts in each case?
@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.
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 Excellent point. Yes. The git log --follow usually works to show the history of renamed files.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.