Remove a file from a Git repository without deleti

2018-12-31 07:35发布

My initial commit contained some log files. I've added *log to my .gitignore, and now I want to remove the log files from my repository.

git rm mylogfile.log

will remove a file from the repository, but will also remove it from the local file system.

How can I remove this file from the repo without deleting my local copy of the file?

9条回答
荒废的爱情
2楼-- · 2018-12-31 08:04

As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

To remove folder/directory or file only from git repository and not from the local try 3 simple steps.


Steps to remove directory

git rm -r --cached File-or-FolderName
git commit -m "Removed folder from repository"
git push origin master

Steps to ignore that folder in next commits

To ignore that folder from next commits make one file in root named .gitignore and put that folders name into it. You can put as many as you want

.gitignore file will be look like this

/FolderName

remove directory

查看更多
十年一品温如言
3楼-- · 2018-12-31 08:04

Above answers didn't work for me. I used filter-branch to remove all committed files

remove a file from a git repository with:

git filter-branch --tree-filter 'rm  file'

remove a folder from a git repository with:

git filter-branch --tree-filter 'rm -rf directory'

This removes the directory or file from all the commits

you can specify a commit by using:

git filter-branch --tree-filter 'rm -rf directory' HEAD

Or an range

git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD

To push everything to remote, you can do:

git push origin master --force
查看更多
无与为乐者.
4楼-- · 2018-12-31 08:09

To remove an entire folder from the repo (like Resharper files), do this:

git rm -r --cached folderName

I had committed some resharper files, and did not want those to persist for other project users.

查看更多
冷夜・残月
5楼-- · 2018-12-31 08:11

A more generic solution:

  1. Edit .gitignore file.

    ECHO mylogfile.log >> .gitignore

  2. Remove all items from index.

    git rm -r -f --cached .

  3. Rebuild index.

    git add .

  4. Make new commit

    git commit -m "Removed mylogfile.log"

查看更多
倾城一夜雪
6楼-- · 2018-12-31 08:15

If you want to just untrack a file and not delete from local and remote repo then use thhis command:

git update-index --assume-unchanged  file_name_with_path
查看更多
不流泪的眼
7楼-- · 2018-12-31 08:18

Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that: http://help.github.com/remove-sensitive-data/

查看更多
登录 后发表回答