我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
這不再是最新 Git 的問題(v2.17.1 在寫作時)。
.gitignore 文件最终忽略了跟踪但删除的文件. 您可以通过运行下列脚本来为自己测试此文件. 最终 git 状态声明应该报告“没有任何承诺”。
# Create an empty repository
mkdir gitignore-test
cd gitignore-test
git init
# Create a file and commit it
echo "hello" > file
git add file
git commit -m initial
# Add the file to gitignore and commit
echo "file" > .gitignore
git add .gitignore
git commit -m gitignore
# Remove the file and commit
git rm file
git commit -m "removed file"
# Reintroduce the file and check status.
# .gitignore is now respected - status reports "nothing to commit".
echo "hello" > file
git status
其他回答
为文件 / 文件夹执行下列步骤:
删除文件:
需要添加该文件到.gitignore. 需要删除该文件使用命令(git rm --cached 文件名)。 需要运行(git add.). 需要(commit -m)“文件删除”。 最后,(git push)。
例如:
我想删除 test.txt 文件. 我偶然推到 GitHub 并想删除它. 命令将如下:
首先,在.gitignore 文件中添加“test.txt”
git rm --cached test.txt
git add .
git commit -m "test.txt removed"
git push
删除文件夹:
需要添加该文件夹到.gitignore 文件夹. 需要删除该文件夹使用命令(git rm -r --cached 文件夹名称)。 需要运行(git add.). 需要(commit -m)“文件夹删除”。 最后,(git push)。
例如:
我想删除.idea 文件夹/目录. 我偶然推到 GitHub 并想删除它。
首先,在.gitignore 文件中添加.idea
git rm -r --cached .idea
git add .
git commit -m ".idea removed"
git push
下面的命令系列将从 Git 索引中删除所有项目(不是工作目录或本地存储库),然后更新 Git 索引,同时遵守 Git 忽略。
首先:
git rm -r --cached .
git add .
然后:
git commit -am "Remove ignored files"
或者作为一个单线:
git rm -r --cached . && git add . && git commit -am "Remove ignored files"
我总是使用这个命令去删除那些未追踪的文件. 单线,Unix风格,清洁输出:
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached
它列出所有被忽略的文件,取代每个输出线与引用的线,而不是处理路径的空间,并将一切转移到 git rm -r -cached 以从指数中删除路径 / 文件 / 指南。
什么不为我工作
(在Linux下),我想使用这里的帖子,建议Ls - 文件 - 被忽略 - 排除 - 标准 - xargs git rm -r - 被捕的方法。
git ls-files --ignored --exclude-standard | xargs -d"\n" git rm --cached
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached
处理此情况(获取未找到文件的错误)。
所以我提供
git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git commit -am "Remove ignored files"
这使用 -z 对 ls 文件的论点,以及 -0 对 xargs 的论点,以便在文件名中安全/正确地输入“不良”字符。
在 git-ls-file(1)的手册页面上,它说:
当 -z 选项未使用时,在 pathnames 中,TAB、LF 和 backslash 字符分别以 \t、 \n 和 \\ 表示。
所以我认为我的解决方案是必要的,如果有这些字符中的任何一个字符。
对于我来说,文件在历史上仍然可用,我首先需要清除添加删除文件的命令: https://gist.github.com/patik/b8a9dc5cd356f9f6f980
下面的例子结合了最后3个命令
git reset --soft HEAD~3
git commit -m "New message for the combined commit"
推破的承诺 如果承诺已推到远程:
git push origin +name-of-branch
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别