# Renaming or Moving Files

**URL:** <https://forum.codewithmosh.com/t/renaming-or-moving-files/1163>\
**Category:** Git\
**Created:** [December 7, 2020, 1:10am UTC](https://forum.codewithmosh.com/t/renaming-or-moving-files/1163 "2020-12-07T01:10:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![A100D-JS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a100d-js/32/8_2.png) [@A100D-JS](https://forum.codewithmosh.com/u/A100D-JS)\
**Post date:** [December 7, 2020, 1:10am UTC](https://forum.codewithmosh.com/t/renaming-or-moving-files/1163/1 "2020-12-07T01:10:30Z")

</div>

![Git Renaming](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/e079a17321d71c1ba31aacc0f503a8fa61fed1dd.jpeg)

Hi,  
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 😕 of course i got the same result but i don’t know why  
Shouldn’t it be `main.js -> file1.js` ?

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [December 7, 2020, 6:11am UTC](https://forum.codewithmosh.com/t/renaming-or-moving-files/1163/2 "2020-12-07T06:11:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![A100D-JS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a100d-js/32/8_2.png) [@A100D-JS](https://forum.codewithmosh.com/u/A100D-JS)\
**Post date:** [December 7, 2020, 3:37pm UTC](https://forum.codewithmosh.com/t/renaming-or-moving-files/1163/3 "2020-12-07T15:37:06Z")

</div>

I found this on stackoverflow :

> <https://stackoverflow.com/questions/2641146/handling-file-renames-in-git>

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

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [August 17, 2021, 4:25pm UTC](https://forum.codewithmosh.com/t/renaming-or-moving-files/1163/4 "2021-08-17T16:25:49Z")

</div>

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…

> [@A100D-JS](#):
>
> 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 😕 of course i got the same result but i don’t know why  
> Shouldn’t it be `main.js -> file1.js` ?

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.
