How to change git branch in Android Studio

AndroidGitAndroid Studio

Android Problem Overview


I have 2 branches in my gitlab project. Now I'm on the master (default) branch. I want to change it to another base, this is what I tried:

When I go to VCS -> Git -> Branches, and click Checkout tag or revision I always get an error :

> pathspec 'my branch' did not match any files known to git

What should I do? All I want to do is to change the branch and do a pull request.

Android Solutions


Solution 1 - Android

To checkout a different branch from within Android Studio:

Under VCS -> Git -> Branches you will see this screen

enter image description here

under Local branches you will see the local branches you can checkout. At the very bottom you see the currently checked out branch (in my case master). To checkout a different branch, select it and click Checkout.

enter image description here

In my example I only have one other local branch, named test. That is why it only shows that one.

After checking out a different branch, this window will pop up in the bottom of the screen

enter image description here


In order to get your remote branches to show up in Android Studio, you first need to fetch them:

VCS -> Git -> Fetch. After that, it will show up

enter image description here

Solution 2 - Android

I've noticed that when I try this flow, and then paste the branch name in, I get:

Couldn't checkout foo: fatal: 'foo^0' is not a valid branch name. I don't what is inserting the ^0 a the end,

So I instead just call git checkout (paste branch name) from the command line, which works.

Solution 3 - Android

Open terminal of AndroidStudio then type

git checkout name_of_branch_you_want_to_switch

Solution 4 - Android

To change Git branch also you can click at the current branch which is checked out at the bottom right corner

https://i.stack.imgur.com/aVV0u.png" height="75" />

Here you can:

  • create and checkout new branch from current branch
  • work with local branches
  • work with remote branches

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlaze TamaView Question on Stackoverflow
Solution 1 - AndroidTimView Answer on Stackoverflow
Solution 2 - AndroidMK.View Answer on Stackoverflow
Solution 3 - AndroidLinhView Answer on Stackoverflow
Solution 4 - AndroidyoAlex5View Answer on Stackoverflow