I have added a file named "file1.txt" to git repo. After that I committed it, added a couple directories called dir1 and dir2, and committed them to git repo.

Now the current repo has "file1.txt", dir1 and dir2.
How can I delete "file1.txt" without affecting others like dir1 and dir2?

15 Answers 15

up vote 2179 down vote accepted

Use git rm:

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  
  • 37
    Also handy: git rm -r directory // To remove directory and content – Reg Nov 15 '14 at 15:02
  • 8
    That's not going to get rid of that file in other commits though. – VaTo Jun 16 '15 at 23:32
  • 3
    @SaulOrtega: That's correct. To remove a file from previous commits (changing past history), see GitHub's help page on Remove sensitive data. – Greg Hewgill Jun 16 '15 at 23:43
  • 10
    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 – Weston Ganger Aug 19 '15 at 22:44
  • 3
    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 '16 at 20:51

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.

  • 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. – ban-geoengineering Jun 10 '14 at 18:13

If your file is already on GitHub, you now (July 2013) can 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.

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".

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.

  • 4
    This is a great answer, but note that in contrast to the other answers, this command rewrites the commit history and removes all trace of the files matching the pattern. This is particularly useful if you found you accidentally committed some sensitive information (and, if needed can successfully use --force to rewrite history of any remote repos this has been pushed to). – yoniLavi Apr 28 '15 at 0:51
  • 2
    That was exactly the problem that I had and it took some trial and error to get it right. A guy that I was working with had committed several large database dumps into a repo going back through many many commits. Which is why I used .sql in my example. My repo got so bloated and large that github wouldn't accept pushes anymore. – Jason Glisson May 19 '15 at 18:52
  • If you are using a remote master you need to force a commit and push it up with git push --force -u origin master – AlbertMarkovski Aug 31 '16 at 22:56
  • 1
    @AlbertMarkovski Sorry I wasn't specific. Github actually wouldn't accept the push because the repo was beyond their size limit. At least several GBs. They cut me off and emailed me about it. So, unfortunately, no amount of forcing a push would help at that point. – Jason Glisson Sep 8 '16 at 18:12
  • 1
    @JasonGlisson I've also faced that issue where a video got added to a repository by accident. – AlbertMarkovski Sep 20 '16 at 19:53

More generally, git help will help with at least simple questions like this:

zhasper@berens:/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
  • 4
    For more complex things, I find git help confusing, but it's good for the simple things :) – James Polley Jan 12 '10 at 10:43
  • 8
    It's not actually that great for git noobs because "index" is not a concept git noobs are familiar with. I speak from personal experience of being a git noob :) Also, I feel it's much less confusing to say that rm stages a file for deletion rather than removes from the index (though it's still meaningless for noobs). – Roman Starkov Apr 4 '13 at 11:51
  • Agreeing with romkyns: The description "Remove files from the working tree and from the index" doesn't sound to me like it's a way to remove the file from the repo. Maybe if I understood git better it would. Instead it sounds like you're just unstaging files (and optionally removing them from the working tree -- would that necessarily affect the repo?) – LarsH Jul 5 '16 at 21:33

Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:

git rm -r foldername

If you want to delete the file from the repo, but leave it in the the file system (will be untracked):

bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"

If you want to delete the file from the repo and from the file system then there are two options:

  1. If the file has no changes staged in the index:

    bykov@gitserver:~/temp> git rm file1.txt
    bykov@gitserver:~/temp> git commit -m "remove file1.txt"
    
  2. If the file has changes staged in the index:

    bykov@gitserver:~/temp> git rm -f file1.txt
    bykov@gitserver:~/temp> git commit -m "remove file1.txt"
    

git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.

The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.

But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.

If you want to completely obliterate it in one step, I recommend you to use git forget-blob

https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

It is easy, just do git forget-blob file1.txt.

This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.

Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.

rm file1.txt

git commit -a -m "Deleting files"

git push origin master

If you have the GitHub for Windows application, you can delete a file in 5 easy steps:

  • Click Sync.
  • Click on the directory where the file is located and select your latest version of the file.
  • Click on tools and select "Open a shell here."
  • In the shell, type: "rm {filename}" and hit enter.
  • Commit the change and resync.

In my case I tried to remove file on github after few commits but save on computer

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master

and later this file was ignored

To delete a specific file

git rm filename

To clean all the untracked files from a directory recursively in single shot

git clean -fdx

  1. First,Remove files from local repository.

    git rm -r File-Name

    or, remove files only from local repository but from filesystem

    git rm --cached File-Name

  2. Secondly, Commit changes into local repository.

    git commit -m "unwanted files or some inline comments"   
    
  3. Finally, update/push local changes into remote repository.

    git push 
    
  • would this remove everything related with the accidentally added and pushed file? – bapors Feb 14 at 22:56

I tried a lot of the suggested options and none appeared to work (I won't list the various problems). What I ended up doing, which worked, was simple and intuitive (to me) was:

  1. move the whole local repo elsewhere
  2. clone the repo again from master to your local drive
  3. copy back the files/folder from your original copy in #1 back into the new clone from #2
  4. make sure that the problem large file is either not there or excluded in the .gitignore file
  5. do the usual git add/git commit/git push

Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface. Click Here, To view interface Delete Option

protected by Nicolas Filotto Nov 30 '16 at 17:31

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.