How to Git- Delete, Move & Rename files

In this guide, we are going to learn how to delete, move & rename files. Let’s dive into each of the topics.

Delete Files

To delete a file from the git repository, use git rm command. The following example will show you how to delete the second.txt file from the repository.

Create a file named second.txt on our directory. Add and commit it to git repository.

 

Git Commit
   $ touch second.txt
   $ git add second.txt
   $ git commit -m "second.txt commited"

 

Commit Result
   [master 27304a3] second.txt commited
   1 file changed, 0 insertion(+), 0 deletion(-)
   create mode 100644 second.txt

 

Use git rm command to remove the file and track the change using git status command.

 

Git Remove
    $ git rm second.txt
    rm 'second.txt'

    $ git status
    On branch master
    Changes to be commited:
      (use "git reset HEAD <file>..." to unstage)

           deleted:   second.txt

Commit the deletion using git commit -m command.

Commit changes
   $ git commit -m "commited the deletion of second.txt"
   [master f38b409] commited the deletion of second.txt
   1 file changed, 0 insertions(+), 0 deletions(-)
   delete mode 100644 second.txt

Rename Files

Renaming files use nothing but the git mv command. git mv takes at least two arguments, a source, and a destination. The following command is used to rename a file.

Git Rename
   git mv old_file_name new_file_name
   git add new_file_name
   git commit -m "message goes here"

Let’s rename the file first.txt to home.txt.

 

Example
   $ git mv first.txt home.txt
   $ git add home.txt

 

git mv first.txt home.txt command renames first.txt to home.txt git add home.txt add

git add home.txt  save home.txt to the staged area.

Let’s check the status,

 

Status
    $ git status
    On branch master
    Changes to be commited:
        (use "git reset HEAD <file>..." to unstage)

               renamed:   first.txt  ->  home.txt

 

Git status says that the renamed file need to be commit to the repository.

Let’s commit staged area to repository using git commit

 

Commit the Rename
    $ git commit -m "Renamed file commited"
    [master 93ec701] Renamed file commited
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename first.txt  =>  home.txt (100%)

 

Move Files

To move a file from the git repository to another directory, use git mv command.

 

Git Move command
   git mv filename_to_move directory_name

 

The following example moves home.txt to a folder named FirstFolder.

 

Git Move Example
    mkdir FirstFolder creates a directory named FirstFolder.
    git mv home.txt FirstFolder

$ mkdir FirstFolder creates a directory named FirstFolder.

$ git mv home.txt FirstFolder Move home.txt to FirstFolder directory.

Let’s check status

 

Status
    $ git status
    On branch master
    Changes to be commited:
        (use "git reset HEAD <file>..." to unstage)

               renamed:   first.txt  ->  FirstFolder/home.txt

 

commit staged area to repository using git commit -m

 

Commit the move
    $ git commit -m "Moved file commited"
    [master 93ec701] Moved file commited
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename first.txt  =>  FirstFolder/home.txt (100%)

 

Muhsin Yakkiparamban

Muhsin Yakkiparamban is the co-founder of Lauyou Learning. Muhsin holds Bachelor Degree in Civil Engineering from KMCT Calicut.

You may also like...