如何在 IntelliJ VCS 中从 Git 中删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35122376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:54:12  来源:igfitidea点击:

How can I remove a file from Git within IntelliJ VCS?

gitintellij-ideaversion-control

提问by Jire

VCS has an Addoption (Git Add) but seems to lack Git Remove.

VCS 有一个Add选项 (Git Add),但似乎缺少 Git Remove。

What's the idiomatic way to Git Remove with VCS?

使用 VCS 进行 Git Remove 的惯用方法是什么?

采纳答案by vikingsteve

Given your project is linked to a git repo already, you can just use the standard intellij feature to "delete" the file.

鉴于您的项目已经链接到一个 git repo,您​​可以只使用标准的 intellij 功能来“删除”文件。

In the project pane, focus the file and hit Deletekey. The "Safe Delete" popup will appear, click OK.

在项目窗格中,聚焦文件并按下Delete键。将出现“安全删除”弹出窗口,单击OK

Now observe under 9: Version Control-> Local Changesthat the file is listed there in "grey" colour - when you commit and push to your gitrepo the file will be deleted on the current branch.

现在观察9: Version Control->Local Changes文件在那里以“灰色”颜色列出 - 当您提交并推送到您的存储git库时,该文件将在当前分支上被删除。

Edit: if these are intellij files, this becomes more difficult.

编辑:如果这些是 intellij 文件,这将变得更加困难。

First, close intellij, make a list of the exact files you want to delete from repo, and take a copy of those files on your local file system.

首先,关闭 intellij,列出要从 repo 中删除的确切文件,并在本地文件系统上复制这些文件。

Then use git rmto remove them and then commit.

然后使用git rm删除它们然后提交。

Next step, add a .gitignorefile to ignore local intellij files. A good start is *.imland .idea.

下一步,添加一个.gitignore文件以忽略本地 intellij 文件。一个好的开始是*.iml.idea

Finally, restore the files that you copied up and restart intellij.

最后,恢复您复制的文件并重新启动intellij。

回答by Kick_the_BUCKET

In the terminal, use git rm --cached -r .idea/. This will remove the files from the GIT index, but leave the files locally.

在终端中,使用git rm --cached -r .idea/. 这将从 GIT 索引中删除文件,但将文件保留在本地。

回答by jyapx

To change a file from green (staged) to red (untracked) using Intellij:

使用 Intellij 将文件从绿色(暂存)更改为红色(未跟踪):

  1. Right click the file(s)
  2. Hover over Git(the git pane will expand)
  3. Click Rollback...(in older versions Revert)
  4. Check that Delete local copies of added filesis not checked and click the Rollbackbutton
  1. 右键单击文件
  2. 悬停Git(git 窗格将展开)
  3. 单击Rollback...(在旧版本中Revert
  4. 检查Delete local copies of added files未选中并单击Rollback按钮

This will not delete the file, only unstage it (remove it from git's index).

这不会删除文件,只会取消暂存(从 gi​​t 的索引中删除它)。

回答by Nicolas Zozol

You may have ADDed too quickly a file to Git, thus your file is green (staged) rather than red (untracked).

您可能将文件ADD过快地添加到 Git,因此您的文件是绿色(暂存)而不是红色(未跟踪)。

The right thing is to use command lines in your terminal. git statuswill hint to make git reset HEAD <file>to untrack your file.

正确的做法是在终端中使用命令行。git status将提示git reset HEAD <file>取消跟踪您的文件。

Then your file will be untracked again (red color).

然后您的文件将再次取消跟踪(红色)。

In case of doubt, you can savebefore your current work with the custom Intellij repo using the menu VCS -> Local History-> Put Label

如有疑问,您可以使用菜单在当前使用自定义 Intellij存储库之前进行保存VCS -> Local History-> Put Label