git study notes(2)
reference Liao Xuefeng’s blog
1.look through the whole versions
Let’s suppose a situation: you modified readme.txt again. And now you have 3 versions in your repository:
vertion 1:wrote a readme file
Git is a version control system.
Git is free software
vertion 2:add distributed
Git is a distributed version control system.
Git is free software
vertion 3:append GPL
Git is a distributed version control system.
Git is free software distributed under the GPL.
Now this system can give us detailed imformation about vertions if we input git log
:
$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Tue Aug 20 15:11:49 2013 +0800
append GPL
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date: Tue Aug 20 14:53:12 2013 +0800
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date: Mon Aug 19 17:51:55 2013 +0800
wrote a readme file
If you think it’s littery, you can add a parameter --pretty=oneline
,then it looks cleaner:
$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
2.Go back to your previous version
Let’s take a time machine to go back to our last version(add distributed).First of all, git should know the exact num of each version. And In git, we use HEAD
to imply the newest version, and the last one can express like: HEAD^
, and the version before last is HEAD^^
. But if we want to go back 100 versions, we can write HEAD~100
. Now ,git reset
can be used to go back to our previous version:
$ git reset --hard HEAD^
HEAD is now at ea34578 add distributed
But if you are an idiot, you regreted(hhh).Now you want your append GPL, but you can’t find the commit id(in git,you can use commit id to go to every version you want,maybe??i guess). Don’t worry, Git gives us a order named git reflog
to record every orders you made:
$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file
Ok, you can see your append GPL commit id is 3628164.So you can input like this to recover your file:
$ git reset --hard 3628164
HEAD is now at 3628164 append GPL
summary
Now let’s summarize what we’ve learnt:
HEAD
is a pointer pointing to the current version, and we can usegit reset --hard commit_id
to shuttle back and forth between these versions.- Before we shuttle, we can use
git log
to see the submit history to see which version we want to go. - We can use
git reflog
to see historical orders which include the future version commit id.