Categories
git git-rm

How do I delete a file from a Git repository?

2367

How do I delete "file1.txt" from my repository?

5

  • 113

    git rm is the right answer, but remember that the file will still be there in history. If you want to remove a file because it had sensitive information, you’ll need to do something more drastic. (Changing history, especially for content you’ve already pushed, is a drastic action, and should be avoided if possible.)

    May 16, 2013 at 21:06


  • 11

    Note: on GitHub, you now can directly delete a file from the web interface (without having to even clone the repo). See my answer below.

    – VonC

    Jul 4, 2013 at 20:53

  • 6

    @KeithThompson what steps might that be if I desperately want to do that?

    Feb 21, 2014 at 19:09

  • 7

    @lessthanl0l: stackoverflow.com/q/872565/827263

    Feb 21, 2014 at 19:19

  • related help.github.com/articles/…

    Jun 21, 2018 at 18:59

3933

Use git rm.

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

20

  • 141

    Also handy: git rm -r directory // To remove directory and content

    – Reg

    Nov 15, 2014 at 15:02

  • 14

    That’s not going to get rid of that file in other commits though.

    – VaTo

    Jun 16, 2015 at 23:32

  • 10

    @SaulOrtega: That’s correct. To remove a file from previous commits (changing past history), see GitHub’s help page on Remove sensitive data.

    Jun 16, 2015 at 23:43

  • 21

    Take note this will delete the file locally too. If you only want to delete it from the repo do: git rm –cached file1.txt

    Aug 19, 2015 at 22:44


  • 8

    It’s worth noting that if that file contained sensitive information (e.g. credentials) you should change those credentials immediately. To quote GitHub “Once you have pushed a commit to GitHub, you should consider any data it contains to be compromised. If you committed a password, change it! If you committed a key, generate a new one.”

    – c1moore

    Sep 14, 2016 at 20:51


641

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.

1

  • 6

    You’d then need to commit this with something like git commit -m "Just removing file1.txt" and then, if you have a remote repository, push the commit with something like git push origin master.

    Jun 10, 2014 at 18:13


100

First, if you are using git rm, especially for multiple files, consider any wildcard will be resolved by the shell, not by the git command.

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.

Then “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”)


delete button

(the commit will reflect the deletion of that file):

commit a deletion

And just like that, it’s gone.

For help with these features, be sure to read our help articles on creating, moving, renaming, and deleting files.

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):

See “Restore a deleted file in a Git repo“.

3

  • 4

    You have to scroll to the bottom and hit “Commit changes” to commit the delete!

    Aug 18, 2015 at 19:16

  • 1

    This does not work for large text files. The server tries to render the file as in the screenshot and then fails with a 500 error.

    – CapeCoder

    Aug 31, 2016 at 19:37

  • 12

    This answer is refering to github, the question is about a git repo. Not how to use the github interface.

    Apr 12, 2017 at 9:46