Contents...
In this tutorial we will how to delete file from git. There is a very common command in git “git rm’. This command is used to remove a single file or multiple files. Basic function of the git rm command is to remove file from Git Index.
We can also use git commit and git push to changes and delete file from the local and remote repository. If you want to learn more about git read git tutorial.
Delete File from Git
Let’s start to delete the file form git. Follow the below command to delete file from git. For example I have multiple file in my project.
$ 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 test.txt -rw-r----- 1 root root 119180 Dec 28 03:28 apache.log -rw-r--r-- 1 root root 47 Dec 28 03:27 README.md
Now I am going to delete apache.log file from git repository and file system. Please select the file which you want to delete in my case I have selected apache.log file.
$ git rm apache.log rm 'apache.log'
Once file is deleted now we have to commit the changes to locate git repository. Follow the below command with commit.
$ git commit -a -m "Removed apache log file" [master 5558666] Removed apache log file 1 file changed, 1068 deletions(-) delete mode 100644 apache.log
That’s all now push the changes so that it also deleted from remote git repository.
$ git push origin master Username for 'https://github.com': [GIT USERNAME] Password for 'https://[GIT 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), 223 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/santosh.prasad/looklinux.com 3d31ae3..5558666 master -> master
But if we just want to delete file form git instead of filesystem use the below command.
$ git rm --cached apache.log
Now commit it.
$ git commit -m "apache.log"
Let’s push the changes to remote repository.
$ git push origin master
FAQs
How can I delete file from git?
User “git rm” command to delete the file from git repository and filesystem. If you just want to delete from git run “git rm –cached” command. For example : git rm –cached apache.log .
How can I delete file from GitHub?
Deleting the file from GitHub is easy. Follow these below steps:
- First make sure your repository should be fully updated.
- Now go to repository folder.
- Let’s select the file that you want to delete and delete it using “git rm” command.
- Then run the git commit -m “Deleted File”.
- Finally run the “git push” command to merge with remote repository.
How can I remove file from git without removing from system?
You can just remove the file from git without removing it from filesystem. Run “git rm –cached file_name” command and run “git commit -m ” commit message here” and finally run “git push origin branch_name”.
How can I remove folder from git?
If you want to delete folder from git just use “-r” option. It will remove or delete the file recursively from your folder. For example: git -rm -r folder_name and run “git commit -m ” commit message here” and finally run “git push origin branch_name”.
Can I undo deleted file from git?
NO, in simple world you can not undo it because in Linux files remove permanently. There are no any concept of ” Trash”.
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