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

Git Move Files

Use git mv command to move files and directories in current git repository. You can see that the same command is used to rename files as well.

Syntax

git mv [FILENAME] [LOCATION TO MOVE]

Example

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

[email protected]:/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
-rw-r----- 1 root root 121185 Dec 28 03:59 general.log
-rw-r--r-- 1 root root     47 Dec 28 03:27 README.md

Now creating a logs directory in the current project and move the general.log file under logs directory.

[email protected]:/app$ mkdir logs 
[email protected]:/app$ git mv general.log logs/ 

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

[email protected]:/app$ git status -s

R  general.log -> logs/general.log

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.

[email protected]:/app$ git commit -m "Modified Logs Structure" 
[master 608ab63] Modified Logs Structure
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename general.log => logs/general.log (100%)


[email protected]:/app$ git push origin master 
Username for 'https://github.com': [GIT USERNAME]
Password for 'https://[USERNAME]@github.com':
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 314 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/tecrahul/tecadmin.net
   5d9a477..608ab63  master -> master
Tags , ,
Was this article helpful to you? Yes 1 No
Advertisement