git tutorials to learn from Liao Xuefeng's official website
1. version rollback
Modify readme.txt file submitted to the Git repository, for example:
Git is a distributed version control system.
Git is free software
Any modified several versions add and commit later, consider the change to the current version has been for generations?
In practice, the mind how we might remember a few thousand lines of each file have changed what, or to do version control system. Version control system must have a command history can tell us, in Git, we git log
view the command
$ git log commit e82e6f6c8b90fbf95f23273e7c7a37973529d463 (HEAD -> master) Author: leox <leox520@163.com> Date: Wed Sep 4 15:21:19 2019 +0800 append HAHAHA commit cf418a19d4725b02680b08b83414cea9bb0c797b Author: leox <leox520@163.com> Date: Wed Sep 4 15:18:46 2019 +0800 append GPL commit 7f6805e527555bc4a2d153a6e85ec86a2661f2f2 Author: leox <leox520@163 .com> a Date: Wed On Sep . 4 14 : 52 is : 31 is 2019 + 0800 adds a txt file
git log
Command to display the most recent commit log from the farthest we can see the 3 submission, most recently append HAHAHA
, the last time add GPL
, the earliest of which was added a txt file.
If the output too much information, see dazzling, you can try to add --pretty=oneline
parameters:
Git log $ - Pretty = oneline e82e6f6c8b90fbf95f23273e7c7a37973529d463 (the HEAD -> Master) the append HAHAHA cf418a19d4725b02680b08b83414cea9bb0c797b the append the GPL 7f6805e527555bc4a2d153a6e85ec86a2661f2f2 added a txt file
Tips required is that you see the long list of similar e82e6f6c8b ...
is commit id
(version number), and not the same as SVN, Git is commit id
not 1,2,3 ...... incremental number, but a SHA1 calculated from a very large digital, hexadecimal, and you see commit id
, and I certainly do not like to your subject. Why commit id
we need to use such strings of numbers that represent it? Because Git is a distributed version control system, we have to study people back to work in the same repository, if everyone uses 1,2,3 ...... as the version number, it is certainly on the conflict.
Well, now we start Time Machine, ready to readme.txt
fall back to the previous version, is add GPL
that version, how to do it?
First of all, Git must know the current version of which version, in Git, use HEAD
represents the current version, which is the latest submitted e82e6f6c8b ...
(note my submission ID and you are certainly not the same), the previous version is HEAD^
, the previous version is HEAD^^
, of course, up 100 write 100 version ^
is easier to count, so written HEAD~100
.
Now, we want the current version append HAHAHA
to fall back to the previous version add GPL
, you can use git reset
the command:
$ git reset --hard HEAD^ HEAD is now at cf418a1 append GPL
Now and then to see the readme.txt file, it really is restored.
You can continue to fall back to the previous version wrote a readme file
, but wait a minute, and then we git log
look at the current state of the repository:
Git log $ the commit cf418a19d4725b02680b08b83414cea9bb0c797b (the HEAD -> Master) the Author: LEOX <leox520 @ 163 .com> a Date: Wed On Sep . 4 15 : 18 is : 46 is 2019 + 0800 the append the GPL the commit 7f6805e527555bc4a2d153a6e85ec86a2661f2f2 the Author: LEOX <leox520 @ 163 .com> a Date: Sep Wed 4 14 : 52 : 31 2019 + 0800 added a txt file
The latest version that append GPL
has been to see! You sit like Time Machine from the 21 century came in the 19th century, already want to go back and go back, swollen do?
Way but it is still there, as long as the above command line window has not been turned off, you can follow up ah searched and searched to find that append HAHAHA
the commit id
is e82e6f6c8b9 ...
, so he can return to specify a future version:
$ git reset --hard e82e6 HEAD is now at e82e6f6 append HAHAHA
And then view the readme.txt file, HAHAHA is back! !
$ cat readme.txt
Git is a distributed version control system. sound HAHAHA.
Git is free software distributed under the GPL.
Git version rollback speed is very fast, because Git inside there is a point to the current version of the HEAD
pointer when you roll back version of the time, Git is just from the HEAD points append GPL
:
┌────┐
│ HEAD │
└────┘
│
└──> ○ append GPL
│
○ add distributed
│
○ wrote a readme file
To point to add distributed
:
┌────┐
│ HEAD │
└────┘
│
│ ○ append GPL
│ │
└──> ○ add distributed
│
○ wrote a readme file
Then the way to update the document workspace. So you get HEAD
points which version number, you put the current version of the positioning is.
Now, you fall back to a version, turn off the computer, you'll regret it the next morning, and would like to return to a new version of how to do? Can not find the new version of commit id
how to do?
In Git, there is always regret it can eat. When you use $ git reset --hard HEAD^
retreated back to the add HAHAHA
time of release, would like to return to append GPL
, you must find append GPL
the commit id. Git provides a command git reflog
to record your every command:
$ git reflog e82e6f6 (HEAD -> master) HEAD@{0}: reset: moving to e82e6 cf418a1 HEAD@{1}: reset: moving to HEAD^ e82e6f6 (HEAD -> master) HEAD@{2}: commit: append HAHAHA cf418a1 HEAD@{3}: commit: append GPL 7f6805e HEAD@{4}: commit (initial): 添加了一个txt文件
Finally sigh, we can see from the output, append GPL
the commit id is cf418a1 , now, and you can ride a time machine back to the future.