Contents...
Whenever we rename file in git or change the file, Git keep the track of all changes in the working directory into your repository by their name. Whenever we rename or move a file, git does not see that file moved, actually git see that there is a file with new name and old file has been deleted even if it contents remain the same.
So we can say that in Git rename and move process both are the same. We can see that the same are used for renaming and moving a file.
In this article I am going to show you how to rename file in Git. So I am in my current git working directory and rename a previously committed file name called “myfile.txt” to “newfile.txt” and commit the this newly renamed file.
Rename File in Git
Follow these simple steps to rename the file in Git.
1.Got to your git repository. For example my repository is here:
$ cd /var/project
In my case I have multiple files in my project.
$ ls -l total 4 drwxr-xr-x 2 root root 4096 Dec 28 03:29 Documents -rw-r--r-- 1 root root 35259 Dec 28 03:28 myfile.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
2. Now I am going to rename “myfile.txt” to “newfile.txt”
$ git mv myfile.txt newfile.txt
Here we can see that there is no any output after running above command.
3. Let’s run the below command to commit the change.
$ git commit -m 'File Renamed'
Output:
[master d58f9b4] File Renamed 1 file changed, 0 insertions(+), 0 deletions(-) rename myfile.txt => newfile.txt (100%)
If you want to push this changes from your local repository to remote repository just run the below command.
$ git push origin master
Output:
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/santosh.prasad/looklinux.com 608ab63..d58f9b4 master -> master
FAQs
How can I rename a folder in Git?
To rename a folder just use “mv” command and then deleted the old folder using “git rm old_directory”. Once file deleted run “git add new_directory” and commit and push the file.
Can I rename a Git project in GitHib?
In GitHub, go to main page of the repository. You will find the heading under repository name, just type the new name of the repository and click on Rename button. That’s all.
How can I push a file after rename it?
Simply rename the file using “git mv” command and commit the changes with “git commit -m “Msg here” command. Then push your changes to remote repository using “git push origin master” command.
How can I rename a branch in Git?
Renaming a branch is not in practice because you can not rename branch. You should create a new branch from the branch which you want to rename and then you can delete the old branch.
Can I rename master branch in Git?
Yes, first rename your local master branch using “git branch -m master main” and then rename the remote master branch as well using “Git Push -u origin main”.
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Leave a Comment