How can I delete a file from a Git repository?

后端 未结 24 1991
無奈伤痛
無奈伤痛 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 13:54

    Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.

    0 讨论(0)
  • 2020-11-22 13:55

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

    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.

    0 讨论(0)
  • 2020-11-22 13:56

    After you have removed the file from the repo with git rm you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.

    0 讨论(0)
  • 2020-11-22 13:56

    I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list

    After I noticed they went to the remote, I ignored them by adding this to .gitignore

    /*/obj
    /*/bin
    

    Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list.

    To stop seeing them, you need to delete the whole folder from the remote repository.

    In a command prompt:

    1. CD to the repo folder (i.e. C:\repos\MyRepo)
    2. I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. C:\repos\MyRepo\SSIS)
    3. Now type the magic incantation git rm -r -f obj
      • rm=remove
      • -r = recursively remove
      • -f = means force, cause you really mean it
      • obj is the folder
    4. Now run git commit -m "remove obj folder"

    I got an alarming message saying 13 files changed 315222 deletions

    Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote

    0 讨论(0)
  • 2020-11-22 13:57

    For the case where git rm doesn't suffice and the file needs to be removed from history: As the git filter-branch manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git

    1. Clone the repository into a new directory git clone https://example/eguser/eg.git

    2. Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths

    3. Add the remote repository origin back : git remote add origin https://example/eguser/eg.git. The git-filter-repo tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.

    4. When happy with the contents of local, replace remote with it.

      git push --force -u origin master. Forcing is required due to the changed history.

    Also note the useful --dry-run option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.

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