jan*_*anw 550 git github gitignore
我提交并将一些目录推送到github.之后,我更改了.gitignore
文件,添加了一个应该被忽略的目录.一切正常,但(现在被忽略)目录保留在github上.
如何从github和存储库历史记录中删除该目录?
Mar*_*air 977
.gitignore
文件中的规则仅适用于未跟踪的文件.由于该目录下的文件已经在您的存储库中提交,您必须取消它们,创建一个提交,并将其推送到GitHub:
git rm -r --cached some-directory
git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master
Run Code Online (Sandbox Code Playgroud)
如果不重写存储库的历史记录,则无法从历史记录中删除该文件 - 如果其他人正在使用您的存储库,或者您正在从多台计算机上使用它,则不应该执行此操作.如果您仍想这样做,您可以使用git filter-branch
重写历史记录 - 这里有一个有用的指南.
另外,请注意输出git rm -r --cached some-directory
将是:
rm 'some-directory/product/cache/1/small_image/130x130/small_image.jpg'
rm 'some-directory/product/cache/1/small_image/135x/small_image.jpg'
rm 'some-directory/.htaccess'
rm 'some-directory/logo.jpg'
Run Code Online (Sandbox Code Playgroud)
这rm
是来自git的关于存储库的反馈; 文件仍在工作目录中.
Blu*_*ell 233
我这样做:
git rm --cached `git ls-files -i --exclude-from=.gitignore`
git commit -m 'Removed all files that are in the .gitignore'
git push origin master
Run Code Online (Sandbox Code Playgroud)
这将删除你的git忽略的所有文件/文件夹,省去你必须手动选择每一个
这似乎已停止为我工作,我现在做:
git rm -r --cached .
git add .
git commit -m 'Removed all files that are in the .gitignore'
git push origin master
Run Code Online (Sandbox Code Playgroud)
eir*_*ios 47
根据我在这里的答案:如何从git存储库中删除目录?
删除目录的步骤
git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master
Run Code Online (Sandbox Code Playgroud)
在下次提交中忽略该文件夹的步骤
要在下次提交时忽略该文件夹,请在根目录中创建一个名为.gitignore的文件 ,并将该文件夹名称放入其中.您可以根据需要添加任意数量
.gitignore文件将如下所示
/FolderName
Run Code Online (Sandbox Code Playgroud)
Blundell的第一个答案对我不起作用.然而它向我展示了正确的方法.我做了同样的事情:
> for i in `git ls-files -i --exclude-from=.gitignore`; do git rm --cached $i; done
> git commit -m 'Removed all files that are in the .gitignore'
> git push origin master
Run Code Online (Sandbox Code Playgroud)
我建议您先运行以下语句检查要删除的文件:
git ls-files -i --exclude-from=.gitignore
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio的默认.gitignore文件,我注意到它删除了项目中的所有日志和bin文件夹,这不是我想要的操作.
此方法应用标准 .gitignore 行为,不需要手动指定需要忽略的文件。
不能再使用
--exclude-from=.gitignore
了:/ - 这是更新的方法:一般建议:从一个干净的 repo 开始- 提交的所有内容,工作目录或索引中没有任何未决的内容,并进行备份!
#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"
#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *
#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
git commit -m "re-applied modified .gitignore"
#other devs who pull after this commit is pushed will see the newly-.gitignored files DELETED
Run Code Online (Sandbox Code Playgroud)
如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从未来的拉取中删除新忽略的文件,请参阅此答案。
布伦德尔的答案应该起作用,但出于某些奇怪的原因,它与我无关。我必须先将第一个命令输出的文件名通过管道传送到文件中,然后遍历该文件并逐个删除该文件。
git ls-files -i --exclude-from=.gitignore > to_remove.txt
while read line; do `git rm -r --cached "$line"`; done < to_remove.txt
rm to_remove.txt
git commit -m 'Removed all files that are in the .gitignore'
git push origin master
Run Code Online (Sandbox Code Playgroud)
注意:此解决方案仅适用于Github Desktop GUI。
通过使用Github Desktop GUI,它非常简单。
暂时将文件夹移到另一个位置(移出项目文件夹)。
编辑.gitignore
文件并删除文件夹条目,该条目将在github页面上删除主存储库。
提交并同步项目文件夹。
将文件夹重新移到项目文件夹中
重新编辑.gitignore
文件。
就这样。
归档时间: |
|
查看次数: |
214525 次 |
最近记录: |