Git在AndroidStudio使用的详细介绍(英文版)

I’m currently working on developing my skills as an Android programmer, and certainly have quite a bit to learn. I’ve start going through courses on Udemy to gain a foundation that I can build upon. During these courses I have developed some working applications for my Android devices and am progressing fairly well, but I’ll soon be starting my own projects and developing my own applications from scratch.

I realized that I needed a system that will allow me to develop my application across my desktop PC as well as my laptop. My first instinct was to use an application I was already familiar with, Dropbox, and create a shared project workspace that I could access from anywhere. I found out quickly that this was not going to work. The Dropbox system is great, but it just isn’t that great for code development. There seem to be problems with how the reading/writing work with the syncing functionality, and at one point I ran into a severely corrupted project file. Now, because Dropbox tracks file change history, I simply went back and restored a working version of the file.

A quick search confirmed my suspicion about using Dropbox, and directed me toward the use of Git and a remote repository system. Git is a revision control system that keeps track of the changes made to all project files that are included in the repository. With Git, branches can be made from the primary development files, and merged back in once complete. This allows for multiple users to work on the same project with ease. More importantly for me, however, is the ability to push and pull the project information from repositories hosted on-line; allowing me to functionally work across multiple systems.

Android Studio has, in my opinion, great Git integration, and most specifically integration with Github. I created a Github account and found that I have to pay to use their private repository feature. I’m not particularly worried about people finding/stealing my code, but for me I wanted the peace of mind of having the ability to work privately within my repository.

Instead, I turned to Bitbucket for my repository needs. After doing some experimentation, and quite a bit of reading, I have learned how to set up and use Android Studio with Bitbucket, SourceTree, and Git. Following is a quick overview of what it takes to get started using Android Studio with Bitbucket, and getting VCS off the ground for your Android development projects.

 Step 1: Android Studio, Git, Bitbucket, SourceTree.

Under the assumption that you’ve already got Android Studio installed, check to ensure you have Git installed on your computer and linked to Android Studio.

Look in Settings –> Version Control –> Git. Direct the Path to Git executable toward your Git installation git.exe file.

Git在AndroidStudio使用的详细介绍(英文版)_第1张图片

You can find the installation instruction for Git here.

Next we need a Bitbucket account. Head here for the sign up page!

In the tutorial repository provided by Bitbucket it will direct you to download SourceTree, which you can download here.

Once you have everything downloaded, installed, and signed up for move on to creation of a new repository.

Step 2: Creating a repository.

After logging in to the Bitbucket website, at the top of the page click on create, and click on Create repository.

Next, type in the name and a quick description of your project, and hit the Create repository button.

Git在AndroidStudio使用的详细介绍(英文版)_第2张图片

You’ll see a screen that says Repository setup, but just hold tight, we’ll get to that in a moment. Move on to creating a new project in Android Studio.

Step 3: Creating a new project in Android Studio.

Now we are going to create the project we want to work on in Android Studio. If you already have a project you want to use with Git, you can skip the creation step.

Set up your project in Android Studio, I’m just going to run through the setup using the defaults provided.

Git在AndroidStudio使用的详细介绍(英文版)_第3张图片

Once you have your project created, in the top menu click on VCS and then click on Enable Version Control Integration

Android Studio Enable VCS

Select Git for VCS option.

Git在AndroidStudio使用的详细介绍(英文版)_第4张图片

At the bottom you will now have access to the Version Control menu, opening that you will notice that it is telling you that you’ve got unversioned files.

Open up the project level navigation viewer (default will likely be Android) right click your project files, drop down to the Git sub menu and click Add. This will add all of the relevant project files to your Git system.

Git在AndroidStudio使用的详细介绍(英文版)_第5张图片

Upon completion your files will go from red to green.

Now you’re ready to link your project into SourceTree!

Step 4: Linking the project to SourceTree.

Open SourceTree and click on Clone / New

Git在AndroidStudio使用的详细介绍(英文版)_第6张图片

And then click on Add Working Copy. This will allow you to add the project/repository you just made to SourceTree for tracking. Locate your project path, and click add.

Git在AndroidStudio使用的详细介绍(英文版)_第7张图片

You now have a connection between your project file and SourceTree.

Next, we will do our initial commit, and connect our project to Bitbucket.

Step 5: Initial commit, and pushing to Bitbucket repository.

Back in Android Studio hit the VCS menu again and go down to Commit Changes, or press ctrl + k

Git在AndroidStudio使用的详细介绍(英文版)_第8张图片

This will bring up the Commit Menu for our initial commit and push.

Git在AndroidStudio使用的详细介绍(英文版)_第9张图片

At the bottom, hover over the Commit button to get the drop down, what we are looking for right now is Commit and Push.

Click that button and Studio will perform the commit, and bring up the Push menu.

Click on Define remote, it will bring up the menu allowing you to specify the URL of the remote repository.

Going back to Bitbucket, we can grab the URL from the setup page…

Copy your link into the URL prompt in Android Studio. Then click OK.

Git在AndroidStudio使用的详细介绍(英文版)_第10张图片

You should now be able to click Push and push your initial commit to the remote repository.

Check the Event Log in Android Studio to ensure the push was successful…

Refresh your Bitbucket page to ensure the push showed up correctly in the repository.

And look at your SourceTree to confirm that a remote master branch has been created and we are tracking our history.

Git在AndroidStudio使用的详细介绍(英文版)_第11张图片

Now you can track new commits, control checkouts, branches, mergers etc. graphically using SourceTree. You can also control the fetches from the repository, pulls, and commits.

Using both Android Studio and SourceTree is a good way to keep track of the project status and flow, and utilize the powerful tools Git allows you to use.

Git在AndroidStudio使用的详细介绍(英文版)_第12张图片

Git在AndroidStudio使用的详细介绍(英文版)_第13张图片

 

Step 6: Linking to another computer.

Of course, my ultimate goal was to develop across multiple systems, and so if that is your goal as well the next step is to start up Android Studio on your second computer.

From the startup screen you want to start a new project by checking out from version control.

Git在AndroidStudio使用的详细介绍(英文版)_第14张图片

And select Git to bring up the following screen.

Here you must take the URL from your Bitbucket Repository and past it into the URL section. Ensure that the parent directory is where you want your project to land. For the Directory Name, I find it helps to ensure you type the same name as the Directory Name on your original PC. Once everything is in place, click the clone button. The next menu will ask if you want to open the project. Click ‘yes’.

I now suggest adding your project folder to the SourceTree list on this computer.

I’ve made my first Laptop commit, and pushed it to the repository. Everything is synced up in SourceTree and working in Android Studio!

That’s about it. There is a lot more that can be done using these systems. There are other ways to do all of this, but this is how I do it right now, and I hope this guide can help someone else set up Android Studio across multiple systems!

你可能感兴趣的:(Git在AndroidStudio使用的详细介绍(英文版))