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:
is a distributed version control system.Git is free software distributed under the GPL.
Then try to commit:
$ 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 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
is a version control system.Git is free software.
Version 2:add distributed
is a distributed version control system.Git is free software.
Version 3:append GPL
is a distributed version control system.Git is 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:
<[email protected]>Date: Tue Aug 20 15:11:49 2013 +0800 append GPLcommit ea34578d5496d7dd233c827ed32a8cd576c5ee85Author: Michael Liao <[email protected]>Date: Tue Aug 20 14:53:12 2013 +0800 add distributedcommit cb926e7ea50ad11b8f9e909c05226233bf755030Author: Michael Liao <[email protected]>Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme file
git log
The 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:
$ git log --pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0 append GPLea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributedcb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
The need for a friendly tip is that you see a bunch of similar 3628164...882e1e0
commit id
(version number), and SVN is not the same, Git is commit id
not a three-way ... 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.
Each time a new version is submitted, git actually automatically strings them into a timeline. If you use the visualizer to view git history, you can see the timeline of the commit history more clearly:
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:
$ git reset --hard HEAD^HEAD is now at ea34578 add distributed
--hard
What 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
:
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:
<[email protected]>Date: Tue Aug 20 14:53:12 2013 +0800 add distributedcommit cb926e7ea50ad11b8f9e909c05226233bf755030Author: Michael Liao <[email protected]>Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme 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 3628164...
, so you can specify back to a future version:
$ git reset --hard 3628164HEAD is now at 3628164 append GPL
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:
is a distributed version control system.Git is free software distributed under the GPL.
Sure enough, I domain back again.
Git's version fallback is very fast because git has a pointer to the current version inside, and HEAD
when you roll back the version, git just points to the head append GPL
:
Instead, point to add distributed
:
And then by the way, the workspace files are updated. So you HEAD
can point to which version number, and you position the current version.
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:
$ git reflogea34578 head@{0}: reset: moving to head^3628164 head@{1}: commit: append gplea34578 head@{2}: commit: add distributedcb926e7 head@{3}: commit (initial): wrote a Readme file
Finally relieved, the second line shows append GPL
the commit ID is 3628164
, now, you can take the time machine back to the future.
Summary
Now summarize:
HEAD
The 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 tutorial-version fallback