Correspondence between Git commands and AndroidStudio’s Git menu

First of all, I feel ashamed. I have been using Android Studio for development for a long time, but I am still struggling with Git version control. I decided to sort it out in the past two days and write this article to inspire others.

Why don’t I just talk about Git commands? Because I think commands can sometimes be used in specific situations, but how can it be as comfortable as GUI interface operation? Moreover, some commands cannot be remembered at all when they are not used very often.

Anyway, in actual development, I recommend using the GUI graphical interface to operate Git. If you encounter some interface-based operations that really cannot be used, use commands. However, most operations in daily development can be found in the interface.

So, this article came into being.

I would like to recommend a few well-written blogs about git commands. I will not write what others have written. I will write something different.

Usage and common commands of Git in Android Studio
Summary of common git commands
Complete list of commonly used GIT commands

Students who want to learn Git commands in depth can refer to the above articles.

In addition, while reading the article, it is recommended that you also type the commands or operate it on the GUI interface to know the steps and effects. It is recommended that you go to gitee to create an account and warehouse yourself, and then build a project and submit it to facilitate testing.
Link is as follows: Gitee

Without further ado, let’s get to the point.

1.git add

Android Studio interface operation:

Equivalent to:

git add app/src/main/java/com/example/mygittest/widget/MyRecyclerView.kt

Description: Add unadded files to the temporary storage area (index area)

Note: If you click “add” on a folder, all files in the directory and its subdirectories that have not been added to the staging area will be traversed and added to the staging area.

2.git reset

2.1, git reset –mixed

Android Studio interface operation:
Equivalent to:

git reset --mixed

Description: Remove all files added to the temporary storage area (index area) since the last Commit and become unadded.
If you want to move the files after commit out of the temporary storage area again and change them to an unjoined state, please see:

Move the specified file out of the staging area:

git reset --mixed HEAD app/src/main/java/com/example/mygittest/widget/MyRecyclerView.kt

Note: git reset defaults to –mixed

2.2, git reset HEAD^

Android Studio interface operation:
Suppose there is a local submission now, and I want to roll back this submission, revise it again and submit it again:

Here’s how to do it:


Equivalent to:

git reset HEAD^

Description:
Roll back the most recent submission to the state before submission. This will revoke the submitted records.

Note:
HEAD represents the state after the last commit of the current branch, HEAD^ represents the last commit of the current branch, HEAD^^ represents the last commit of the current branch, and so on. HEAD^n represents the first n submissions of the current branch, such as: HEAD^5 represents the first 5 submissions.

Additional:

git reset --mixed HEAD^^ / git reset --mixed HEAD^ 2 #Roll back two commits forward
git reset --mixed HEAD #Roll back to the last successful local commit.
git reset --mixed commit_id #Roll back to the corresponding commit_id.

2.3 –soft

Only moving the current Head pointer will not change the contents of the work area and temporary storage area.

2.4 –mixed

It is the default parameter of reset. It moves the head pointer and changes the contents of the temporary storage area, but does not change the work area.

2.5 –hard

The current head pointer, work area and temporary storage area contents are all changed.

3. git commit


Equivalent to:

git commit -m "Add MyRecyclerView" --all

Note: You need to add –all here, otherwise only 1 file will be submitted.

4. git stash



Equivalent to:

git stash -m "temporary storage"

Description:
Suppose that when you are writing a certain business in branch A and need to switch to the previous version of branch B to deal with bugs, you do not need git commit at this time. You only need to use git stash to save the modifications of the current branch A, and then Just checkout to another branch B. When you finish dealing with the bug in another branch B, you only need to checkout to branch A, and then use git unstash to restore your saved modification status.

This method can avoid unfinished business submissions, resulting in an incomplete business branch.

You don’t need to write -m here, you can also use git stash directly. However, git will use the default name to label this stash, which is not easy for developers to identify.

5. git unstash



Equivalent to:

git stash pop stash@{<!-- -->0}

Description: Restore all previously saved modification status.

Replenish:

#Store current modifications
git stash
#View all temporary caches
git stash list
#Pop up stash@{0} temporary storage, which will be discarded
git stash pop stash@{<!-- -->0}
#Apply stash@{0} temporary storage, the temporary storage will not be discarded
git stash apply stash@{<!-- -->0}

Resolve conflicts

Yes, after executing git stash, you may execute git pull to update local files. When the updated file conflicts with the file you have temporarily stored, you will be prompted to resolve the conflict after executing git unstash.

Note: “Accept Theirs” here is the file saved using stash, and the conflict caused by git pull “Accept Theirs” is using the record submitted on the remote branch.

6. git cherry-pick

Steps: First checkout to the target branch that you want to merge (the target branch in the above picture is: developer_master), double-click a remote branch (the above picture is: developer_master_1), and right-click on a commit record Click Cherry-Pick

Equivalent to:

git cherry-pick f2de02a4

Here f2de02a4 is the submitted hash value, which can be viewed as shown below.

Description:

Assuming scenario A: developer_master is an online branch, which can also be called the trunk branch. When building code, it is built in the trunk branch, and the developer develops in developer_master_1. When the developer submits the modification to developer_master_1, after After the test and verification are completed, you can select all modifications at the same time and click cherry-pick to synchronize all modifications to the main branch developer_master, saving time and effort.

Assume scenario B: developer_master is the code of the tablet APP, and developer_master_1 is the code of the mobile APP. When a certain modification involves common issues between both ends, such as modifying the code of some file operations (such as: FileUtils.java), you can change this Some modifications are synchronized to the branch on the other end using cherry-pick.

Note: After cherry-pick, the selected commit will only be synchronized to the local repository of another branch, and manual push is required to commit to the remote branch.

7. git pull –rebase


Description: The purpose of git pull –rebase is: after modifying the local code and performing a commit operation, a normal git pull will produce a meaningless commit record “Merge… to…”, while If –rebase is added, this record will not appear. But it also has shortcomings. After rebase, I don’t know which branch my current branch was first pulled from, because the base has changed, so it depends on personal needs.

For a detailed explanation of git rebase, please refer: Detailed explanation of git rebase (illustration + the simplest example, understand it once)

8. git commit –amend


Equivalent to:

git add app/src/main/java/com/example/mygittest/FileUtils.kt
git commit --amend -m "fun createNewFile(path: String)"

Note: After you commit to the local warehouse, you find that there are still some mistakes that have been made. This command can merge and submit subsequent changes to the local warehouse record, so there is no need to commit another record. Of course, you can also choose to use git reset HEAD^.

Additional:
For some companies’ actual development scenarios, for example, the remote warehouse is gerrit, and if it has not been reviewed or failed to pass the review after submission, use –amend to submit the same record to the remote warehouse after modifying the code.

git add .
git commit --amend
git push origin HEAD:refs/for/developer_master

This article is continuously updated. The current updates are some uncommon git operation commands, but if used well during the development process, it can get twice the result with half the effort.

If there is something wrong or wrong in the article, please feel free to point it out, I would be very grateful.

You’ve all seen this, give it a like and go? ?