Renaming or Moving Files

In this picture as you see mosh uses this command git mv main.js file1.js , but in the status file1.txt renamed to file1.js :confused: of course i got the same result but i don’t know why
Shouldn’t it be main.js -> file1.js ?

1 Like

That’s the output I would have expected when renaming a file. But when actually changing the location of a file git also sees that as a rename:

git mv file1.js ./bin
git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    file1.js -> bin/file1.js

I’m not that comfortable yet with how git handles file system changes internally but can say that most version control system have their strangenesses when it comes to renaming and moving files. Be especially careful when renaming or moving files by other means than git or a git-aware IDE (e.g. explorer/finder/shell). You might loose the history of the moved/renamed file.

It’s still on my agenda to do some research on how to deal with these kind of problems.


I found this on stackoverflow :

Suggesting to use git commit —dry-run which shows you the real expected result of renaming files.

But i’m not sure if that’s the only permanent solution to this issue

1 Like

Since I received a notification on this thread and noticed that I didn’t really get your point when answering I’m going to revive this…

A little bit earlier in the video Mosh did a mv file1.txt main.js. (files system change with OS means) and staged the changes to both files. He did that to show us that a rename is a two step operation.

Then he did what your screenshots shows (a file system change with git means). So the original file file1.txt was renamed twice to finally become file1.js. Since we didn’t commit in between, git correctly shows that we actually have a rename of the file file1.txt to file1.js in our staging area.

BTW: What I found interesting in the first step was that git correctly noticed that we renamed a file after staging the deletion of file1.txt and the addition of main.js. Since the file content didn’t change git noticed that there’s a deleted file and an added file and they both have the same hash so it concluded that we actually did a rename.

I don’t think this would have worked if we also changed the content of the file. From my experience with other VCSs I’d never do file system changes by OS means but use the means the VCS provides.

1 Like