Git Tutorials (4)--version fallback

Source: Internet
Author: User
Tags version control system

Now that you've learned to modify the file and commit the changes to the Git repository, now, practice again and modify the Readme.txt file as follows:

 free software distributed under the GPL.

Then try to commit:

1 git add readme.txt 2 " Append GPL "
[Master 9a36c54] Append GPL
1 file changed, 1 insertion (+), 1 deletion (-)

Like this, you continue to modify the file, and then constantly submit changes to the repository, like playing RPG, every pass through the game will automatically save the state, if there is no past, you can also choose to read the status of the previous level. In some cases, you can manually save the boss before hitting it, so that if the boss fails, you may start again from the nearest place. Git, too, can "save a snapshot" whenever you feel that the file has been modified to a certain level, a snapshot that is called in Git commit . Once you've changed the file, or deleted it by mistake, you can recover from the latest one commit , and then continue working instead of losing all of your work for months.

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

Version 1:wrote a Readme file

 free software.

Version 2:add distributed

 free software.

Version 3:append GPL

 free software distributed under the GPL.

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

1

git logThe command shows the commit log from the most recent to the farthest, we can see 3 commits, the most recent being, the last one was append GPL add distributed , the first one was wrote a readme file .

If too many output information, see dazzling, you can try to add --pretty=oneline parameters:

1 git log--pretty=oneline
9a36c54c00350f8fe82d7b7500f515742ce5c97d Append GPL
a4492ee66eb8d65e147c431102330d92db52e45d Add distributed
089fec1548ef9962a00fedd7ec61d2cf183c586a wrote a file

The need for a friendly tip is that you see a bunch of similar 9a36c54c00350f8fe8 commit id (version number), and SVN is not the same, Git is not a three-way ... commit id Incrementing the number, but a SHA1 calculated by a very large number, in hexadecimal notation, and you see commit id and my affirmation is not the same, with your own prevail. Why do commit id we need to use such a large number of numbers to express? Because Git is a distributed version control system, we also want to study how many people work in the same repository, if we all use a three-to-one version ... As a version number, it must have been a conflict.

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

First, git must know which version of the current version is, in Git, the current version, that is, the HEAD latest commit 3628164...882e1e0 (note that my commit ID is not the same as yours), the previous version is HEAD^ , the last version is, HEAD^^ Of course up to 100 versions write 100 ^ more easy to 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

1 git reset--hard head^
HEAD is now located in A4492EE add distribute

--hardWhat does the parameter mean? I'll talk about this later, and now you can use it with ease.

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

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

Sure enough

You can also continue to fall back to the previous version wrote a readme file , but wait, let's take git log a look at the status of the repository now:

1 git log
Commit a4492ee66eb8d65e147c431102330d92db52e45d
Author:zhao Lu <1054304817@qq.com>
date:wed Nov 23 20:55:04 2016 +0800

Add distributed

Commit 089fec1548ef9962a00fedd7ec61d2cf183c586a
Author:zhao Lu <1054304817@qq.com>
date:wed Nov 23 20:51:17 2016 +0800

Wrote a file

The latest version is out of append GPL sight! Like you from 21st century to sit time shuttle machine came to 19th century, want to go back already can't go back, swollen?

The method is still some, as long as the above command Line window has not been turned off, you can go up and look for Ah, find append GPL that commit id is 9a36c54c0035... , so you can specify back to a future version:

1 git reset--hard 9a36c54c0035
HEAD is now located in 9a36c54 append GP

The version number is not necessary to write the whole, the first few can be, git will automatically go to find. Of course, you can't just write the top one or two bits, because git might find multiple version numbers, and there's no way to determine which one.

Look carefully at the contents of Readme.txt:

1 Cat Readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.

Sure enough, I am back again.

Now, you fall back to a version, turn off the computer, the next morning regret, want to revert to the new version of what to do? What if I can't find a new version commit id ?

In Git, there's always a regret pill to eat. $ git reset --hard HEAD^ add distributed The Commit ID that you must find when you want to revert back to the version append GPL append GPL . git provides a command git reflog to keep track of every command you make:

1 git reflog
9a36c54 head@{0}: Reset:moving to 9a36c54c0035
A4492EE Head@{1}: Reset:moving to head^
9a36c54 head@{2}: Commit:append GPL
A4492EE head@{3}: Reset:moving to head^
CAC7E4B head@{4}: Commit:append GPL
A4492EE head@{5}: Commit:add Distributed
089FEC1 head@{6}: Commit (initial): wrote a file

Finally relieved, the 3rd line shows append GPL the commit ID is 3628164 , now, you can take the time machine back to the future.

Summary

Now summarize:

    • HEADThe point is the current version, so git allows us to navigate between versions of history, using commands git reset --hard commit_id .

    • Before you travel, git log you can view the commit history to determine which version to fallback to.

    • To return to the future, use the git reflog view command history to determine which version to return to in the future.

Git Tutorials (4)--version fallback

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.