Git tutorial Learning (iv)-version control _git Learning

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

We have successfully added and submitted a Readme.txt file, now it is time to continue working, so we continue to modify the Readme.txt file and change it to the following:

Git is a distributed version control system.
Git is free software.

Now run the git status command to see the results:

$ git Status
# on branch Master
# Changes don't staged for commit:
#   [use ' git add <file>. ' To Updat e what'll be committed)
#   (use "Git checkout-<file> ..." to discard changes in working directory)
#< c9/>#    Modified:   readme.txt
#
No changes added to commit (use "git add" and/or "Git Commit-a")

The git status command allows us to keep abreast of the current state of the warehouse, and the above command tells us that Readme.txt has been modified but has not yet been prepared to submit the changes.

While Git tells us that Readme.txt has been modified, it would be nice if we could look at what was specifically modified. For example, you are on vacation two weeks from abroad back, the first day of work, has not remembered how last modified Readme.txt, so, need to use git diff this command to see:

$ git diff readme.txt 
diff--git A/readme.txt b/readme.txt
index 46D49BF. 9247DB6 100644
---a/readme.txt
+++ b/readme.txt @@ -1,2
+1,2 @@ -1,2 is
a version of control system.
+git is a distributed version control system.
 Git is free software.

Git diff, as the name suggests, is to view difference, the format is the common UNIX diff format, you can see from the above command output, we added a "distributed" word in the first line.

Knowing what has been done to Readme.txt, and then submit it to the warehouse is much more assured that the submission of changes and submit new files is the same two steps, the first step is git add:

$ git Add readme.txt

There is also no output. Before performing the second git commit, we run GIT status to see the status of the current warehouse:

$ git Status
# on branch Master
# Changes to is committed:
#   (use "git Reset head <file> ..." to unstage)
#
#       Modified:   Readme.txt
#

Git status tells us that the changes to be submitted include Readme.txt, and next, you can safely submit:

$ git commit-m "add distributed"
[Master ea34578] Add distributed
 1 file changed, 1 insertion (+), 1 deletion (-)

Once submitted, we then use the git status command to look at the current state of the warehouse:

$ git Status
# on branch Master No to
commit (working directory clean)

Git tells us that there are currently no changes that need to be submitted, and that the working directory is clean (working directory cleanly).

Summary

To master the status of the workspace at any time, use the git status command.

If Git status tells you that a file has been modified, use Git diff to view the changes. 1: Version fallback

Now that you have learned to modify the file, and then submit the changes to the GIT version library, now practice again and modify the Readme.txt file as follows:

Git is a distributed version control system.
The Git is free software distributed under the GPL.

And then try to submit:

$ git add readme.txt
$ git commit-m "append GPL"
[Master 3628164] Append GPL
 1 file changed, 1 insertion (+), 1 deletion (-)

Like this, you continue to modify the file, and then continue to submit changes to the version of the library, like playing RPG games, each pass will automatically save the game status, if a certain not past, you can also choose to read the status of the previous pass. In some cases, you will manually save the boss, so that if you fail to hit the boss, you can start afresh from the nearest place. The same is true of Git, where you can "save a snapshot" whenever you feel that the file has been modified to a certain extent, and this snapshot is called a commit in Git. Once you've changed the file, or deleted the file, you can recover from a recent commit, and then go on working instead of losing all of your work for months.

Now, let's recap how several versions of the Readme.txt file have been submitted to the GIT repository:

Version 1:wrote a Readme file

Git is a version of control system.
Git is free software.

Version 2:add distributed

Git is a distributed version control system.
Git is free software.

Version 3:append GPL

Git is a distributed version control system.
The Git is free software distributed under the GPL.

Of course, in the actual work, how can we remember a thousands of-line file every time change what content, or to version control system what to do. The version control system must have a command that tells us the history, and in Git we use the git log command to view:

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 15:11:49 2013 +0800

    append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 14:53:12 2013 +0800

    Add distributed

Commit cb926e7ea50ad11b8f9e909c05226233bf755030
author:michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 17:51:55 2013 +0800

    wrote a readme file

Git log command display from the most recent to the most remote submission log, we can see 3 submissions, the most recent one is the append GPL, the last one is add distributed, the earliest one is wrote a readme file. If too much output information, see dazzling, you can try to add--pretty=oneline parameters:

$ git log--pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
Ea34578d5496d7dd233c827ed32a8cd576c5ee85 Add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a Readme file

Need a friendship hint is that you see a large string of similar 3628164 ... 882E1E0 is the commit ID (version number), unlike SVN, GIT's commit ID is not 1,2,3 ... Increment of the number, but a SHA1 calculated a very large number, in hexadecimal notation, and you see the commit ID and my affirmation is not the same, whichever is your own. Why does a commit ID need to be represented by such a large number of digits. Since Git is a distributed version control system, we have to study many people working in the same version library, if everyone uses 1,2,3 ... As a version number, that's definitely a conflict.

Each time a new version is submitted, Git will actually automatically string them into a timeline. If you use visual tools to view git history, you can see the timeline for submitting history more clearly:


OK, now we start the time shuttle, ready to Readme.txt back to the previous version, which is the "Add distributed" version, how to do it.

First of all, Git must know which version of the current version, in Git, with head to represent the current version, that is, the latest submission 3628164 ... 882E1E0 (Note that my submission ID and your affirmation is not the same), the previous version is head^, the first version is head^^, of course, to 100 versions of the 100 ^ more easily count, so write head~100.

Now we're going to roll back the current version of "Append GPL" to the previous version of "Add distributed" and you can use the git reset command:

$ git reset--hard head^ head are now at
ea34578 Add distributed

What is the meaning of the--hard parameter? This is the back of the talk, now you rest assured that use.

See if the Readme.txt content is not version add distributed:

$ cat Readme.txt
Git is a distributed version control system.
Git is free software.

Really

You can also continue to fall back to the previous version of wrote a Readme file, but wait, let's use git log to see the status of the current version library:

$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 14:53:12 2013 +0800

    Add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 17:51:55 2013 +0800

    wrote a readme file

The latest version of the Append GPL is out of sight. Like you from 21st century time shuttle came to 19th century, want to go back to have not gone back, swollen mody do.

The solution is actually still some, as long as the above command Line window has not been turned off, you can go up and look for Ah, find that append the GPL's commit ID is 3628164 ..., so you can specify a version back to the future:

$ git reset--hard 3628164 head are now at
3628164 append GPL

The version number is not necessary to write the full, the first few can, git will automatically find. Of course, you can't just write the first one or two digits, because git might find more than one version number and you can't be sure which one it is.

And look carefully at the contents of Readme.txt:

$ cat Readme.txt
Git is a distributed version control system.
The Git is free software distributed under the GPL.

Sure enough, I Hu Hansan back again.

Git's version rollback is very fast because git has an internal head pointer pointing to the current version, and when you roll back the version, git simply points the head from the Append GPL:

To point to add distributed:

And then, by the way, update the workspace files. So you have the head point to which version number you want to locate the current version. Version fallback read: 823697

Now that you have learned to modify the file, and then submit the changes to the GIT version library, now practice again and modify the Readme.txt file as follows:

Git is a distributed version control system.
The Git is free software distributed under the GPL.

And then try to submit:

$ git add readme.txt
$ git commit-m "append GPL"
[Master 3628164] Append GPL
 1 file changed, 1 insertion (+), 1 deletion (-)

Like this, you continue to modify the file, and then continue to submit changes to the version of the library, like playing RPG games, each pass will automatically save the game status, if a certain not past, you can also choose to read the status of the previous pass. In some cases, you will manually save the boss, so that if you fail to hit the boss, you can start afresh from the nearest place. The same is true of Git, where you can "save a snapshot" whenever you feel that the file has been modified to a certain extent, and this snapshot is called a commit in Git. Once you've changed the file, or deleted the file, you can recover from a recent commit, and then go on working instead of losing all of your work for months.

Now, let's recap how several versions of the Readme.txt file have been submitted to the GIT repository:

Version 1:wrote a Readme file

Git is a version of control system.
Git is free software.

Version 2:add distributed

Git is a distributed version control system.
Git is free software.

Version 3:append GPL

Git is a distributed version control system.
The Git is free software distributed under the GPL.

Of course, in the actual work, how can we remember a thousands of-line file every time change what content, or to version control system what to do. The version control system must have a command that tells us the history, and in Git we use the git log command to view:

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 15:11:49 2013 +0800

    append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 14:53:12 2013 +0800

    Add distributed

Commit cb926e7ea50ad11b8f9e909c05226233bf755030
author:michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 17:51:55 2013 +0800

    wrote a readme file

Git log command display from the most recent to the most remote submission log, we can see 3 submissions, the most recent one is the append GPL, the last one is add distributed, the earliest one is wrote a readme file. If too much output information, see dazzling, you can try to add--pretty=oneline parameters:

$ git log--pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
Ea34578d5496d7dd233c827ed32a8cd576c5ee85 Add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a Readme file

Need a friendship hint is that you see a large string of similar 3628164 ... 882E1E0 is the commit ID (version number), unlike SVN, GIT's commit ID is not 1,2,3 ... Increment of the number, but a SHA1 calculated a very large number, in hexadecimal notation, and you see the commit ID and my affirmation is not the same, whichever is your own. Why does a commit ID need to be represented by such a large number of digits. Since Git is a distributed version control system, we have to study many people working in the same version library, if everyone uses 1,2,3 ... As a version number, that's definitely a conflict.

Each time a new version is submitted, Git will actually automatically string them into a timeline. If you use visual tools to view git history, you can see the timeline for submitting history more clearly:

OK, now we start the time shuttle, ready to Readme.txt back to the previous version, which is the "Add distributed" version, how to do it.

First of all, Git must know which version of the current version, in Git, with head to represent the current version, that is, the latest submission 3628164 ... 882E1E0 (Note that my submission ID and your affirmation is not the same), the previous version is head^, the first version is head^^, of course, to 100 versions of the 100 ^ more easily count, so write head~100.

Now we're going to roll back the current version of "Append GPL" to the previous version of "Add distributed" and you can use the git reset command:

$ git reset--hard head^ head are now at
ea34578 Add distributed

What is the meaning of the--hard parameter? This is the back of the talk, now you rest assured that use.

See if the Readme.txt content is not version add distributed:

$ cat Readme.txt
Git is a distributed version control system.
Git is free software.

Really

You can also continue to fall back to the previous version of wrote a Readme file, but wait, let's use git log to see the status of the current version library:

$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
author:michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 14:53:12 2013 +0800

    Add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 17:51:55 2013 +0800

    wrote a readme file

The latest version of the Append GPL is out of sight. Like you from 21st century time shuttle came to 19th century, want to go back to have not gone back, swollen mody do.

The solution is actually still some, as long as the above command Line window has not been turned off, you can go up and look for Ah, find that append the GPL's commit ID is 3628164 ..., so you can specify a version back to the future:

$ git reset--hard 3628164 head are now at
3628164 append GPL

The version number is not necessary to write the full, the first few can, git will automatically find. Of course, you can't just write the first one or two digits, because git might find more than one version number and you can't be sure which one it is.

And look carefully at the contents of Readme.txt:

$ cat Readme.txt
Git is a distributed version control system.
The Git is free software distributed under the GPL.

Sure enough, I Hu Hansan back again.

Git's version rollback is very fast because git has an internal head pointer pointing to the current version, and when you roll back the version, git simply points the head from the Append GPL:

To point to add distributed:

And then, by the way, update the workspace files. So you have the head point to which version number you want to locate the current version.

Now, you fall back to a certain version, turn off the computer, regret the next morning, want to revert to the new version of how to do. What to do if a new version of the commit ID is not found.

In Git, there is always regret medicine to eat. When you return to the add distributed version with $ git reset--hard head^, you will need to find the Append GPL's commit ID if you want to revert to the append GPL. git provides a command git reflog to record every command you make:

$ git reflog
ea34578 head@{0}: reset:moving to head^ 3628164 head@{1
}: Commit:append GPL
ea34578 head@{2}: C Ommit:add distributed
Cb926e7 head@{3}: Commit (initial): wrote a readme file

Finally, a sigh of relief, the second line shows the Append GPL's commit ID is 3628164, and now you can go back to the future by riding the time machine.

Summary

Now summarize:

The version that head points to is the current version, so git allows us to travel between versions of the history, using the command git reset--hard commit_id.

Before the shuttle, use git log to view the submission history to determine which version to roll back to.

To return to the future, look at the command history with git Reflog to determine which version of the future you want to go back to. 2: Work area and registers

One of the differences between Git and other version control systems like SVN is the concept of registers.

First look at the noun explanation. Workspace (Working Directory)

Is the directory you can see on the computer, such as my Learngit folder is a workspace:

Version Library (Repository)

There is a hidden directory in the workspace. Git, this is not a workspace, it's a repository of git.

A lot of things are stored in Git's version library, the most important of which is called the stage (or index) registers, and the first branch master git automatically created for us, and one pointer to master is called head.

The concept of branching and head we'll talk about it later.

When we added the file to the Git repository, it was done in two steps:

The first step is to add the file to Git, in fact, to add the file modification to the registers;

The second step is to commit the change with git commit, which in effect commits all the contents of the registers to the current branch.

Because git created the only master branch for us when we created the Git version library, git commit now commits the changes to the Master branch.

You can simply understand that the file changes that need to be submitted are all put into registers, and then submit all changes to registers at once.

As the saying goes, practice is genuine. Now, let's go over it and make a change to Readme.txt, like adding a line:

Git is a distributed version control system.
The Git is free software distributed under the GPL.
Git has a mutable index called stage.

Then, add a license text file in the workspace (content is written casually).

Let's look at the status by using git status:

$ git Status
# on branch Master
# Changes don't staged for commit:
#   [use ' git add <file>. ' To Updat e what'll be committed)
#   (use "Git checkout-<file> ..." to discard changes in working directory)
#< c10/>#       Modified:   readme.txt
#
untracked files:
#   (use "git add <file> ..." to Include in what'll be committed
#       LICENSE
No changes added to commit (use "git" add "and/or" Git C Ommit-a ")

Git tells us very clearly that Readme.txt has been modified, and license has never been added, so its state is untracked.

Now, use git add two times, add Readme.txt and license, and then look again with git status:

$ git Status
# on branch Master
# Changes to is committed:
#   (use "git Reset head <file> ..." to Unst Age)
#       new file:   LICENSE
#       modified:   Readme.txt
#

Now, the state of registers becomes this:

So, the git add command actually puts all the changes to be submitted to registers (Stage), and then the Git commit can commit all the changes registers to the branch at once.

$ git commit-m "understand how Stage"
[master 27c9860] Understand how stage works
 2 files changed, 675 in Sertions (+)
 Create mode 100644 LICENSE

Once submitted, if you do not make any changes to the workspace, the workspace is "clean":

$ git Status
# on branch Master No to
commit (working directory clean)

Now that the version library has become so, registers has nothing:

Summary

Registers is a very important concept for git, understanding the registers and figuring out what a lot of git is doing.

Didn't figure out what's going on with the registers, please scroll up the page and look at it again. 3: Administrative Modifications

Now, suppose you have a complete grasp of the concept of registers. Next, we're going to talk about why Git is better designed than other version control systems because git tracks and manages changes, not files.

You will ask, what is the modification. For example, you add a line, this is a modification, delete a row, is also a modification, changed some characters, but also a modification, delete some added some, is also a modification, or even create a new file, also calculate a modification.

Why is it that git manages changes, not files? We'll do the experiment. The first step is to make a change to the Readme.txt, such as adding one line of content:

$ cat Readme.txt
Git is a distributed version control system.
The Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.

Then, add:

$ git add readme.txt
$ git Status
# on branch Master # Changes to is
committed:
#   (use) git reset head <file> ... "to Unstage)
# #       modified:   Readme.txt
#

Then, modify the Readme.txt:

$ cat Readme.txt 
Git is a distributed version control system.
The Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

Submit:

$ git commit-m "git tracks changes"
[master D4f25b6] git tracks changes
 1 file changed, 1 insertion (+)

After submitting, look again at the status:

$ git Status
# on branch Master
# Changes don't staged for commit:
#   [use ' git add <file>. ' To Updat e what'll be committed)
#   (use "Git checkout-<file> ..." to discard changes in working directory)
#< c7/>#       Modified:   readme.txt
#
No changes added to commit (use "git add" and/or "Git Commit-a")

Gee, how the second modification was not submitted.

Don't get excited, let's review the process:

First modification-> git add-> second modification-> git commit

You see, we talked about it earlier, Git manages the modification, and when you use the git add command, the first modification in the workspace is put into registers, ready to commit, but the second modification in the workspace is not placed in the registers, so git commit is only responsible for submitting the registers modifications, The first modification was submitted, and the second modification was not submitted.

After submitting, use the Git diff head-readme.txt command to see the difference between the latest version in the workspace and the version library:

$ git diff head--Readme.txt 
diff--git A/readme.txt b/readme.txt
index 76d770f. a9c5755 100644
---a/readme.txt
+++ b/readme.txt @@ -1,4
+1,4 @@ -1,4 is
 a distributed version control Syst EM.
 The Git is free software distributed under the GPL.
 Git has a mutable index called stage.
-git tracks changes.
+git tracks changes of files.

Visible, the second modification was not actually submitted.

How do you submit the Second Amendment? You can continue to git add git commit, you can also do not worry about submitting the first modification, git add the second modification, and then git commit, it is equivalent to two changes after merging a piece submitted:

First modification-> git add-> Second modification-> git add-> git commit

OK, now, submit the second modification and start the summary. Summary

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.