In this tutorial we will learn how to move file in git. In git you can move the file and directory easily in current git repository. We can also use the same command to rename the file as well.
Follow my Git Tutorial page to learn more about git.
Move File in Git
Let’s start how to move a file in git repository. For example I am going to create file structure in current repository. First I will create a directory in git repository named “data“. Then I will move my test.txt file in it.
$ mkdir data $ git mv test.txt data $ git status
$ mkdir $ git mv test.txt data $ git status # On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # renamed: test.txt -> data/test.txt
Here if you noticed that moving file in git:
- The text.txt file has been deleted
- Now test.txt file created in data directory.
Git status command will show you that file has been moved.
Second Way to Move File in Git
If you don’t think about version controlling when moving file we can use the simple Linux “mv” command instead of “git mv ” command.
Follow the below simple command to move the file in Git.
$ mkdir data $ mv test.txt data $ git add data/test.txt $ git rm test.txt
Commit Data Directory
Now just simply commit the changes to the current git repository and then push changes to remote git repository.
$ git commit -m "Moved test.txt file to Data Director"
Push changes to remote git repository.
$ git push origin master
How can I move file in Git?
To move file in git simply run the “git mv” command with file name. For example: git mv text.txt data and run git status to check the file status.
How can I move folder in Git?
Run “git mv” command to move folder from one folder to another folder. For example:
- mkdir data
- git mv data lib
- git rm -r data
- and git add lib/data
Run git status command to show the status of the above command.
How can I move .git file?
In simple word NO. It is not good in practice to rename and move .git file. It will be harmful for you.
How can I move my local git repository?
Yes, we can move entire local git repository including .git hidden directory. It is easy to move your current local git repository to another location in the system and it will not affect our remote repository for example GitHub.
How can I move multiple files in Git?
To move multiple file in Git, simply run the “git mv” command with files individually or just use Linux mv command and update the index manually. For example: git add -A.