Deleting Files in Git

Deleting Files in Git

 Deleting Files in Git

Use git rm command to delete any file from the current working directory. You also need to commit and push your changes to delete the file from the local and remote git repository.


git rm [filename]


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

rahul@tecadmin:/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 119180 Dec 28 03:28 general.log
-rw-r--r-- 1 root root     47 Dec 28 03:27

Now, I am deleting the general.log file from the git repository. Change filename according to your deletion.

rahul@tecadmin:/app$ git rm general.log

rm 'general.log'

After deleting the file, you have to commit your changes to the local git repository using the following command. Change your commit message according to yours.

rahul@tecadmin:/app$ git commit -a -m "Removed log file"

[master 5558666] Removed log file
 1 file changed, 1068 deletions(-)
 delete mode 100644 general.log

Now push your changes, So that the file also gets deleted from the remote git repository.

rahul@tecadmin:/app$ git push origin master

Username for '': [GIT USERNAME]
Password for 'https://[GIT USERNAME]':
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.
   3d31ae3..5558666  master -> master

Post a Comment