What happens when I rename a tracked file in GIT repo - jsanderson104/GIT-CheatSheet GitHub Wiki
There are essentially 2 ways to do this.
- The Linux shell way which is longer and more error-prone using native Linux command
mv. - The GIT way and much easier - Recommended!
We'll go over the recommended way first since it's just better all around when using GIT tracked files.
git mv [oldfilename] [newfilename]
Tadaah!! :)
Ok. Since I'm a Linux geek at heart. He's the lengthy Linux way... So what it actually does is create a NEW untracked file. See below for the fix.
mv [oldname] [newname]
The output of ``` git status `` should now show that the original filename is to be deleted -AND- the new files is NOT being tracked.
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: js.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
renamed.txt
no changes added to commit (use "git add" and/or "git commit -a")
- GIT basically thinks these 2 files are completely not related.
We've already renamed the file w/o letting GIT know about it... So we really have 2 choices to move forward.
-
We do
git add .which will inform GIT that they are the same file and the resultantgit statusafter that will reflect it. BUT, there's a small problem with this... what if you didn't want to start tracking ALL of the files in that directory. Well now you are and have potentially created another mess to clean up. -
Follow the steps below to do it on just the filenames we need.
git add [newfilename]
git rm [oldfilename]
git status should now show that it was renamed.
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: js.txt -> renamed.txt
NOTE: If you move a file into a folder inside your repo (previously created or new folder, doesn't matter) the file is treated the same way as a rename.
You have to do a git add directory/movedfile.txt then a git rm originalfile.txt to make it happy again. Alternatively, you can go inside the directory where you moved the file to and do a generic get add . but we explained the caveats above with that. AGAIN, if you used the recommended git mv method then you wouldn't need to do these 2 additional steps for every file!