git Application Quick Start

1. git Introduction

1.1 generates history

git is the world's most advanced distributed version control system.

Linus created the open-source Linux in 1991. Since then, the development of Linux systems, has become the largest server system software. Although Linus created Linux, but Linux is to grow by enthusiastic volunteers around the world to participate, so many people write code for Linux around the world, and that Linux code is how to manage it? The fact is that, by 2002, volunteers from around the world to source code files sent by Linus diff way, then by Linus himself by manually merge code!

You may be thinking, why not put Linus Linux code into version control system do? Not a CVS, SVN these free version control system? Because Linus firmly opposed to CVS and SVN, these centralized version control system is not only slow, but also must be networked to use. There are some commercial version control system, although the ratio of CVS, SVN easy to use, but it is paid for, does not match the spirit of Linux and open source. However, by the year 2002, Linux systems have been developed for a decade, a large code base of hard to let Linus continue to manage through manually, community brothers also expressed strong dissatisfaction in this way, so Linus chose a business version control system, BitKeeper, BitKeeper's owner BitMover company out of humanitarian spirit, the authorization to use the free Linux community version control system. Excellent situation of stability and unity in 2005 was broken, because the Linux community gathered cattle, inevitably tainted with some of the Liangshan heroes of rivers and lakes habits. Samba developed by Andrew trying to crack BitKeeper protocols (so dry in fact, not only him), it was found in the company BitMover (monitoring job well done!), So the company BitMover anger, to recover the right to use the free Linux community. Linus can apologize to BitMover companies to ensure strict discipline after the brothers, ah, this is impossible.

The reality is this: Linus spent two weeks own use C to write a distributed version control system, which is Git! Within a month, the source Linux system has been managed by Git up! Cattle is how to define it? You can taste. Git quickly became the most popular distributed version control system, especially in 2008, on the site GitHub line, and it is an open source project to provide free storage Git, many migrated to GitHub open source project started, including jQuery, PHP, Ruby, and so on. History is such a chance, if not the year the company BitMover threat Linux community, and now we may not have free and super easy to use Git up.

Two features of 1.2 git

  • Version control : people can solve the problem at the same time developing the code, you can also solve the problem of code to retrieve history.
  • Distributed : Git is a distributed version control system, with a Git repository, it can be distributed to different machines. First, find a computer acting as a server role, 24 hours a day, everyone else from this "server" Warehouse cloned copy to your own computer, and each of the respective push submitted to the server in the warehouse, but also from the warehouse server pulls submit someone else's. This can set up your own server, you can use the site GitHub.

2. Installation and Configuration

2.1 manner 1: yum install command follows

(This installation of 1.8, the version is too old, you can learn, build environment is recommended to use the second way):

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/a69f1d7dde72520efa39cd829ebc424d.png)

2.2 Method 2: compile and install (production environment is recommended in this way)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/07456582b5f33002e870c1b7d493006a.png)

(3) the installation is successful, run the following command:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/f6db1580572213aa6f925902a1dba10c.png)

3. Create a repository

New Directory git_test, create a repository in git_test directory, the command: git init

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/4722f206b798e103d3025b2e141c0e7e.png)

You can see creates a hidden directory under git_test .git directory, which is the repository directory.

4. Create a version with rollback

Works of 4.1 git

① when the file was created in the repository, there is a first version of a file, this will also generate a pointer to this version

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/6b88e8ed7860db0b26ed20dc7f7deda8.png)

② When you modify the contents code.txt in the future, they generate a second version, but this version is dependent on a version, in this version will only modify the file record that has occurred. After the birth of a new version, it will point the latest version.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/6df42071833e9d06ac2e29311a3137c5.png)

③ may be achieved by modifying the position of the pointer back to an versions, represented in two ways, one is to use ^ A ~ used, as follows

l HEAD ^ or HEAD ~ 1: represents a version of the

l HEAD ^^ or HEAD ~ 2: represents the two versions

Following the adoption of the above ways to fall back to the first version, the pointer points to a version 1

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/12f0ad0473c03b018e670f66adb80859.png)

4.2

(1) Create a file code.txt in git_test directory, writes a line reads as follows:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/52eed22d940b2c0dd28dafdbc44b0a82.png)

(2) using the following two commands to create a version, the version created in two steps:

① git add code.txt

② git commit -m 'Version 1'

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/0b245c928d82cb17440a199fca4a106a.png)

(3) View version record, the command is git log:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/1928a9c46af9b1e0d5fba3d223e11e6d.png)

(4) continue to edit code.txt, add a line in there.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/a8b00e15d20927d2e0f221c9f85ea374.png)

(5) Use the following command to re-create a version and check the version record:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/19ec9f94aa928236749a533ab7273fc1.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/59b8e34134704ae7ffad891986cdce40.png)

(6) Now if you want to return to a version, you can use the following command:

git reset --hard HEAD^

HEAD which represents the current latest version, HEAD ^ represents the current version of the previous version, HEAD ^^ represented before the former version of the current version, you can also use HEAD ~ 1 represents the current version of the previous version, HEAD ~ 100 represents the current version of the the first 100 version.

Now, if it feels like to return to version 1, you can use the following command:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/622cf2c65767930b0336720086b396a4.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/bf209a7e8a84c9561660f3d2c832fc3d.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/293e0ee75ade3c7407946616e7e174e7.png)

After executing the command to view the version using git log records and found that now only see the records version 1, cat code.txt view the contents of the file, now only one line, which is the first version code.txt content.

(7) If we now want to return to version 2, this time how to do?

You can use the following command:

git reset --hard version number

From the above you can see the version number 2 is:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/d9f54d5442caa834c55f89d961d12ef2.png)

(8) were selected based on the query to the version number:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/43688f6a851ed6184b10b9eb4b36ea66.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/ddc8fb3d31b0b1f58f126f93928438a4.png)

Discovery version 2 now has come back. Cat code.txt can view the inside reads as follows:

(9) If we say that the above change how the terminal has been shut fallback version.

We execute the following command will fall back to version 1 release.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/adc54a2e15245ecf6ac2032952b54ee5.png)

Below the terminal off, and then open the terminal, the version number found in version 2 of the see before.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/2d945896a80dc6303c56dd357abfd91d.png)

So how do go back to version 2? git reflog command to view our operating record.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/1856351329cd92321260f41e3d11a073.png)

You can see the version number 2, we then use the following command version rollback, version back to version 2.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/675df08acb21bd8dfe60ed15a5af6fe7.png)

4.3 work area and staging area

4.2.1 workspace (Working Directory)

Computer directory, such as our git_test, is a work area.

4.2.2 repository (Repository)

l work area has a hidden directory .git, this is not the work area, but git repository.

L git version of the library saved a lot of things, most important of which is called the stage (or called index) staging area, and the first branch master git automatically created for us, and point to a master pointer called HEAD.

l because we created git repository, git automatically creates a master branch is the only one we have, so, now, git commit is to commit the changes to the master branch.

l You can simply understood as the documents required to submit all changes into the staging area, and then, a one-time submission of all the changes in the temporary area.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/9d04da9025130069684dc44c5cf6925b.png)

Speaking in front when we put the file to the git repository added, is performed in two steps:

The first step is to use git add to add into the file, the file is actually added to modify the staging area;

The second step is to submit changes with git commit, in fact, is to submit all the contents of the staging area to the current branch.

(1) below and then create a file code2.txt in git_test directory, and then edit the content as follows:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/83ee9f6032ae0bb75f2f08cc9d9c315e.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/ef7e51dcf1ef925e187405bec12ff783.png)

(2) Then again code.txt edit content, to which a line, the edited contents are as follows:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/97dc419986fac449458333fb110a7d22.png)

(3) Use the following command to view the current working state of the tree:

git status

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/cfb1eb0f1fe49ade9363f35ce4bb992b.png)

The above tips we code.txt be modified, but code2.txt not being tracked.

(4) We use the following command to code.txt and code2.txt added to the staging area, and then execute git status command, as follows:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/06847424b8663028ecab6fecdde05e7b.png)

All the git add command is to submit all changes to the temporary storage area.

(5) Then, you can git commit to a one-time submission of all modifications to the staging area to create a version branch.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/80b3bb11d8d8644dcd211cbbfad30a0c.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/85d569a429786cce0694f26a4df4d1f1.png)

(6) Once submitted, and if you do not make any changes to the work area, the work area is "clean". Run the following command can be found:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/1a39201a621e16067b015747128700ea.png)

4.3 modify management

Git modified file management, it will only be submitted to modify the staging area to create a version.

(1) Edit code.txt, and use git add command to add it to the staging area.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/4fa2426cd28b21364efc4b55f4aa36be.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/8424cedf040abd54acf8cca269290b8f.png)

Add to cache

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/57b57dfcc86e2556bd1c3f1bb474a88d.png)

Then add a line

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/f865de8037b69aff355f1603f06fab93.png)

(2) git commit to create a version,

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/f566cad50cca22c4a19fe3b993667c6f.png)

Use git status to see this time and found that the state is not clean

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/87ede3cfb11ed956cee55c026ea0ef82.png)

This is because after the second revision code.txt content, and do not add workspace, so create a version of the time and has not been submitted.

4.4 undo changes

(1) Continuing the above operation, suggesting that we can use to undo the format, that is, discard your work area changes:

git checkout - <file>

Run the following command

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/355890b389e820c43d12d5e6a440856e.png)

Found work area clean,

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/19fbdc3cd4e7f4d36298f6d2fed0582b.png)

The second change content is also gone.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/b8d1df6711d8a61f8704c4da411da7e6.png)

(1) previously demonstrated is not added to the contents of the staging area to be rolled back, in fact, even if added to the staging area, also began to be rolled back.

We continue to edit code.txt, and add the following [img] (file:! /// C: / Users / sy / Documents / My Knowledge / temp / fc8918e9-6218-4de1-ae60-6e01447f1ea3 / 128 / index_files /ba216e8d1d4fdee2a99d4381557af131.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/e66b86628c924d16fa519cec7411b916.png)

And add the staging area

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/762edae36f9b9aa5787e9933e9c02dac.png)

(3) git also told us, use the command git reset [Options] HEAD file can modify the staging area of ​​the revocation away, back into the work area.

There are three options

l --hard: content caching and working directory are rolled back to the state specified version

l --mixed: default option, and you specify a buffer submit sync, but the work will not be affected directory

l --soft: the contents of the cache, and the working directory is not changed (the specified version only content library rollback)

L When using these options, you can not specify a specific file

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/54ae8f8351550ce24176c04c92239604.png)

status

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/71a550c8a984f4a2128a9f64b7012a95.png)

Here, only the content is stored in the staging area is deleted, then you can roll back the file to the state before the modification

(4) To discard modifications code.txt Now, the following command can be executed.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/9831f9bea646ce2be61d164e3df5b478.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/37630b24e0baa6f8d66130e4d625beee.png)

Now, if you make mistakes not only something from scratch also submitted to the repository, you need to version rollback.

summary:

l Scenario 1: When you change the contents of a file chaotic workspace want direct discard modify the workspace, use the command git checkout - file.

l Scenario 2: When you change not only upset the contents of a file workspace, also added to the staging area, to discard the changes in two steps, the first step with the command git reset HEAD file, returned to the scene 1, a second step of operation by scene.

l Scenario 3: have submitted improper modifications to the repository, you want to withdraw this submission, reference version rollback one.

To demonstrate the role of the three options

First to demonstrate the hard option

1 Edit a file in the repository 1, and add to the staging area

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/5d530dcb7bce2f17fc4248533e299132.png)

2 Edit another file (not saved to the staging area)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/2c5bbb630e4f1b577ff93f0a2ca70758.png)

3 View state is not a trace state in a temporary area

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/b71db601d079d062721aef77afa375b2.png)

4 with git reset --hard xxxx rollback to a state

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/a1e85d5186146a53bd624aa9642f51d9.png)

5 again view the status (in this case found to be clean, what state are gone)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/41b06d00f52f39d5b36681a1fb4af1cd.png)

6 These two documents newly added content is gone

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/a370b5a15a1deabee555e293779fd538.png)

First to demonstrate the soft option

The front section slightly

1 execute reset, roll back to the first version

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/fc1c65b52e38f2304d76f209c5218e70.png)

2 The contents of the staging area and the files are still

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/69dc1a095d7b3d61645f6b9f38eee320.png)

View version 3 is currently located, has returned to the first version, the other version is gone

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/9c879cf18be9dfe16f3930329531f09a.png)

4.5 Comparison of different file

Compare different workspaces and files in a version:

(1) continue to edit the file code.txt, add a line in which the content.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/e5f736f400318148a93ceabadc4a10fb.png)

(2) now compare different workspace code.txt and previous versions of code.txt. Use the following command:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/1a5ec4f8e3e0b2d76e150bfc7fae92de.png)

We can and repository of the second version than done

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/e0a7de42a8b74086425df0e2f1ebddd3.png)

Comparison between two versions of different documents:

(1) now ^ HEAD HEAD and compare different versions code.txt using the following command:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/3ed628ef52b772b3e19fef0b79259c63.png)

4.6 Delete Files

(1) we delete the directory code2.txt.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/6ad4029784e757af1f86d3196beaec1b.png)

This time, git know deleted files, so the workspace and repository on inconsistent, git status command will prompt immediately which files are deleted.

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/f4bd59749d6c6686506a782cfbfef7b6.png)

(2) Now you have two options, one sure you want to delete the file repository, then use git rm command to delete, and git commit:

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/50be31433c697f5f0b37eca1a304442c.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/11ebc5ed25d427c4c956f2bc92b2aa83.png)

![img](file:///C:/Users/sy/Documents/My Knowledge/temp/fc8918e9-6218-4de1-ae60-6e01447f1ea3/128/index_files/10747f8023cf258fd8bd1b157e2fc920.png)

Another case is deleting the wrong, you can directly use git checkout - code2.txt, this file code2.txt back.

summary:

  • Git rm command is used to delete a file.
  • If a file has been submitted to the repository, so you never have to worry about mistakenly deleted, but be careful, you can only restore files to the latest version, you will lose the contents of the most recent commit your changes.

Guess you like

Origin blog.51cto.com/54dev/2435416