How can I delete a file from a Git repository?

后端 未结 24 1992
無奈伤痛
無奈伤痛 2020-11-22 13:41

I have added a file named \"file1.txt\" to a Git repository. After that, I committed it, added a couple of directories called dir1 and dir2

相关标签:
24条回答
  • 2020-11-22 14:13

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


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

    0 讨论(0)
  • 2020-11-22 14:13
    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 
      
    0 讨论(0)
  • 2020-11-22 14:13

    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

    0 讨论(0)
  • 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"
      
    0 讨论(0)
  • 2020-11-22 14:21

    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

    0 讨论(0)
  • 2020-11-22 14:21

    To delete a specific file

    git rm filename

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

    git clean -fdx

    0 讨论(0)
提交回复
热议问题