不小心将 .idea 目录文件提交到 git 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11124053/
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-10 13:58:47  来源:igfitidea点击:

Accidentally committed .idea directory files into git

git

提问by sethu

I have accidentally committed the .idea/directory into git. This is causing conflicts everywhere else I need to checkout my repo. I was wondering how do I remove these files from the remote?

我不小心将.idea/目录提交到 git 中。这在我需要检查我的回购的其他地方引起冲突。我想知道如何从遥控器中删除这些文件?

I still need these files locally since the Intellij IDE needs them. I just don't want them in the remote. I have added the directory .idea/to my .gitignoreand committed and pushed this file into remote. This seems to have no effect during my checkout on my other machine though. I still get the error message:

我在本地仍然需要这些文件,因为 Intellij IDE 需要它们。我只是不想让他们在遥控器上。我已将该目录添加.idea/到我的.gitignore并提交并将此文件推送到远程。不过,这在我在另一台机器上结账时似乎没有影响。我仍然收到错误消息:

error: The following untracked working tree files would be overwritten by checkout:
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/uiDesigner.xml
.idea/vcs.xml
.idea/workspace.xml

回答by Tadeck

Add .idea directory to the list of ignored files

将 .idea 目录添加到忽略文件列表中

First, add it to .gitignore, so it is not accidentally committed by you (or someone else) again:

首先,将其添加到.gitignore,以免您(或其他人)再次意外提交:

.idea

Remove it from repository

从存储库中删除它

Second, remove the directory only from the repository, but do not delete it locally. To achieve that, do what is listed here:

其次,仅从存储库中删除目录,但不要在本地删除它。为此,请执行此处列出的操作:

Remove a file from a Git repository without deleting it from the local filesystem

从 Git 存储库中删除文件而不从本地文件系统中删除它

Send the change to others

将更改发送给其他人

Third, commit the .gitignorefile and the removal of .ideafrom the repository. After that push it to the remote(s).

第三,提交.gitignore文件并.idea从存储库中移除。之后将其推送到遥控器。

Summary

概括

The full process would look like this:

整个过程如下所示:

$ echo '.idea' >> .gitignore
$ git rm -r --cached .idea
$ git add .gitignore
$ git commit -m '(some message stating you added .idea to ignored entries)'
$ git push

(optionally you can replace last line with git push some_remote, where some_remoteis the name of the remote you want to push to)

(可选地,您可以用 替换最后一行git push some_remote,其中some_remote是您要推送到的遥控器的名称)

回答by Ricardo Souza

You can remove it from the repo and commit the change.

您可以将其从 repo 中删除并提交更改。

git rm .idea/ -r --cached
git add -u .idea/
git commit -m "Removed the .idea folder"

After that, you can push it to the remote and every checkout/clone after that will be ok.

之后,您可以将其推送到遥控器,之后的每个结帐/克隆都可以。

回答by Hossein

You should add a .gitignorefile to your project and add /.ideato it. You should add each directory / file in one line.

您应该将.gitignore文件添加到您的项目中并添加/.idea到其中。您应该在一行中添加每个目录/文件。

If you have an existing .gitignorefile then you should simply add a new line to the file and put /.ideato the new line.

如果您有一个现有的.gitignore文件,那么您应该简单地在文件中添加一个新行并放入/.idea新行。

After that run git rm -r --cached .ideacommand.

在那之后运行git rm -r --cached .idea命令。

If you faced an error you can run git rm -r -f --cached .ideacommand. After all run git add .and then git commit -m "Removed .idea directory and added a .gitignore file"and finally push the changes by running git pushcommand.

如果遇到错误,可以运行git rm -r -f --cached .idea命令。毕竟运行git add .然后git commit -m "Removed .idea directory and added a .gitignore file"最后通过运行git push命令推送更改。

回答by rahulnikhare

Its better to perform this over Master branch

最好在 Master 分支上执行此操作

Edit .gitignore file. Add the below line in it.

编辑 .gitignore 文件。在其中添加以下行。

.idea

.idea

Remove .idea folder from remote repo. using below command.

从远程仓库中删除 .idea 文件夹。使用下面的命令。

git rm -r --cached .idea

git rm -r --cached .idea

For more info. reference: Removing Files from a Git Repository Without Actually Deleting Them

欲了解更多信息。参考:从 Git 存储库中删除文件而不实际删除它们

Stage .gitignore file. Using below command

暂存 .gitignore 文件。使用以下命令

git add .gitignore

git add .gitignore

Commit

犯罪

git commit -m 'Removed .idea folder'

git commit -m 'Removed .idea folder'

Push to remote

推送到远程

git push origin master

git push origin master