Android advanced intermediate tutorial-1.1 Local Git Usage Details

Source: Internet
Author: User
Tags how to use git using git version control system

Android advanced intermediate tutorial-1.1 Local Git Usage Details
Android advanced intermediate tutorial-1.1 Local Git Usage Details

Tags (separated by spaces): Android advanced

1. Introduction

In the previous basic Android series, we have explained the simple use of Git. In the advanced series, we
Systematically explain Git, use of basic commands, concept of workspace, temporary storage area, historical warehouse, remote warehouse,
Branch Management in team collaboration, using Git in Android Studio, etc.; Git is a fast distributed version
The difference between the control system and other version control systems is that Git directly recordsSnapshotsInstead of comparison!
The version control system for comparison of differences only cares about the specific differences in the file content, and then records which files are updated each time,
And what rows have been updated! When we want to switch to a previous version, we need merge, and
Git willCompletely store all modified files of the current versionInstead of storing diff,
So you only need a simple reset to switch to a previous version!

2. Several Parts of Git

Before getting started with Git, we need to know the four parts of Git:

The following describes the four parts:

Work ZoneNeedless to say, our current workspace; and the other three parts, let's give an example of an image to help us understand:
Everyone should have purchased the product online. For example, when visiting a cat supermarket and seeing the desired product, we can add the product
Shopping Cart (temporary storage area), We may frequently add products (AddOr remove the product (Checkout),
In this process, we can do whatever we want. We don't have to pay for it anyway. When we pick 7 to 8, we will submit our order,
Click Submit Order (Commit), A product order list (Snapshots), We can also add some points when submitting
Remark information, such as the required color (Commit-m "color"At this time, the order has not been paid (Push), We
You canList of unprocessed orders (Local warehouse) To find our order (Snapshots), You can also see yourself
Some previous order records; well, then we select this order that has not been paid, and then make the payment (Push), Payment
After completion,Merchant (remote warehouse)You will receive this order, and then deliver the goods...

I believe that with this figure and the online shopping visualization example, you should be able to understand the concept of Git's four parts, while Git's operations
Most of them are executed locally. below is the download and installation of Git.

3. Download and install Git

Windows:
Download to Git For Windows or git-for-windows.github.io, and then be the next step of the silly way

Linux:
Download to Download for Linux and Unix. If you are Ubuntu like me, type:
Sudo apt-get install git

Mac system:
Download from Installing on Mac

After installing Git, we can open the Git command line:

WindowsRight-click any location and click Git Bash to open the Git command line.
UbuntuYou can directly open Terminal. The shortcut for Terminal is:Ctrl + alt + t

Here we will explain why we should use the command line:
If you are familiar with commands, you will be able to show off what you need. Of course, it actually reduces the cost of using Git after cross-platform operations,
For example, if you are familiar with a Git GUI tool in windows, but one day you want to migrate to Linux, all commands
Okay. How do you play... Or you need to change to another New GUI tool, and you need to take the time to familiarize yourself with the use of this tool.
You can also use graphical tools. You can also use Git on Android Studio later!
PS: I was planning to complete the command demonstration in Ubuntu. Later I found that I had to handle it myself, because
Ubuntu only supports full screen or window. Therefore, we demonstrated it directly on win ~

4. Set your identity information

After installing GitFirst thingIt is to configure your identity information and team development. If something goes wrong, who will stick to it? Yes!
Enter the following command:

git config --global user.name "coder-pig"git config --global user.email "[email protected]"

After the configuration is complete, remove the "" part of the information and enter the preceding command one more time to check whether the configuration is successful.

You can also enter the following command to view all git settings:

git config --list

4. Get help

Like other command lines, git also has the help command. When you encounter a command that you have never seen or forgotten, you can type:

git help init

Change init to the command you want to query! For exampleGit help add

WinA GitManual (Manual)You can view the usage of commands in the following ways:
WhileUbuntuIn the command line:

You can also go to the Git official manual to find the corresponding command!

5. Create a local code repository

You can directly type the following command to create a project with a Git Repository:

git init GitForTest

Change GitForTest to the project name you want to create! Next we can go to the directory of the newly created Project.
Modify the file to make the hidden file visible. You can see that the. git folder contains the stuff of our git repository. Remember
Do not modify or delete the content at will! (You can also Type ls-ah to view hidden files)

Of course, if you already have a project and want to add a Git repository based on the previous project, you can use the command line or git bash to go To the folder directory of the current project, enter the following command to add a local Git repository for your project:

git init
6. Put the file in the temporary storage area

We can use the newly created or modified filesGit addAdd command to temporary storage Zone
You can use the following commands to add files one by one:

git add README.md

There are a lot of files to be added, and it is very troublesome to add them. You can add multiple files at a time:
Here is the next term:TrackedIndicates that you have joined the Git repository,UntrackedIndicates you have not joined the Git repository!

1) Add the modified or deleted file information in all tracked files to the Git staging area, and the untracked files will not be processed!

git add -u

2) Add the modified or deleted file information in all tracked files to the Git repository, and untracked file information will be processed.
Also join the Git staging zone

git add -A

3) add all the files in the current workspace to the Git staging zone.

git add .

In addition to the above two methods, git also provides an interactive mode. You can type:

git add -i

The process is as follows::

1. I have created two files in the GitForTest folder.
2. Type git add-I, enter, type 4, select the file to add untracked
3. He listed untracked files for us, and then we added the files according to the serial number.
4. input? A prompt will pop up, and press enter to end the selection!
5. Enter git add-I again, and enter 4. The untacked file does not exist!

Of course, there are several other commands, limited by space. If you are interested, you can study them on your own!

7. Submit the content of the temporary storage area to the local repository (History)

We can useGit commit-m "xxx"Command to submit the content of the temporary storage area to the warehouse

Git commit-m "modified xxx"

-M is the description of this submission, and "xxx" is the description. You should not be lazy and save it if you do not
Input-m "xxx" will also let you enter Vim to compile the declaration information ~ Therefore, we recommend that you describe the submitted content here!

In addition, our project may have several hundred years of unchanged or automatically generated files, such as lib, gen, bin folder, etc. We do not need
Every time we make all of these commit, we can create a file named. gitignore in the directory at the same level of. git, then edit the content
If you do not need to write the submitted files, the commit will automatically ignore these files ~ :

8. view the status of the current workspace and temporary storage area

We can useGit statusCommand to view the current situation of the workspace and the temporary storage area, such as the files in the workspace and the files in the temporary storage area
The comparison has changed. Do you want to add it? For example, if something has been added in the temporary storage area but has not yet been submitted, directly type the following command:

git status

For example, I modified the README. md file ~
I just changed not to add:

After adding the file:

Submit the content of the temporary storage area

Well, it's very simple. In addition, you can use the following commands to make the results output in a short form ~

git status -s
9. view the differences between the workspace and the temporary storage area.

Above, we can use git status to get the status of the current workspace and cache zone, only the status,
If you want to view the changed content, enter the following command:

git diff

In this way, you can see the changes made in the current workspace and the temporary storage area!
PS: we added a statement in the README. md file, and then input git diff!

10. View submitted changes

Do you still remember the online shopping example above? We can find our order records in my orders,
Similarly, in git, we can also view all commit records! You can enter the following commands:

git log

Of course, you can also call the following command to get more streamlined results

git log --oneline

If the above does not meet your requirements, you can refer to: Viewing the Commit History
Customizes logs, such:

11. delete the file + restore the file (not added to the temporary storage area)

You can right-click to delete an object, or enter the command line and type rm xxx. xxx to delete the object.
The file in the current workspace still exists in the temporary storage area, so if you type git status, you will find:

Git tells you that the file in the workspace has been deleted, and you have two options:
1) delete the files in the temporary storage area, and enter:

git rm "xxx.xxx"git commit -m "xxx"

2) If the file is deleted by mistake and the file in the temporary storage area is restored to the work area, you can enter:

git checkout -- xxx.xxx

Duang! The deleted file is back ~
Of course, the preceding checkout is not only applicable to accidental file deletion. When you change a file to a non-visual one,
You suddenly regret it, but you have saved the code many times by ctrl + s. You can use the above command to return
The initial appearance of this file! (The premise is that you haven't added it yet!)

12. File recovery (added to the temporary storage area without commit)

If you have added the file to the temporary storage zone using git, it is useless to directly use the checkout file!
We need to use the git reset command to abolish this modification record (version rollback) and bring the current file back to the status of the last commit!
Type:

git reset HEAD xxx.xxx

Then call:

git checkout -- xxx.xxx

The file can be restored to its original state!

13. File recovery (commit) -- version rollback

Assume that our file has been modified to commit, and you regret it for no reason, and want to restore it to the file at the previous commit,
Or the last time, you may have started. But Git provides us with a time machine (version rollback). We can
Run the following command to roll back to the previous version:

git reset HEAD^

Well, after typing git log, we can see that the version has been rolled back to the previous version!
If it is the previous version, you only need to add more ^, and then the previous version continues to add ^, and so on!
Of course, in addition to the above form, you can also roll back based on the version number. For example, here I return to the first version:

git reset --hard 8c3f91f

Hey, no pressure, you suddenly regret it and want to return to the new version... Okay, the same is the preceding command:
However, you can change the version to the latest version of commit!

git reset --hard cf2d155

You may say to me, "Well, I just shut down the command line and the latest version cannot be found,
Git log cannot find the latest version number. Can I return to the future ?" Fortunately, Git time opportunity records
For each command you enter, you only need to enter the following command:

git reflog

Get the version number and git reset it ~

14. Git command auto-completion

Press the Tab twice when you enter the Git command!

15. Git command alias

If you want to be lazy and want to knock a few letters less, you can set the alias for the command, and then type the alias to call the corresponding command, such as setting the status to st:

16. Summary

This section describes how to use Git locally. Most of the time, we perform Git operations locally.
These commands are very important! Of course, this requires a little bit of accumulation. If you think too much, it's just a bit of time! Starting from next section, we
To learn about remote warehouses, branch management, and collaboration in team development ~ Thank you!

References:

Pro Git (official) Pro Git (oschina)

Related Article

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.