Android Studio uses Git and Github

foreword

Recently, I am studying how to store the code in Git and push it to Github. There are many tutorials on the use of Git on the Internet, but most of them use Git bash statements. Since the author usually uses Android Studio a lot, and Android Studio integrates the function of linking projects to Git local warehouses and Github remote warehouses, it is more convenient to use Android Studio directly. The author stepped on a lot of pitfalls in the past two days to get it right, so write it down for your reference.

Download the Git client

1. There is no need to say much about this part. First of all, there needs to be a Git client in the computer. The download link is:
https://www.git-scm.com/downloads
When downloading, you can modify the download directory according to your own needs. Others can be directly clicked The default configuration is fine.
2. After downloading, open the Git client and enter the following three lines of code:

git init

The function of this statement is roughly to enter the "master" mode, which is convenient for subsequent settings;

git config --global user.email "**********"

The function of this statement is to set the email address of the Git user, where "**********" is your own email address.

git config --global user.name "username"

The function of this statement is to set the username of the Git user, where "username" is your own name.
After the above three statements are entered, Git is set up; you can check it with the following statement:

git config --list

Create a warehouse in Github

First log in to your Github account, and then create a warehouse. Paste the URL:
https://github.com
The creation process is as follows:
1. Click the "+" sign in the upper right corner of the github homepage, and click "New repository";
Create a new warehouse on the homepage
2. Enter relevant information on the creation page. Among them, "owner" is the name of your own Github account; "repository name" is the name of the newly created warehouse; other things basically do not need to be changed, just click "Create repository" after inputting;
creation process

Committing to Git from Android Studio

For the convenience of demonstration, we create a new Android project for demonstration:
Create a new Android project
1. Open "file" - "settings" - "version control" - "git", click "..." to add " git.exe":
Click "..."
2. Click "Test" in the above picture, make sure that the association is successful before proceeding to the next step. The screenshot of the successful association is as follows:
Association succeeded
3. Return to the Android Studio programming interface, select the project to be added to the Git local warehouse, For example, here we directly select the entire "GitTest", and then right-click - "Git" - "add" to associate the project with the Git local warehouse:
add
4. Also select the project, right-click - "Git" - "Commit Directory" - "Commit ", so far, the GitTest project has been submitted to the local warehouse:
Commit Directory
commit

Push from Android Studio to Github

1. Right-click "GitTest" - "Git" - "Repository" - "Push" to enter the Push interface:
Push
2. Click "Define remote" to set:
Define remote
3. Fill in the URL (the URL is the address of the project on github, which can be found in See the search bar, as shown below), click "OK":
URL
URL settings
4. Click "Push", you're done! At this point, refresh the project in github, and you can see that the code has been successfully uploaded:
success

some pits

During the last step of "Push", "Push Rejected" may be displayed. The solution is as follows:
1. Right-click on the project - "Git" - "Repository" - "Fetch":
fetch
2. In the Terminal window of Android Studio, perform Settings, specifically enter the following codes in sequence:

git branch --set-upstream-to origin/master
git pull --allow-unrelated-histories

3. This problem should be solved. It should be set as follows at the first time, and then you can push directly.

postscript

The above is only a relatively elementary usage, and the author is just getting started. If you have any questions or comments, please leave a message for discussion.
——————————————————————————————
Finally, post my personal public account: WeChat search "Chaqian" or scan the picture below. Usually, some programming-related articles will be updated, and everyone is welcome to pay attention~

tea move

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/110164635