6

I'm trying to convert some Javascript files to TypeScript files. I've renamed my move.js file to move.ts in my IDE (VSCode in this instance) and compiled with TSC to check if everything works. Now I want to commit my changes.

I don't know how to make SourceTree and/or Git understand that:

  1. My original file was renamed; and
  2. That file (with its new name) has some changes; and
  3. A new file with the original file's name was created;

Is that even possible? How do I use SourceTree (or failing that, the git command line tool) to indicate this status? Does it require two seperate commits?

Here's what SourceTree shows for the status:

SourceTree status

If I'd stage these files as is it would be committed as an edit+addition, which I don't want.

Here's what git status reports:

On branch TypeScriptConversion
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   src/js/domain/move.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  src/js/domain/move.ts

no changes added to commit (use "git add" and/or "git commit -a")

How do I tell SourceTree and/or git that my file was renamed, edited, and a new file with the same name entered in its place?

Jeroen
  • 60,696
  • 40
  • 206
  • 339

2 Answers2

4

Separate the commits: do the rename in one commit, then do the add of the replacement for the original file in a second commit.

otto-null
  • 593
  • 3
  • 15
  • In other words: you *cannot* do a rename and edit of a replacement file in one commit whilst keeping the info that the file was renamed. I just now learned as much by doing the seperate steps and then squashing them. I will heed your advice and use two commits instead. – Jeroen Feb 28 '16 at 20:10
  • Yes that's right - basically, if the file still exists in its original name, then it's technically changed (even if 100% changed), not renamed. There is no --"yeah but no, that's not what I mean" switch on rename for this :) – otto-null Mar 02 '16 at 19:53
  • I don't find a "rename" command in Sourcetree, so that must mean that you are referring to file rename. But that will take as a first action to add a "new" file in Sourcetree. So my Q is: how is "rename" and "add" two different actions/steps? – RIL Feb 07 '19 at 09:50
1

Just to add to the above answer (in 2020): it seems that it does detect renames if only a few, or a certain % of the lines are the same -- but of course if you want something guaranteed, do that in two steps (as mentioned in the recommended answer above).

One more important thing: be sure that you stage both items together at the same time to increase the chances that ST detects the diff as a rename properly; myself and coworkers have both observed that if you accidentally only stage the new file, it detects the old as deleted no matter how insignificant the changes are. It is even possible that files which are completely the same get mis-tagged as delete/add when they are done one at a time. I am not sure if this is internally via git itself or via ST adding on for this functionality (since I don't think detecting renames intelligently happens in git), but anyone can feel free to elaborate in a comment if you are aware

rob2d
  • 964
  • 9
  • 16