How can I delete a file from git repo?

2019-01-01 11:17发布

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?

标签: git git-rm
18条回答
有味是清欢
2楼-- · 2019-01-01 12:01

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

查看更多
余生请多指教
3楼-- · 2019-01-01 12:05

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.

查看更多
美炸的是我
4楼-- · 2019-01-01 12:07

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.
查看更多
孤独寂梦人
5楼-- · 2019-01-01 12:09

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

查看更多
梦醉为红颜
6楼-- · 2019-01-01 12:10
  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 
    
查看更多
像晚风撩人
7楼-- · 2019-01-01 12:11

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"
    
查看更多
登录 后发表回答