简体   繁体   English

Android Studio Git .gitignore vs 项目 > 设置 > 版本控制 > 忽略的文件

[英]Android Studio Git .gitignore vs Project > Settings > Version Control > Ignored Files

I am somewhat confused, I noticed that upon setting up my first Git repository in Android Studio all of the correct files to ignore were automatically added to the Project's Settings under Settings > Version Control > Ignored Files.我有点困惑,我注意到在 Android Studio 中设置我的第一个 Git 存储库时,所有要忽略的正确文件都会自动添加到“设置”>“版本控制”>“忽略的文件”下的项目设置中。

This appeared to be magic, I never told it what to ignore.这似乎很神奇,我从来没有告诉它应该忽略什么。 Moreover, there is no .gitignore file in any directory of my project as I'm used to creating.此外,在我习惯创建的项目的任何目录中都没有 .gitignore 文件。

This being said, I can't get my second project to automatically add ignored files, and really, I just want to use the simple .gitignore method.话虽如此,我无法让我的第二个项目自动添加被忽略的文件,实际上,我只想使用简单的 .gitignore 方法。

Does anyone know how to override Android Studio's ignored files list and simply use a .gitignore instead?有谁知道如何覆盖 Android Studio 的忽略文件列表并简单地使用 .gitignore 代替? I'd like the ease of copying my .gitignore to all other projects I make, it does not appear you can copy Settings > Version Control > Ignored Files.我希望能够轻松地将我的 .gitignore 复制到我制作的所有其他项目中,但您似乎无法复制 Settings > Version Control > Ignored Files。

Thank you for commenting on how I can just use .gitignore.感谢您对我如何使用 .gitignore 发表评论。

Ok to answer my own question.好的,回答我自己的问题。 To go back to using .gitignore, do the following:要返回使用 .gitignore,请执行以下操作:

  1. Go to Settings > Version Control > Ignored Files and remove all ignored files.转到设置 > 版本控制 > 忽略的文件并删除所有忽略的文件。 Hit Apply.点击应用。
  2. Still in Settings > Version Control change the type of version control from Git to none.仍然在设置 > 版本控制中,将版本控制的类型从 Git 更改为无。 Hit Apply.点击应用。
  3. Delete the project's .git directory created previously.删除之前创建的项目的 .git 目录。
  4. Create the following .gitignore file in the root directory of your project: https://stackoverflow.com/a/21049338/459095在项目的根目录下创建以下 .gitignore 文件: https : //stackoverflow.com/a/21049338/459095
  5. VCS > Enable Version Control... to Re-enable Git. VCS > 启用版本控制...以重新启用 Git。
  6. Then right click on the project root and select Git > Add to add the entire project.然后右键单击项目根目录并选择 Git > 添加以添加整个项目。

Doing the above will utilize the .gitignore file instead of Settings > Version Control > Ignored Files to commit the right files.执行上述操作将利用 .gitignore 文件而不是 Settings > Version Control > Ignored Files 来提交正确的文件。

For me the process was a little easier:对我来说,这个过程要容易一些:

  1. In Android Studio: Settings > Version Control > Ignored Files > Remove all ignored files > Apply在 Android Studio 中:设置 > 版本控制 > 忽略的文件 > 删除所有忽略的文件 > 应用
  2. Backup .gitignore.备份 .gitignore。 Create an empty new .gitignore.创建一个空的新 .g​​itignore。 (in the root folder) (在根文件夹中)
  3. Restart Android Studio!重启安卓工作室!
  4. Fill up the new .gitignore with the old one.用旧的 .gitignore 填充新的 .gitignore 。 Don't forget to commit the change .gitignore + related ignored/deleted files!不要忘记提交更改 .gitignore + 相关忽略/删除的文件!

No re-enable VCS required!无需重新启用 VCS! I'm working with a GitRepo, which is extremely inconvient to do re-enabling VCS.我正在使用 GitRepo,重新启用 VCS 非常不方便。 The steps helped.步骤有所帮助。

Using command line, this becomes easy :使用命令行,这变得很容易:

> git status > git 状态

On branch master在分支主

No commits yet还没有提交

Untracked files: (use "git add ..." to include in what will be committed)未跟踪的文件:(使用“git add ...”包含在将提交的内容中)

    .gitignore
    .idea/
    app/
    build.gradle
    gradle.properties
    gradle/
    gradlew
    gradlew.bat
    settings.gradle

nothing added to commit but untracked files present (use "git add" to track)没有添加任何提交但存在未跟踪的文件(使用“git add”来跟踪)

git add * git 添加 *

That's it, if you check again you'll see that your files are now added as files to checkin :就是这样,如果您再次检查,您会看到您的文件现在已作为文件添加到 checkin :

git status状态

On branch master在分支主

No commits yet还没有提交

Changes to be committed: (use "git rm --cached ..." to unstage)要提交的更改:(使用“git rm --cached ...”取消暂存)

    new file:   .gitignore
    new file:   .idea/caches/build_file_checksums.ser
    new file:   .idea/codeStyles/Project.xml
    ....

Neither solution here worked for me with the latest build of android studio or rubymine and as far as I can tell this isn't supported by intellij products any more - https://youtrack.jetbrains.com/issue/IDEA-60354 .这里的任何解决方案都不适用于最新版本的 android studio 或 ruby​​mine,据我所知,intellij 产品不再支持此功能 - https://youtrack.jetbrains.com/issue/IDEA-60354

The ignore plugin seems to be the recommended solution to this problem now.忽略插件现在似乎是这个问题的推荐解决方案。 See https://plugins.jetbrains.com/plugin/7495--ignore and http://ignore.hsz.mobi/请参阅https://plugins.jetbrains.com/plugin/7495--ignorehttp://ignore.hsz.mobi/

For me it works:对我来说它有效:

  1. Add files to be ignored (Settings -> Version Control -> Ignored Files -> Remove all ignored files -> Apply).添加要忽略的文件(设置 -> 版本控制 -> 忽略的文件 -> 删除所有忽略的文件 -> 应用)。

  2. Then delete these files in the Android Studio project (after copying in advance).然后将Android Studio项目中的这些文件删除(提前复制后)。

  3. Then copy these files back into the project.然后将这些文件复制回项目中。

  4. They will turn brown color and will not commit to git/github.它们会变成棕色并且不会提交到 git/github。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM