Removing & Renaming files are one of fundamental requirements any Git users would need. For instance, you might want to correct a typo or merge two files into one file. There are several use cases for these two functionalities, thereby in this section, we'll cover the following items:
- How to remove a file?
- How to remove a file? [Git-Style]
- How to rename a file?
- How to rename a file? [Git-Style]
Before removing a file, let's create a file in the previous project which we created, namely git_01.
cd git_01
touch hello_world.txt # On Windows, you can create it graphically.
git add hello_world.txt # Add to stage(one step before commiting)
git commit -m "Add hello_world.txt" # commit the changes on stage
git push origin main # push the changes to remote repository
Now, let's remove the file simply by deleting it:
rm hello_world.txt # On Windows, you can remove it graphically.
git status
Image Notes:
hello_world.txt
is deleted, but the changes are not staged!!- To restore a removed file use:
git restore <file-name>
. Using this command, you will have the exact file present on your git project/directory! - For adding the changes cause by removing a file you should use the following command:
git rm <file-name>
So let's add the changes to stage:
git rm hello_world.txt
git status
Image Notes:
- the
rm
command aftergit
is one of the git commands, and it works on Windows/Linux/Mac. - the changes now are transfered to stage, ready for being committed
- Like always, by using
git restored --staged <file-name>
command one can restore the files which are staged!
Let's commit and push the changes:
git commit -m "Remove hello_world.txt"
git push origin main
Git provides a way to remove files directly that you might find more convenient to use. To explore it, let's create a file like the previous step:
cd git_01
touch hello_world.txt # On Windows, you can create it graphically.
git add hello_world.txt # Add to stage(one step before commiting)
git commit -m "Add hello_world.txt" # commit the changes on stage
git push origin main # push the changes to remote repository
Now, let's remove it using git:
git rm hello_world.txt
git status
Image Note:
- As it's shown in the image above, the file is removed directly. It's not present in the project's directory.
- The changes are directly added to the
stage
and ready to be committed!
Let's commit and remove them:
git commit -m "Remove hello_world.txt"
git push origin main
NOTE: The commit is tagged as delete mode
Renaming a file is a bit trickier than removing it because we still want to have the file but in another name!
So as always, let's create a file with a typo in its name. We'll continue our experiments on git_01 project.
cd git_01
touch names_datbase.txt # On Windows, you can create it graphically.
git add names_datbase.txt
git commit -m "names_database.txt"
git push origin main
Now, let's correct the typo by renaming it!
mv names_datbase.txt names_database.txt # On Windows, you can rename it graphically!
git status
Image Notes:
- As it's evident, renaming a file compasses two steps:
- Removing it
- Creating a new one
- Therefore, the previous name must be removed from git and the new one must be added!
Let's take the appropriate actions to completing the renaming process:
git rm names_datbase.txt # remove the one with typo
git add names_database.txt # Add the new one
git status
As depicted in the image above, git
finds out that a renaming is going on and tags the files as renamed
The changes are applied to stage
and they are ready to be committed:
git commit -m "[Fix-Typo] Rename names_datbase.txt to names_database.txt"
git push origin main # push the changes to remote repository!
NOTE: Even in the commit message it mentions the renaming process!
Git also provides a way to rename files directly that you might find more convenient to use. To explore it, let's create a file containing a typo like the previous section:
cd git_01
touch names_datbase.txt # On Windows, you can create it graphically.
# There is a typo in the name of the file!
git add names_datbase.txt
git commit -m "names_database.txt"
git push origin main
In order to correct the intentionally introduced typo, we can use git mv
:
git mv names_datbase.txt names_database.txt
git status
Image Notes:
git mv
is git command can be executed in any operation-systems(os) like Windows and Linux- The renaming process is done automatically
- The changes are added to
stage
automatically as well and are ready to be committed!
git commit -m "[Fix-Typo] Rename names_datbase.txt to names_database.txt"
git push origin main
NOTE: the commit is tagged as rename
!