1. Home
  2. Git
  3. Git Tutorial
  4. Git – Rename Files

Git – Rename Files

Git Rename Files

Use git mv command to rename files and directories in current git repository. You can see that the same command is used to move files as well. The difference between both operations is that if you changed the filename only and destination is the same directory then it called rename.

Syntax

git mv [FILENAME] [NEW FILENAME]

Example

For example, you have multiple files in your current project. In my case, the files are as followings.

rahul@tecadmin:/app$ ls -l 

total 164
drwxr-xr-x 2 root root  4096 Dec 28 03:29 Documents
-rw-r--r-- 1 root root 35259 Dec 28 03:28 firstfile.txt
drwxr-xr-x 2 root root  4096 Dec 28 04:03 logs
-rw-r--r-- 1 root root    47 Dec 28 03:27 README.md

Now, I am renaming file firstfile.txt with new name users.txt in the current directory.

rahul@tecadmin:/app$ git mv firstfile.txt users.txt 

You can view the current changes using git status command. The -s switch shows the short message only.

rahul@tecadmin:/app$ git status -s

R  firstfile.txt -> users.txt

At the end, you need to commit your changes to the local git repository and then push changes to remote git repository using the following commands.

rahul@tecadmin:/app$ git commit -m "Changed filename" 

[master d58f9b4] Changed filename
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename firstfile.txt => users.txt (100%)


rahul@tecadmin:/app$ git push origin master 

Username for 'https://github.com': [GIT USERNAME]
Password for 'https://[USERNAME]@github.com':
Counting objects: 2, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 335 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/tecrahul/tecadmin.net
   608ab63..d58f9b4  master -> master
Tags , ,