Android Advanced Intermediate Tutorial--1.1 git's local usage

Source: Internet
Author: User
Tags diff using git version control system

Android Advanced Intermediate Tutorial--1.1 git's local usage

tags (space delimited): Android advanced

1. Introduction

In the previous Android Basics series we explained the simple use of Git, the Advanced series we
A systematic explanation of git, the use of basic commands, the concept of workspaces, staging area, historical warehouses, remote warehouses,
Branch management in team collaboration, using Git in Android studio, Git is a fast, distributed version
The difference between a control system and other version control systems is that git directly records snapshots , not differences!
The diff version control system only cares about the specific differences in the file content, and then each time it records which files are updated,
And the content of the rows updated! When we want to switch to a previous version, we need the merge, and
Git will completely store all the modified files in the current version at each commit, instead of just storing the diff,
So if you want to switch to a historical version, simply reset!

Several parts of 2.Git

Before we start using git, we need to know four parts of git:

Here are the four sections that are described separately:

the workspace, needless to say, our current workspace, and three other parts, let's take an example of the image point to help understand it:
Everyone should be online shopping, such as visiting a cat supermarket, see the right products, we can add products to
Shopping Cart (staging area), we may frequently add items (add) or remove items (checkout),
This process we can be casually hi, anyway not to pay, when we pick 7788, then we will submit our order,
Click Submit Order (commit), will generate a list of product orders ( snapshot ), we can also add in the time of submission
Note information, such as what color to be (commit-m "Color"), at this time the order has not been paid (push), we
You can find our order ( snapshot ) in the list of non-processed orders in your account (the local warehouse), or you can see your
Some previous order records; OK, then we select the order that has not been paid, then make the payment (push), payment
Upon completion, the Merchant (remote warehouse) will receive your order and then ship ...

With this diagram and a visual example of online shopping, you should be able to understand Git's four-part concept, and Git's operation
Most are executed locally, and the following is a download and installation of Git

3.Git Download and Installation
    • Windows system:
      To git for Windows or Git-for-windows.github.io to download, then the next fool-style

    • Linux Systems :
      To download for Linux and UNIX download, if you are the same as me Ubuntu, type directly in terminal:
      sudo apt-get install git

    • mac System :
      To installing on Mac download

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

Windows Right click anywhere in git bash to open git command line
Ubuntu directly open terminal can, Terminal shortcut is:Ctrl + ALT + T

Here's why you use the command line:
Forcing the high, command familiar can be nothing ruthless show a wave; Of course, more reduce the cost of using Git after cross-platform,
For example, you are familiar with win under a Git GUI tool, but join one day you want to migrate to Linux, all is the command
OK, how do you play ... Or if you need to switch to a new GUI tool, you need to take the time to familiarize yourself with the tool's use.
Look at yourself, not necessarily the command line, you can also use the graphical tools, you will also talk about the use of git! on Android studio
PS: Originally intended to complete the command demonstration under Ubuntu, later found to deal with their own, because
Ubuntu can only be full screen or window, so, directly on the win demo ~

4. Set up your identity information

The first thing to do when you install Git is to configure your identity information, team development, in case the wrong problem, who back pot? Yes, put it!
Type the following directives:

git config --global"coder-pig"git config --global"[email protected]"

After the configuration, the "" part of the information is removed, one more time to lose the above instructions, you can see whether the configuration is successful

You can also type the following instructions to view all git settings:

gitconfig--list

4. Get Help

As with other command lines, Git also has a help command, and when you encounter a command that you haven't seen or forgotten, you can type:

help init

Change init to the command you want to query! Like git help add

win will open a git Manual (manual) page, you can see some of the usage of the instructions
and Ubuntu will output directly on the command line:

You can also go to the GIT official manual to find the corresponding instructions!

5. Create a local code warehouse

You can create a new project with a git repository directly by typing the following command:

git init GitForTest

Change the gitfortest to the project name you want to create! Then we can come to the newly created project directory, where
If you make the hidden file visible under Modify, you can see the. git folder, which is the stuff of our Git repository, remember
Don't feel free to change or delete anything inside! (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 on top of the previous project, you can add a local git repository to your project by typing the following instructions by using the command line or Git bash in the folder directory of the current project:

git init
6. Put the file into staging area

We can put new or modified files, we can add to staging area via git add command
You can use the following instructions to add a file:

add README.md

To add a lot of files, think add up very cumbersome, you can add multiple at once:
Here's the next noun:tracked representatives have joined the GIT repository,untracked representatives have not yet joined the GIT repository!

1) Add the modified or deleted file information from all tracked files to git staging area, and will not process untracked files!

add -u

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

add -A

3) Add all the files from your current workspace to git staging area

add .

In addition to the above two, git actually provides the interactive interface mode, we can type:

add -i

The process is this :

1. I have created two files in the Gitfortest folder now.
2. Type git add-i, enter it, type 4, select the file to add untracked
3. He gave us a list of untracked files, and then we added the files according to the serial number.
4. The input will pop up the relevant prompt, then directly enter, end the selection!
5. Then enter Git add-i again, enter 4, you can see no untacked files already exist!

Of course, there are several other instructions, confined to space, interested in the study can be their own!

7. Submit the contents of the staging area to the local repository (history)

We can submit the contents of the staging area to the warehouse via the git commit-m "xxx" command

commit -m "修改了xxx"

After-M is the description of this submission, "XXX" is a description of the content, should not be lazy omitted, if you do not
Input,-M "xxx" will also let you enter vim to write the declaration information ~ so the proposal or here to submit the content of the instructions!

In addition, our project may have some hundreds of-year unchanging or automatically generated files, such as the Lib,gen,bin folder, etc., we do not need
Each time we commit these, we can create a file named. gitignore in the. git sibling directory, and then edit the content to
Do not need to submit the file to write, then commit the time will automatically ignore these files ~:

8. View the status of the current workspace and staging area

We can use the git status command to see the current situation with the workspace and staging area, such as what files and staging area files are in the workspace.
Contrast, has changed, want to add under? Another example of staging area in the add, but not yet submitted, etc., directly type the following instructions:

git status

For example, I have modified the next readme.md file ~
Just changed. No add:

After the Add File:

Commit commits the staging area content

Well, it's simple, and you can also use the following instructions to output the results in short form ~

-s
9. View the differences between workspaces and staging area

Above we can get the status of the current workspace and the buffer state through Git status, just the state,
If we need to see what has changed, then you need to type in the following instructions:

git diff

This allows you to see how the current workspace compares to the staging area and what changes have been made!
PS: Above is we added a statement in the Readme.md file, and then entered the Git diff!

10. View the record of the commit change

Remember the above example of online shopping, we can find in my order records of their own orders,
Also, in git, we can see all of the Commit records! You can type the following directives:

log

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

log--oneline

If the above is not enough to meet you, you can refer to: Viewing the Commit history
To customize the log, such as:

11. Delete file + File recovery (not add to staging area)

We can just right-click to delete a file, or go to the command line, type RM xxx.xxx to delete the file, but only delete the
The file in the current workspace is still present in the staging area, so when you type git status, you will find:

Git tells you that the workspace file is deleted, and then you have two options:
1) Delete the files in the staging area, then type:

"xxx.xxx"-m"xxx"

2) deleted by mistake, restore the file of the staging area to the workspace, then you can type:

gitcheckout--xxx.xxx

duang! The deleted files are back again ~
Of course, the above checkout not only applies to the accidental deletion of files, when you change a file beyond recognition,
You suddenly regret it, but you have ctrl+s to save a lot of code, you can use the above command to return to
The original look of this file! ( If you're not add!, )

12. File recovery (add to staging area, not commit)

If you have the file with git add to the staging area, then you directly with the checkout file is not useful!
We need to use the git reset command to revoke this modification record (version fallback) so that the current file goes back to the state of the last commit!
Type:

reset HEAD xxx.xxx

Call again at this point:

gitcheckout--xxx.xxx

The file can be restored as it is!

13. File Recovery (commit)--version fallback

If our file changes have been made, and you regret it, you want to revert to the last commit file,
Or last time, you may have started, but Git has provided us with a time machine (version fallback) that we can
Fallback to previous version with the following instruction:

reset HEAD^

Well, after we type git log, we can see that the version has been rolled back to the previous version!
If this is the last version, you only need to add more ^, and then the previous version to add ^, so on!
Of course, in addition to the above form, you can also go back and forth according to the version number, such as here I retreated to the first version:

gitreset--hard8c3f91f

Hey, no pressure, you suddenly regret again, want to go back to the new version, uh ... Well, here's the same instruction:
But the version number is changed to the latest commit version number!

gitreset--hardcf2d155

You might come over and say to me, "Well, I just closed the command line, and the latest version number is missing.
Git log can not find the latest version number, then I can not go back to the future? "Fortunately, the git time-of-Opportunity record
Each instruction you enter, you simply type the following command:

git reflog

Get the version number, Git reset can ~

14.Git Command Auto-complete

Enter the git command by pressing TAB two times!

15.Git command Aliases

If you want to be lazy and want to knock down a few letters, you can set an alias for the command, and then type the alias to invoke the corresponding instruction, such as setting the status to St:

16. Summary

This section gives you a detailed description of Git's local use, most of the time we are on the local git some of the operation, so cooked
It's important to be aware of these commands! Of course, this needs a little accumulation, knocking more, knocking up the fluid! At the beginning of the next section, we
To learn the remote warehouse, branch management, team development collaboration, etc. ~ Thank you!

Reference Documents :

  • Pro Git (official)
  • Pro Git (Oschina)

Android Advanced Intermediate Tutorial--1.1 git's local usage

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.