Perhaps the most detailed article about Android studio using Git

Source: Internet
Author: User

Objective

This article is long, the picture is many many, the flow party cautions into
It's been a while since I've been using git for a while, but it's been used before, and Git Bash using Git in Android studio is not a habit at first, like using SVN to git for a while, thinking about Android studio for a while, Also saw I think a few but very quality introduction as books 《Android Studio实战 快速、高效地构建Android应用》 , strong Amway Everyone Oh! So I wrote this article to work with you to learn how to use git efficiently in Android studio. In addition, if you want to learn, you can fork my project directly.
Another point to note is the use of Android Studio shortcuts in this article, if you are unfamiliar, you can look at one of my other articles.

Install Git

I'm using windows, so I can only cover the process of installing Git under Windows.
Click here to choose your version, directly installed on the line.
After the installation is complete, it is found in the Start menu Git-->Git Bash , and if a dialog box similar to the following appears, it proves that the installation was successful.

After the installation is successful, you need to configure your information and enter the following command directly on the command line above:

$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"

Attention:
1. Change the top to Your Name your own name and start at random.
2. Replace the above with [email protected] your own mailbox.
3. To tell the parameters of the git config command --global , use this parameter, indicating that all of your git repositories on this machine will use this configuration, of course, you can also specify a different user name and email address of a warehouse.

Android Studio Configuration git

Use the shortcut key Ctrl+Alt+S to open settings, and then click on Version Control --> Git on the Path To Git Executable enter Git location, such as:

After clicking on the test button next to it, if the following success prompt appears, the GIT configuration is successful.

Then follow the configuration of your GitHub account password, enter the completion of the click on the Test button, if the account password is correct, you will be prompted to succeed, then you can continue to go down:

Create a local warehouse

Whether you are a newly built project or a project that has already been written, the operation is the same.
Here I would like to create a new project named Mygit as an example, if you want to practice, you can fork a bit here.
For a new project, locate the VCs on the menu bar and follow the steps:

Select the directory where the project's root directory is initialized for git:

After initialization you will find that the original file name is white and now it is Brown , which means that the file has been tracked by git, but not added to the repository:

Ignore file

We know that when git Initializes a repository, it automatically generates a. gitignore file, which is used to ignore files that are not added to the repository, and in our project we generate a total of two. gitignore files, respectively, under the project root directory, and under the app folder. We can edit these files to indicate which files we need to ignore, but in general we choose the default as well, unless you need to make the appropriate modifications:

Add File

There are four ways to add files, like git commands, git add in Android studio, but they are all the same, let me take one example:

1. Select the root directory of the project, right-click Git, and then select Add, such as:

2. Select the project root directory and click on the VCs menu in the menu bar to select Git-->add, such as:

3. Use the shortcut key Alt+9 , or tap the Version Control Tool button at the bottom of the screen
Open the Version Control window, you can see that it should look like, the window has two drop-down bar, one is the default, to record the files that have been added, and the other is unversioned files, used to record the files that have been tracked but not added.

Right-click unversioned files and select Git-->add, such as:

4. Using shortcut keysCtrl+Alt+A

Add after the file name into a green , this is the representative has been added to the warehouse as a file, then you can commit the file, use the shortcut key Ctrl+K or select the project root right-click Git-->Commit Directory to bring up the commit window, as shown below, Select the file you want to submit, fill in the submitted information, and in the Author text box you can fill in the name of the operator who submitted the submission, and if not, it will default to the user name that was previously configured for the GitHub account. As you can see, the file name changes back to the familiar white after the commit.

After the submission is successful, you can use the shortcut keys Alt+9 , or click the Version Control Tool button at the bottom of the screen to switch to the Log menu to view the log logs as shown in:

How to Clone a project

It's easy to use a Git clone project locally, as in Android Studio, first find your favorite item, fork it into your own warehouse, click the Clone or download button and copy the address as shown:

Next go back to the Android Studio, follow the action can open the Clone dialog box, in the Address bar to paste the address just copied, click the Test button, testing can pass, if successful, so long can click Clone Import Project.

* * Back to our initial Mygit project, first we need to mimic the use of git in daily development, for example, we often work on the branch, so it is very important to master the process and skill of branch work. Next I'll use specific examples to learn how to use branching for development on Android studio.
Here's an example of how git works, that is: Git Flow If you are not very familiar with this, then recommend you Ruan Feng Teacher's article. **

Git Flow

Git flow is a widely used workflow

His main features are two:

1. First, there are two long-term branches of the project

  • Master Branch Master
    • Development Branch Dev

The former is used for the release of the release, any time in this branch to get a stable distribution version, the latter for daily development, storage of the latest development version.

2. Second, there are three short-term branches of the project

  • Function branch (feature branch)
    • Patch Branch (Hotfix branch)
    • Pre-Send branch (release branch)
New features show Hello World

As we described in the introduction of Git flow, the master branch is just for the release of the product and will not be used in the usual development, but only with the Dev branch, but if we have a new feature, it will typically create a branch of that feature in the Dev branch, so we should do so.

Create Dev, feature-1 branch, and we need to go to the Feature-1 branch

In Android Studio, we can easily manage the branch, in the bottom right corner of the main interface, click Git can appear the current branch, default to master, we select New Branch , as shown:

In the popup dialog we type feature-1 , click OK, so that we not only create a new Feature-1 branch, and we are in that branch, and then follow the same method of creating the Dev branch, if not unexpected, we should now be on the Dev branch, But since we are now going to develop feature 1, we have to switch to feature-1 the branch and follow the action, we can go back to the Feature-1 branch.

Next open log, we should be able to see the following scenario:

You can see that we now have three branches: master, Dev, feature-1, but as indicates that we should have four branches, in fact the head is the cursor of the current active branch. The memory of the image is: where you are now, HEAD points to where, so Git knows you are there! But HEAD doesn't just point to the top of the branch, it can actually point to any node, which is the tag inside Git to track the current position. We can use the following diagram to illustrate the current branch situation:

Complete the function 1
/**   * feature-1    *   @param view      */ public  void  feature1  (View view) {if  (Btn_feature_1. (). tostring  (). equals  ()) {btn_feature_1. settext  (R.string .        feature_1_dis ); } else  {btn_feature_1. (R.string . ); } }

All right, feature 1 writing is done, then submit it! Old method: Ctrl+k to submit, according to fill in the submission information, each time the submission of information is best to be detailed and format specifications, so later to view the log when it will be more convenient.

Once you have clicked the Commit button, you can see that the current log chart has become:

We can open the right side of the log graph, and he lists all the files that are currently being tracked by Git, and we select Mainactivity.java and click the second button on the top Show Diff(显示差异) :

In the pop-up interface you can show the difference between the current version of the file and the file in the Master branch, and we can use the Esc shortcut key to exit the interface as shown in:

Merging feature-1 branches onto the Dev branch

As shown, check the dev branch and select checkout:

You will find that the code is now back in its first state, now select the Feature-1 branch and select Merge, ready to merge the Feature-1 branch onto the Dev branch:

Again, look at the log chart, he looks like this, you can see now that the Feature-1 branch has been merged with the Dev branch, and now they are in the same state:

Finally, we need to remove the Feature-1 branch that has completed the task so that the branch is too cluttered to manage.

New features 2--display Hello Android

In the same way, we can do this in the following steps:

1. Move out of the Dev branch, and feature-2 then move out of the branch after the new branch feature-2 , the specific steps I will no longer demonstrate, as above is the same.

2. Start the code.

 /**   * feature-2    *   @param View */ public  void  feature 2  (view view) {if  (Btn_feature_2. (). tostring  (). equals  ()) {btn_feature_2. settext  (R.string .        feature_2_dis ); } else  {btn_feature_2. (R.string . ); } }

3. Use the shortcut key ctrl+k to submit the feature-2 changes and fill in the submission information.

4. Switch back to the dev branch and delete the Feature-2 branch after merging the Feature-2 branch.
If it doesn't go wrong, your log should look like this:

Adding functionality to the Dev branch 3--hello Java

In the same way, I'm not going to do one by one demos here, and if you follow the steps above, the last log should look like this:

Focus, the boss said, the new development of the function 3 do not like, need to remove the function 3, how to do?

This is where git fallback comes in, and there are two ways to fallback in Android studio: git revert and git reset.

Git revert

Git revert can rewind the version back to the previous step, but adds a new commit, and his process is like this:

1. First open log, find the function 3 of the submission, right-click the Copy hash code (copy Revision number), as shown in:

2. Open Android Studio Terminal Terminal, he is next to version control, then enter the following command to press ENTER:

git revert 9c834d8c66598fb132a0cc8e4c1f8c341d058f3e

That's a very complicated digital alphabet, the hash code we just copied.

As shown in the following:

3. After the terminal will list the specific information of this submission, if you confirm that you want to rollback, please enter :q Save this operation and exit the session
Now you can see that he did add a commit and go back to the previous version of the content, log should be like this:

Git Reset

In contrast, git reset is much more straightforward, and as with git revert, it can restore the code to the previous version, but it won't add a single commit, and his process is as follows:

Because we need to remove feature 3 and let log appear to have no revert of this commit, we should take two steps back on the Dev branch, that's exactly right!

1. Click on the menu bar VCS-->Git-->Reset HEAD , open the dialog box and enter it in the To commit text box, HEAD~2 like this:


2. After clicking the reset button, you can see that log has changed to the original familiar image:

The master branch has been modified

Suddenly you find that your colleague has submitted two times on the master branch, respectively, with the addition of function 1 and function 4, but the function 1 is clearly HelloWorld written WorldHello , such as:

 /**   * feature-1    *   @param view      */ public  void  feature1  (View view) {btn_feature_1. settext  (R.string .    feature_1_dis ); } /**   * feature-4   * display Hello   *   @param view   */ public void  feature4  (view view) {Btn_feature_4. (R.string . ); }

Commit the changes, and then log should look like this:

We still use the demo graph to indicate the current branch development:

Rebase

The boss said, Master branch as long as function 4 does not require function 1, and the dev branch of the functions 1, 2 are all merged to the master branch. Then we can use rebase at this time.
Git rebase is used to merge changes from one branch into the current branch. Now that we've switched to the master branch and added the changes from Dev to master, we chose Rebase, which provides a very powerful rebase in Android studio.

1. Click Vcs-->git-->rebase on the menu bar, as shown in:

2. In the popup dialog, we select the Dev branch in the onto drop-down bar to indicate that we need to rebase the master branch to that branch.

3. Rebase after clicking, you will find the Android Studio Pop-up dialog box showing two commits of the Master branch, which requires us to make a selection as shown in:

4. Because we do not need the function on the Master branch 1 but need function 4, so in the submission of function 1 we choose skip (skip this commit), on the submission 4 Select pick (pick this commit), click Start Rebasing , we can see the dialog box popup, This time, let's pick each file, as shown in:

5. For each file, you can choose to accept your part, or accept the content on the other branch, or you can click the merge to filter the file. We click on the Merge button to see that there are three files rendered on the screen. The middle of the file is the final result, the left side of the current branch of the master branch, the right side of the dev branch, you can also find that in each modified row there is a X >> symbol, click to X indicate that do not need this line of modification, click to >> accept the change of this line, We can even copy, paste, and edit the content as we did in the editor, and we'll make the final choice as shown, then click Apply to save it, and if you don't want to save it, click Abort to terminate this modification:

6. For the remaining two files to do the same processing, we can see that the master branch has the dev branch of the function 1 and function 2 and its own function 4, and removed its previous function 1, you can look at the log, as follows:

We still use the demo diagram to indicate the final branching situation:

Push to remote Warehouse

Push is simple, you can navigate to the menu bar VCS-->Import Into Version Control -->Share Project on Github as shown in:

In the popup dialog box, fill in the name of the remote repository and click Share:

Then you can see this push on your github.

How local modifications are synchronized to the remote repository

Now I need to add some files to my project, for example, I've created a new screenshots folder and added this article to it, so how do I sync these files to the remote repository? It's actually very simple.

1. Use the shortcut key Alt+9 or click the tool button to open Version Control , right Unversioned Files , select Git-->Add , add all the files to git, such as:

2. Right-click Default, select Commit all files and fill in the submission information, such as:

3. Using the shortcut key Ctrl+Shift+K or right-click the project root, select the push project as shown in:

4. Click the button in the pop-up dialog to Push Sync your changes to the remote repository, such as:

5. For the modified files want to sync to the remote repository, follow the same steps on the line, here no longer repeat.

Postscript

This article is a bit long, a lot of pictures, I also wrote for a long time, sometimes the idea is not clear also mixed (embarrassing face), so inevitably there will be mistakes, but also please criticize correct, we learn from each other, I hope you can learn more knowledge of Android studio.

Perhaps the most detailed article about Android studio using Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.