2

I reset my branch to an earlier commit, keeping changes, using a graphical git app.

However what was previously a moved file (git mv Anchor.jsx Anchor.tsx) has become an add and a delete:

$ git status
git status
On branch more-pre-project-creation-fixes
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  deleted:    components/atoms/Anchor.jsx
  new file:   components/atoms/Anchor.tsx

How can I fix this using the git command line?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • Closing this one myself as I found a duplicate. – mikemaccana May 21 '21 at 18:24
  • 2
    Not sure which duplicate you found, but git doesn't actually record moves as moves. Instead you have one snapshot, and then you have another. If you ask git what the difference between the two are it will use heuristics to figure out that a delete+add is actually a rename/move. If the file was moved/renamed **and** changed enough, the heuristics will fail, and will still be shown as a delete + an add. However, if the file was not changed (enough), it doesn't matter if you moved it yourself or if you asked git to move it. Git still doesn't record information that something was moved/renamed. – Lasse V. Karlsen May 21 '21 at 18:27
  • 1
    As a general tip, if you want git to show files that are moved and/or renamed, as a move/rename instead of a delete+add, make the move/rename in one commit and the changes to the file contents in another. Example changes could be things like fixing C# namespaces or similar which might need to be change if you move the file to a different folder. – Lasse V. Karlsen May 21 '21 at 18:28
  • Duplicate was https://stackoverflow.com/questions/4708655/git-renamed-file-manually-git-confused – mikemaccana May 21 '21 at 18:29
  • 1
    PS Thanks @LasseV.Karlsen - the file was moved & changed which matches what you're writing. – mikemaccana May 21 '21 at 18:30
  • 1
    Note that that answer is a simplification. If you **only** move or rename the file, no matter which method you use to do it, git should in almost all cases list the difference as a move/rename, but if it was also changed *in the same commit*, then the heuristics become involved and they have thresholds for how big changes they tolerate before they give up. – Lasse V. Karlsen May 21 '21 at 18:30
  • Also, when I said "if you want git to show files that are moved ...", you generally would want to do this because history will also follow the file in this case. Things like blame and show history and such might traverse move/rename operations if git is able to detect that's what happened. So in general, try to do minimal or no changes to a file in the same commit that you also rename or move it. – Lasse V. Karlsen May 21 '21 at 18:32
  • And a final tip, if the commit that changed *and* moved/renamed the file is a recent one, or perhaps even *the* most recent one, you can fix this by doing some history rewriting by creating new commits that do rename/move in one commit and the change in another. – Lasse V. Karlsen May 21 '21 at 18:33
  • Nice expl. here https://stackoverflow.com/questions/43906993/how-does-git-log-follow-filename-work – matt May 21 '21 at 19:56

0 Answers0