Bootstrap FreeKB - Git (Version Control) - Move or Rename a file
Git (Version Control) - Move or Rename a file

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.

 

The git mv command can be used to:

  • Move a file from one directory to another directory
  • Rename a file

AVOID TROUBLE

The git mv command should always be used instead of the Linux mv command.


Move a file from one directory to another directory

Let's say you have a file in the currently selected branch of the cloned repository and you want to move the file to some other directory. In this example, we want to move the sample.txt file from the foo directory to the bar directory. 

git mv foo/sample.txt bar

 

The git status command will show that the file has been renamed (technically, moved).

git status
. . .
renamed: foo/sample.txt -> bar/sample.txt

 

Once you commit the change, the git status command will show that the file has been deleted from the old location.

deleted: foo/sample.txt

 


Rename a file

Let's say you want to rename foo.txt currently selected branch of the cloned repository to bar.txt. You can use the -n or --dry-run flag to see what should happen. In this example, no issues are detected, meaning the file should be renamed to bar.txt.

~]# git mv foo.txt bar.txt --dry-run
Checking rename of 'foo.txt' to 'bar.txt'
Renaming foo.txt to bar.txt

 

Use the git mv command without the -n or --dry-run flags to actually rename the file. If the rename is successful, no output should be displayed, and the file should have been renamed to bar.txt.

git mv foo.txt bar.txt

 

The git status command should show that foo.txt is ready to be deleted.

~]# git status foo.txt
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    foo.txt
#

 

And that bar.txt is a new file.

~]# git status bar.txt
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   bar.txt
#

 

You can now commit the change to both foo.txt and bar.txt.

~]# git commit -m "renamed foo.txt to bar.txt" foo.txt
 1 file changed, 208 insertions(+)
 create mode 100755 foo.txt

~]# git commit -m "renamed foo.txt to bar.txt" bar.txt
 1 file changed, 208 insertions(+)
 create mode 100755 bar.txt

 

Now, the status of foo.txt and bar.txt should return the following.

~]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)

 

If the files are not in HEAD, you can merge the file in your currently selected branch to the master branch and then push HEAD to the origin Git repository.

git push origin HEAD

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9ae72f in the box below so that we can be sure you are a human.