如何在 Android Studio 中使用 git branch

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

How to use git branch with Android Studio

androidgitandroid-studiogit-branch

提问by chengluo

I am new to git. I have a very simple scenario for using git. I had my first release written with Android Studio. Now I want to work with some new features. What I did so far:

我是 git 新手。我有一个非常简单的使用 git 的场景。我的第一个版本是用 Android Studio 编写的。现在我想使用一些新功能。到目前为止我做了什么:

  1. enabled the VCS in my Android Studio
  2. created a local repository for my project from Android Studio
  3. pushed my local repository to my Bitbucket remote repository ($git push -u origin master)
  1. 在我的 Android Studio 中启用了 VCS
  2. 从 Android Studio 为我的项目创建了一个本地存储库
  3. 将我的本地存储库推送到我的 Bitbucket 远程存储库 ( $git push -u origin master)

Now I am confused for the next step: create a feature branch. Should I create a branch in the local repository:

现在我对下一步感到困惑:创建一个功能分支。我应该在本地存储库中创建一个分支:

$ git branch --track feature1 origin/master 

or should I create a new branch from the Bitbucket web portal, and clone the new branch?

还是应该从 Bitbucket Web 门户创建一个新分支,然后克隆新分支?

I also want to know how I can switch branches with Android Studio? For example, switch from feature branch to master branch to work on some hotfix. Do I need to use the Bitbucket plugin to checkout the project very time from the remote repository every time I switch branches or I can hot switch it inside Android Studio?

我也想知道如何使用 Android Studio 切换分支?例如,从功能分支切换到主分支以处理一些修补程序。每次切换分支时,我是否需要使用 Bitbucket 插件从远程存储库中检出项目,或者我可以在 Android Studio 中对其进行热切换?

Thanks!

谢谢!

回答by Aegis

You should be able to do this directly from Android studio.

您应该可以直接从 Android Studio 执行此操作。

The easiest way is going to the bottom right cornerof the Android Studio window where you should see the text "Git: branch name", in your case it should say "Git: master". Click on it and it will show a small menu consisting of the different branches available both locally and remotely, also there should be an option "+ New Branch" which will create a new branch for you and switch you to it.

最简单的方法是转到Android Studio 窗口的右下角,您应该在那里看到文本“Git:分支名称”,在您的情况下,它应该显示“Git:master”。单击它,它将显示一个小菜单,其中包含本地和远程可用的不同分支,还应该有一个选项“+ New Branch”,它将为您创建一个新分支并将您切换到它。

You should then be able to change some code, commit it and push it to remote. Merging and checking out branches can also be done from that same menu. The same thing can also be done from the menubar option "VCS"

然后您应该能够更改一些代码,提交它并将其推送到远程。合并和检出分支也可以从同一个菜单中完成。同样的事情也可以从菜单栏选项“VCS”中完成

回答by whyoz

Here's the best way I know to update the remote branches in Android Studio 1.5

这是我所知道的在 Android Studio 1.5 中更新远程分支的最佳方式

1) Go to VCS > Git > Pull (make sure you've pulled your latest changes from master first)

1) 转到 VCS > Git > Pull(确保您已先从 master 拉取最新更改)

2) Click the blue refresh button on this screen enter image description here

2)点击此屏幕上的蓝色刷新按钮 在此处输入图片说明

3) notice all your new branches show up, click on the checkbox of the one you want to switch to and click "Pull" button

3) 注意到你所有的新分支都出现了,点击你想要切换到的分支的复选框,然后点击“拉”按钮

4) go back to the "Git:master" menu in the bottom right of Android Studio, and you'll notice your new branch showed up in the remote section

4) 回到 Android Studio 右下角的“Git:master”菜单,你会注意到你的新分支出现在远程部分

5) click on the branch you want to checkout and select "Check out as new local branch"

5) 点击您要结帐的分支,然后选择“作为新的本地分支结帐”

回答by Shayan Amani

you can do it easily by the shell (Git Bashis recommended) and trying the following for the first time:

您可以通过 shell 轻松完成(推荐使用Git Bash)并首次尝试以下操作:

 git pull [remote] [branch]

for example:

例如:

git pull origin master

after this, you can always do the matter without any trouble like that.

在此之后,您可以随时完成这件事而不会遇到任何麻烦。