Android Studio Git tutorial
Rebase, Merge, git, Rebase, Merge, Git, Merge, Git, Merge Please indicate the source of reprint.
1 Installation and Configuration
It is recommended to use HomeBrew on a MAC to manage packages. Here is how to install HomeBrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Copy the code
Then install git:
brew install git
Copy the code
Configure the user name and email address:
git config --global user.name "John Doe"
git config --global user.email [email protected]
Copy the code
Git config
can be used to view configuration items.
You can check the git instance tutorial I wrote before for basic git commit, push and other operations.
2 the merge and rebase
First create the master branch, create 3 commit 1,2,3, and then cut the feature branch from the master branch:
Create a new commit 4 from the master branch and a new commit 5 from the feature branch:
Merge master on feature branch:
It can be found that commit 4 and commit 5 are parallel lines, and a new commit 6 (Merge brach master into feature) is generated after the merge. Note that the baseline of the feature branch remains unchanged at this time.
Reset the feature branch back to commit 3, create commit 5 again, rebase master:
You can see that this is a line, and the baseline of the feature branch is derived from the last commit of the master.
If you merge the feature master and then rebase another branch, what will become of the feature branch?
We cut out the release branch from the original master branch and create a new commit 7:
Merge the master feature branch and rebase the release branch
As you can see, the baseline becomes the commit of the release branch, and the two parallel lines from the merge operation are merged. And it can be found that commit 4 from the master branch is placed before commit 5 from the feature branch (question: why?) .
3 Android Studio Git Operation (based on 4.2)
Those who are good at English can refer to the official document of IDEA.
3.1 warehouse
If you want to add Git support to a new repository, click Enable Version Control Integration from the VCS menu.
In most cases it is pulled directly from the remote. Choose Get from Version Control from the VCS menu, and enter the repository address and local directory.
Note that there are two types of addresses for the warehouse. One is HTTPS, which requires you to log in to the private warehouse. The other is SSH, which requires you to remotely store the locally generated public key.
Command to generate a public key locally:
Ssh-keygen -t rsa -c "Your email address when registering GitHub"Copy the code
Copy the public key:
pbcopy < ~/.ssh/id_rsa.pub
Copy the code
Github, GitLab and other remote code hosting platforms have SSH Keys Settings, add a copy of the public key to the SSH can be used.
Android Studio supports multi-remote repository management, which we use for two purposes. One is for projects where there are sub-repositories, and the other is for forking out repositories to synchronize the original repository code.
As shown in the image above, after configuring multiple remote repositories, click on the branch name in the lower right corner of Android Studio and a window pops up showing multiple remote repositories. Select one of them to rebase.
3.2 the fetch
The fetch operation is in the Git collection in the VCS menu by default and can be adjusted outside:
It can also be placed on toolbars, which are hidden by default and can be made visible under the View menu.
Edit the Settings again to add Fetch to the Toolbar, and the Push position can also be adjusted.
You can see that there is an icon on the Toolbar, which is the Fetch operation.
3.3 pull
In the terminal, we often use the pull command to update code, but in Android Studio, we can use the Update project to update code. The advantage is that pull is for a single repository, and the Update project is for all repositories in that project.
Note that the popover should select Rebase… If there is a conflict with the remote end, a conflict prompt pop-up window will be displayed.
3.4 the commit
Since Android Studio 4.2, Commit has been placed in the sidebar as a separate TAB. Let’s take a look at what this TAB does.
The top bar:
- Refresh: Refreshes the file status
- Rollback… : Rollback code, click the rollback popup window to select the code to be rolled back, double click the file in the popup window to select a specific line of code to roll back in the new popup window.
- Show Diff: Displays the modification point
- Changelists:To change the file list, you can change the name of the list or create a list. For a list whose name is not “Default Changelist”, click
Toolbar
The submit information box is automatically filled with the current name when the submit button is displayed. Only the files checked in the left box will be submitted. - Shelve Silently:
Shelve
Equivalent to terminal operationstash
, used to temporarily store code, you can store the entire list, or you can select a single or multiple files and right-click in the menuShelve Changes
For selective storage. - Group By:Grouping,
Directory
和Module
Two ways. - View Options:Some display related options, such as whether to display
ignored
The file. - Expand All: Expand All lists/folders/modules.
- Collapse All: Collapse All lists/folders/modules.
On the left side of the bottom column is the Commit button, which can be expanded to select push after commit.
The first option on the right of the bottom bar is the Amend option, which is checked to merge local changes to the last commit, which is equivalent to rebase and can be pushed to the remote by force push.
The second is the setup button.
- Anthor: If it’s not the code you modified, you can specify the name of the person who modified it, and that name will be displayed for the author of this submission.
- Sign-off commit: To add a signature line to your commit, meaning reference why Sign off? .
- Reformat code: Reformat lines of code that you have changed.
- Rearrange code:Rearrange the code in
Code Style
In Settings, some languages have oneArrangement
的tab
Here,Rearrange code
Based on thattab
Defined rules to execute. - Optimize imports:Optimized import, mainly can remove unused
import
. - Perform Code Analysis: Performs code analysis.
- Check TODO: Check TODO
- Cleanup: performs a Cleanup, sometimes does not Cleanup unused code, not very useful.
- Update copyright:Automatically add new files
copyright
, has been set in need Settingscopyright
。
The third is the submission history, which is directly filled into the submission information box.
3.5 push
Clicking the Push button on the Toolbar brings up the Push dialog.
Very simple interface, multiple warehouses will be displayed together, you can check a single warehouse for push, the lower right corner can select Force Push.
3.6 the Git window
As shown in the figure above, the left sidebar operates against the second left branch. The “+” sign means to cut a new branch from the selected branch. Note the case of multiple warehouses. If multiple branches have the same name, a new branch will be cut from each warehouse.
In the middle is the Branch submission history, filtering operations from search box to Paths, Branch, User and Date can be directly typed after clicking. Note that Paths, which is used for multiple repositories, selects local repositories and defaults to all. The cherry is a cherry-pick that can commit the selected commit to the current branch. The later Turn IntelliSort on is mainly used for branches that have been merged, and can display the history of the branches of the merge together.
Right-click on the submission.
-
Create Patch… : Creates a Patch file for the current commit, giving you the option to save it locally.
-
Checkout Revision ‘… ‘: cuts out a new branch from the currently selected commit location.
-
Reset Current Branch to Here… : Reset operation, there are four modes
- The Soft
index
Set to the selected commit, the selected commit is put into the staging area - The Mixed
index
Set to selected submissions. Submissions that are selected will not be put into the staging area - Hard to enforce the
index
Set to select commit, this is a base change operation, local uncommitted changes are discarded. - Keep to enforce the
index
Set to submit, local if there are unsubmitted changes will prompt whetherSmart Reset
.Smart Reset
Uncommitted changes will be put firststash
Uncommitted changes, forcereset
After the completion of the againunstash
Modify, this process is done automatically in the background.
- The Soft
-
Revert Commit: Creates a new Commit and reversely modifies the current Commit.
-
Undo Commit: Deletes the Commit to the uncommitted state. It can only be used for the latest Commit.
-
Edit Commit Message… : Edit submit information, equivalent to rebase, need to be pushed.
-
Fixup… /Squash Into… The: chicken rib function is supposed to merge local changes into the selected commit, but a new commit is generated.
-
Drop Commit: Discards the Commit. The rebase operation needs to be pushed.
-
Interinvited Rebase from Here: A key feature where users organize their submission history.
You can change the submission order, edit the submission information, merge the submission, and discard the submission. The difference between Squash and Fixup is only in the submission information. Squash preserves the original submission information, while Fixup does not.
3.7 solution conflict
If a conflict exists, a conflict window is displayed. You can choose to accept local or remote completely, or click Merge… Or double-click the file to merge the code.
The conflict resolution window is shown in the figure above.
3.8 Common Scenarios
3.8.1 Rebase error causes base change to wrong branch
- if
rebase
The previous code has been pushed to the remote repository, so go directly to the remote repository for the last commit,reset hard
Just get over there. - If the previous code did not push to the remote, then use
git reflog
Find the last local submission number,reset hard
In the past.