How can I delete a file from a Git repository?
I have added a file named
"file1.txt" to a Git repository. After that, I committed it, added a couple of directories called
dir2, and committed them to the Git repository.
Now the current repository has
dir2. How can I delete
"file1.txt" without affecting others, like
If you want to remove the file from the Git repository and the filesystem, use:
git rm file1.txt git commit -m "remove file1.txt"
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
git rm --cached file1.txt git commit -m "remove file1.txt"
And to push changes to remote repo
git push origin branch_name
Read more... Read less...
git rm file.txt removes the file from the repo but also deletes it from the local file system.
To remove the file from the repo and not delete it from the local file system use:
git rm --cached file.txt
The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a
git add -A from the website's base directory. Subsequently,
git status showed the new files needing committing. Now I needed to delete them from git's tracking and version control...
Sample output below is from what just happened to me, where I unintentionally deleted the
.003 file. Thankfully, I don't care what happened to the local copy to
.003, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).
[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003 error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications (use --cached to keep the file, or -f to force removal) [~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003 rm 'shop/mickey/mtt_flange_SCN.7z.003' [~/www]$ [~/www]$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: shop/mickey/mtt_flange_SCN.7z.003 # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: shop/mickey/mtt_flange_SCN.7z.001 # modified: shop/mickey/mtt_flange_SCN.7z.002 [~/www]$ ls shop/mickey/mtt_flange_S* shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002 [~/www]$ [~/www]$ [~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002 rm 'shop/mickey/mtt_flange_SCN.7z.002' [~/www]$ ls shop/mickey/mtt_flange_S* shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002 [~/www]$ [~/www]$ [~/www]$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: shop/mickey/mtt_flange_SCN.7z.002 # deleted: shop/mickey/mtt_flange_SCN.7z.003 # # Changed but not updated: # modified: shop/mickey/mtt_flange_SCN.7z.001 [~/www]$
Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.
First, if you are using
git rm, especially for multiple files, consider any wildcard will be resolved by the shell, not by the
git rm -- *.anExtension git commit -m "remove multiple files"
But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!
Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.
git pull" on your local repo, and that will delete the file locally too.
Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub is in a "git repo")
(the commit will reflect the deletion of that file):
And just like that, it’s gone.
Note: Since it’s a version control system, Git always has your back if you need to recover the file later.
The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):
This is the only option that worked for me.
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.
EDIT: pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull
Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:
git rm -r foldername
git help will help with at least simple questions like this:
[email protected]:/media/Kindle/documents$ git help usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS] The most commonly used git commands are: add Add file contents to the index : rm Remove files from the working tree and from the index