如何在 Android Studio 中正确使用 Git? [英] How to use Git correctly in Android Studio?

查看:28
本文介绍了如何在 Android Studio 中正确使用 Git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个关于 stackoverflow 的热门话题,但我就是不知道如何进行这项工作.我想:

I know this is a much-discussed topic on stackoverflow, but I just can't figure out how to make this work. I would like to:

  1. 创建一个 Android Studio 项目
  2. 使用 git 签入项目
  3. 将项目推送到 bitbucket
  4. 将项目拉到另一台计算机上

这是我正在使用的 .gitignore(基本上是 Android Studio 创建的,我删除了 *.iml 文件 - 因为它不起作用)

This is the .gitignore I'm using (basically the Android Studio created one, where I removed the *.iml files - cause that didn't work)

.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

但不知何故,它就是行不通.无论我的 gitignore 文件是什么样的,Android Studio 都无法识别该项目.

But somehow, it just won't work. Android Studio won't recognise the Project, no matter what my gitignore file looks like.

那么实际的问题:如何将 Android Studio 项目推送到 git,以便我可以简单地将其拉入另一个客户端以继续工作?

So actual question: how do I push an Android Studio project to git so I can simply pull it into another client to continue working?

下面是我做事的方法,一步一步:

So here’s how I do things, step-by-step:

  1. 在 Android Studio 中创建项目 GitTest
  2. 在 Bitbucket 上创建一个 git 存储库
  3. 在 Android Studio 中:选择启用版本控制集成" - 选择git"
  4. 浏览到 gitignore 文件并将其更改为与 Mauker 的匹配
  5. 在左侧的项目"下选择项目",然后在 Git 下选择添加"

  1. create project GitTest in Android Studio
  2. create a git repository on Bitbucket
  3. in Android Studio: select "enable version control integration" - pick "git"
  4. browse to the gitignore files and change it to match Mauker’s
  5. under "project on the left" select "Project" then under Git "Add"

提交/推送更改(在进程中定义远程仓库)

commit/push the changes (define the remote repo in the process)

此时,项目在bitbucket上.接下来是导入项目"部分:

at this point, the project is on bitbucket. Next comes the "import project" part:

  1. 打开安卓工作室
  2. 选择从版本控制检出项目"
  3. 从 Gradle 导入项目(使用推荐的默认成绩包装器)
  4. 检测到未注册的 VCS 根 -> 添加根

项目加载,我得到某种 NullPointerException

The project loads, I get some sort of NullPointerException

推荐答案

在另一台计算机上,您可以尝试导入项目,而不是打开它.

On the other computer you could try to import the project, instead of opening it.

实际上,您不必将项目特定文件提交到您的 git 存储库.Android Studio 足够智能,可以从许多不同的来源导入项目.

Actually, you don't have to commit project specific files to your git repo. Android Studio is smart enough to import a project from many different sources.

使用 IntelliJ IDEA,您不仅可以从从头开始,但也导入使用其他开发的现有项目工具.最常见的场景之一是导入现有的Android-Gradle 项目.不过也可以导入一个Maven、Eclipse或 Flash Builder 项目,甚至从一堆源文件.

With IntelliJ IDEA you can not only create an Android project from scratch, but also import an existing project developed using other tools. One of the most common scenarios is importing an existing Android-Gradle project. However, you can also import a Maven, Eclipse or Flash Builder project, or even build a new project from a bunch of source files.

就像这样:

这里有一个很好的更详细的教程 关于如何从源和其他地方导入 Android Studio 上的项目.

And here's a nice and more detailed tutorial on how to import a project on Android Studio from source and other places.

对于您的 .gitignore 文件,我强烈建议您使用 gitignore 获取一个.io 网站.

For your .gitignore file I strongly recommend you to get one using gitignore.io website.

此外,如果您已经提交了不应该存在的文件,请检查 这个问题,看看如何删除它们.

Also, if you already commited files that are not supposed to be there, check this question and see how you can remove them.

这是我在我的一个项目中使用的 .gitignore 文件的示例.

Here's an example of a .gitignore file that I use on one of my projects.

# Created by https://www.gitignore.io/api/android,osx,windows,linux,intellij,java

### Android ###
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

### Android Patch ###
gen-external-apklibs


### OSX ###
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

这篇关于如何在 Android Studio 中正确使用 Git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆