
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Steps to Rename a File in Git
A file can be renamed in the following two ways −
Use the mv Linux command
Use the git mv command
Scenario 1 − Use the Linux mv command
The following example assumes that a file “file1.txt” exists in the repository. The syntax for using the Linux mv command is −
$ mv <old_filename> <new_filename>
Use the Linux command mv to rename the file to “file1.java”.
$ mv file1.txt file1.java
Execute the git status command to verify the file’s status in Git.
$ git status
The output in the screenshot suggests that the file has been renamed in two steps −
“file1.txt” has been deleted from the working area
A new file “file1.java” has been added. Note that the status of the “file1.java” is “Untracked” which means that the changes have been made only in the working area and have not been staged.
On branch master Changes not staged for commit: (use “git add/rm <file>...” to update what will bw committed) (use “git restore <file>...” to discard changes in working directory) Deleted: file1.txt Untracked files: (use “git add <file>...” to include what will be committed) file1.java no changes added to commit (use “git add” and/or “git commit -a”)
Git does not automatically track these changes in the project. This means that the rename operation should be staged. Perform the following to record the rename operation in the staging area.
Stage the deleted file− “file1.txt” and
Stage the untracked file− “file1.java”
Use the git add command to achieve this.
$ git add file1.txt $ git add file1.java
Use the git status command to verify the status.
$ git status
The output of the command suggests that the rename operation has been recorded.
On branch master Changes to be committed: (use “git restore −−staged <file>...” to unstage) Renamed: file1.txt −>file1.java
Scenario 2 − Use the git mv command
Git provides the git mv command that can be used to rename a file. Unlike the Linux mv command, this command simplifies the process by making changes in the working area and recording the change in the staging area at once. This means that we need not manually record the rename operation in the staging area if this command is used to rename a file.
The syntax to use the git mv command is −
$ git mv <old_filename> <new_filename>
The following example renames a file “file1.txt” to “file1.python”. This example assumes that “file1.txt” already exists.
$ git mv file1.txt file1.python
Verify the operation’s status −
$ git status
It is clear from the output that the rename operation has been staged.
On branch master Changes to be committed: (use “git restore −−staged <file>...” to unstage) Renamed: file1.java −>file1.python
Finally let’s commit the changes to make it permanent.
$ git commit −m ‘rename file java to python’
The output of the command suggests that 1 file changed and it has 0 insertions and 0 deletions. It was just a rename operation.
[master d8f9920] rename file java to python 1 file changed , 0 insertions(+), 0 deletions(-) rename file1.java=>file1.python (100%)