Android Basics Getting Started tutorial--1.5.1 git using the tutorial's basic operation of the local repository

Source: Internet
Author: User
Tags version control system

Android Basics Getting Started tutorial--1.5.1 git using the tutorial's basic operation of the local repository

Tags: Android basics Getting Started Tutorial

What is Git?

A distributed version control system, similar to SVN, but one version control system that is far more powerful than SVN
①git can be easily versioned locally, just as you have a version Management Server locally
We can choose to push the local version to a unified version Management Server at the right time
②git each time it extracts the full image of the entire code warehouse, which is equivalent to a backup of the entire code repository, so that in addition to the problem, we can directly use the Local warehouse recovery! With the local version management feature, the remote version Management Server is out of the problem, we can continue to write our own code, and when he recovers we will submit our local version!
Git was developed initially to better manage the Linux kernel, but it is now widely used in a variety of projects!

Install Git

If your system is Linux, open the shell input directly:

sudo apt-get install git

Of course, most of the system is estimated to be windows, which requires us to download a git for window on the Internet, can be downloaded to the following Web site:
http://msysgit.github.io/
Click the version number, not download, do not know why not open!

Click to enter the page, download the following files can be

Or to the author's cloud disk direct download can also:
Git-1.9.5-preview20150319.exe
Then the next fool will be able to do it ~
Next you can find the Git GUI and start playing git, but if you switch to another platform later, there is no graphical interface you will be unable to move!
So, if you are interested, we will play the command line, after changing the system can also play the normal git!

Play git command line

Of course git must be paired with GitHub to play just flavoursome, but first to learn some local instructions first!
When you're done installing git, we can right-click on git bash to open our git command line in any location!
You can click Git Init here to create a code repository directly under the current directory, or click the Git GUI to open the GUI's graphics Operations page!

1. Create a code Warehouse

Step 1: First configure our identity, so that when the code is submitted git can know who submitted, the command is as follows:

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

After the configuration is complete, we can enter it again, not including the name, we can see that we have successfully configured

Step 2: Find a place to create our code warehouse, then I created a new project: Testforgit, come to the project directory, right-click, open our Git Bash, type the following instructions to complete the establishment of the Code warehouse! In addition, this code warehouse is actually used to save some of the information required for versioning, and our locally submitted code is submitted to the code repository, so we can choose to revert to a version, and, of course, we can push the code stored in the code warehouse to the remote repository if needed! Like github!.

git init

A simple code, the code warehouse is created! Continue typing: Ls-al can see that there is a. git folder under the directory that's him!

You can also open the project catalog and see the. git folder as well, if we want to delete the code repository just remove the folder!

2. Submit Local Code

After creating the code warehouse, the next step is to submit the code, we use the Add command to add the content to be submitted, and then commit the commit is really to execute the operation! The command example is as follows, you can slowly add again and again, of course, you can commit all, direct git Add.
We are now in the engineering directory to create a Readme.txt file, try to write something, then enter the following command:

 git add readme.txt git commit -m "Wrote a readme file"

To enter a command try:

Of course, if you can add multiple files and then commit once, but if we change a lot of files, we can git Add. Add all at once, but some of them are unchanged for hundreds of years or automatically generated, such as the Lib,gen,bin folder, etc. We can create a file named. gitignore in the root directory of the code repository, then edit the contents and ignore the files that need not be submitted!

Then enter the file content to be ignored when submitting!

Then we git add, the file here will not add, in addition, you may feel the commit behind the write-M "xxx" is very troublesome, want to lazy, but still write it! The input is a few statements of this submission, such as what you have modified! It's like writing code, you lazy don't write notes, in a few days you don't even know what you wrote ...

3. See what's changed

Well, we used git add to submit the entire project to the local repository, then we pity dorado things and then use git status to see
The modified section, for example, we delete the Mainactivity.java menu and the extra menu-related packages!

He will prompt us which files have changed, but have not yet submitted, if we want to see what the specific changes, we can use the git diff command, in addition, press Q can be returned to the command line input!

4. View submission History

Of course, with the depth of our project, the number of commits will be more and more, you may have forgotten every time the submission has changed what content,
It's OK, Git helps you remember, use git log to view historical submissions! Type

git log

Enter:

Let's take a few of them to analyze:

commit defd8af52be5183dfceb3e5cf23f78ea47d013b0Author: coder-pig <[email protected]>Date:   Fri Jun 19 17:00:36 2015 +0800MainActivity Delete Menu

In turn:

  • The corresponding version number of this submission
  • Author: name Email
  • Time of submission
  • Commit version Modified content: is we commit-m "xxx" in XXX
5. Revocation of uncommitted changes

For example, we have just submitted a version, and then a mess to write a bunch of things, suddenly found accidentally deleted some of the things, and then CTRL + S saved, this time is not to cry no tears, but there is git, just a checkout command can undo the change, of course, you have not add the situation, For example, we add a statement in mainactivity, and CTRL + S saves the code!

Then, at the command line, type: Git diff:

Well, here we can see what we have changed, we can go back and delete this code, but if you change thousands of lines, how do you change,
So this time we can use

git checkout src/com/jay/example/testforgit/MainActivity.java

And then it magically finds out that the new code we wrote is gone! Duang, don't believe you can try it yourself

Of course, if we have add, then checkout is not any effect, we have to cancel the addition before we can recall the submission, using the following directives:

git reset HEAD src/com/jay/example/testforgit/MainActivity.javagit checkout src/com/jay/example/testforgit/MainActivity.java

6. Version fallback

5th we teach you to undo uncommitted changes, but join the submission, and we want to fall back to a previous version of what to do?
In the 4th we can view our commit record through git log, we need to get a version number from here,
In general we only need the first seven characters is enough, and in Git, the HEAD is the current version, the previous version is head^,
The last version is head^^ and so on! Let's start with git log and look at the version history first!

Let's go back to the previous commit version, and then type the following command:

 git reset --hard HEAD git reset --hard HEAD^ git log

Now look at our console:

You can see that we have returned to the previous version, of course you can write it directly:

git reset --hard ad2080c

It's so simple! Back off, you suddenly regret, want to return to the new version,
Unfortunately, you typed git log and found that you didn't have the latest version number.
OK, Git gives you this "regret medicine", git Records every command you enter! Type:

git reflog

You will find that the version number is here:

Then type:

git reset --hard ad2080c

You can see that we are back to the latest version, that's it!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--1.5.1 git using the tutorial's basic operation of the local repository

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.