我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
为文件 / 文件夹执行下列步骤:
删除文件:
需要添加该文件到.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
其他回答
如果您创建了 gitignore 文件,使用命令如 echo node_modules >>.gitignore,它不会工作。
窗口终端存储文件在 UCS-2 LE BOM 和 git 似乎不接受它。
您可以通过在笔记本中打开并使用 UTF-8 编码重新存储。
此分類上一篇
它现在工作。
我认为他们需要纠正这一点,因为做echo“filetoignore” >>.gitignore实际上看起来非常有用
移动或复制文件到一个安全的位置,所以你不会失去它。
文件仍然会出现,如果你转到以前的任务之一,或另一个分支,它没有被删除. 但是,在所有未来的任务,你不会再看到文件. 如果文件在 Git 忽略,那么你可以将其返回文件夹, Git 不会看到它。
在已承诺的DS_Store的情况下:
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
忽略它们:
echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
最后,做一个承诺!
我喜欢JonBrave的答案,但我有足够的工作目录,承诺 - 一个让我有点害怕,所以这里是我做的事情:
git config --global alias.exclude-ignored '!git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached && git ls-files -z --ignored --exclude-standard | xargs -0 git stage && git stage .gitignore && git commit -m "new gitignore and remove ignored files from index"'
打破它:
git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git ls-files -z --ignored --exclude-standard | xargs -0 git stage
git stage .gitignore
git commit -m "new gitignore and remove ignored files from index"
删除被忽略的文件从索引阶段.gitignore 和您刚刚删除的文件承诺
对于我来说,文件在历史上仍然可用,我首先需要清除添加删除文件的命令: 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
推荐文章
- GitHub -未能连接到GitHub 443 windows/连接到GitHub失败-无错误
- 如何使Eclipse/EGit在更新后识别现有的存储库信息?
- 撤销意外git隐藏pop
- 我怎么能让詹金斯CI与Git触发器推到主人?
- Git:哪个是分支的默认配置远程?
- 如何从拉请求中删除提交
- 如何用分发文件发布npm包?
- 跟踪在GitHub上创建的一个新的远程分支
- 没有空格的合并会产生冲突
- 如何使用refspec将Git标记推到分支?
- 如何使用Git向远程存储库进行初始推送?
- 将文件的当前工作副本与另一个分支提交的副本区别对待
- 配置Git接受特定https远程的特定自签名服务器证书
- 如何将一个特定的提交从一个分支合并到Git中的另一个分支?
- 通过SSH配置Git登录一次