Thursday, May 13, 2021

GIT Basics - 5 Rename and Move files

Renaming and Moving files

Create file at each level and commit
eg: lv1 --> lv1.txt , lv2 --> lv2.txt

git status  --> clean

Rename using mv command. git mv {currentFileName} {newFileName}

git mv lv3.txt lv3-new.txt

ls - list old files, git has already renamed the file in opertating system level
lv3-new.txt

git status
Changes to be committed:
renamed: lv3.txt --> lv3-new.txt
git commit -m "renaming lv3 file "

Now let's rename with os commands. go to top folder cd..

cd..
ls
lv2.txt lv3/

to rename, mv {filename} {newFileName}
mv lv2.txt lv2-new.txt

ls
lv2-new.txt lv3/

if you check git status, git see this as two operations

git status
Changes not statged for commit:
deleted: lv2.txt
Untracked files:
lv2-new.txt
git add -A
recursively add any changes, but also update any file renamed, moved or deleted

git status
Changes to be committed:
renamed: lv2.txt --> lv2-new.txt
git commit
renaming level 2 file

Rename the file and back it out

git mv lv2-new.txt 2.txt
ls
2.txt lv3/

git status
Changes to be committed:
renamed: lv2-new.txt -> 2.txt
Note: file is staged

Now you need to unstage:
you can use reset command shown in status. git reset HEAD 2.txt
let's use simple way using mv command. mv {newFileName} {oldFileName}

git mv 2.txt lv2-new.txt
ls   this will show rename is happened in OS level
lv2-new.txt lv3/

git status
nothing to commit, working directory clean


Move the file to another directory

ls
lv2-new.txt lv3/

git mv lv2-new.txt lv3
ls
lv3/

cd lv3
ls
lv2-new.txt lv3.txt

git status
Changes to be committed:
renamed: ../lv2-new.txt -> lv2-new.txt
Note: this has been staged
let's see what we can see if we go up in directory structure
cd..
git status
Changes to be committed:
renamed: lv2-new.txt -> lv3/lv2-new.txt
git commit
"Moving file from lv2 to lv3"

ls
lv3/

cd lv3
ls
lv2-new.txt lv3.txt

Let's do without git.
mv lv2-new.txt ..
ls
lv3.txt

cd..
pwd
ls
lv2-new.txt lv3/

git status
Changes not staged for commit:
deleted: lv3/lv2-new.txt
Untracked files:
lv2-new.txt
to fix this
git add -A
git status
Changes to be committed:
renamed: lv3/lv2-new.txt -> lv2-new.txt
git commit 
"Moving lv 2 file back to lv2 directory"

cd..
pwd
{..}/lv1

Use windows file explorer to rename file with right click / Mac "Finder"
rename lv1.txt to lv1-new.txt
Now go to terminal and see

ls
lv1-new.txt lv2/

git status will show like before

git status
Changes to be committed:
deleted: lv1.txt
Untracked files:
../.DS_Store
lv1-new.txt
git add lv1-new.txt
git add -u  --> to understand we renamed a file than simply adding afile
git status
Changes to be committed:
renamed: lv1.txt -> lv1-new.txt
Untracked files:
../.DS_Store
git commit
"renaming lv1 file using explorer"


No comments:

Post a Comment