Version back
- Now that you’ve learned how to modify files and commit your changes to Git repositories, try again and modify the readme.txt file as follows:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Copy the code
- Then try to submit:
$ git add readme.txt
$ git commit -m "append GPL"
[master 1094adb] append GPL
1 file changed, 1 insertion(+), 1 deletion(-)
Copy the code
- Like this, you’re constantly making changes to the file and then committing changes to the repository, just like in an RPG where every level you pass is automatically saved, and if you fail to pass a level, you can choose to read the state of the previous level. Sometimes, before a Boss fight, you’ll manually save the game so you can pick up from the nearest place if the Boss fight fails. Git is the same way. Whenever you feel your files have changed enough, you can”Save a snapshotThis snapshot is called in Git
commit
. If you make a mess or delete a file by mistake, you can always go to the nearest onecommit
Recover and get back to work instead of losing months of work. - Now, let’s review
readme.txt
There are several versions of files that have been committed to the Git repository: - Version 1:
wrote a readme file
Git is a version control system.
Git is free software.
Copy the code
- Version 2:
add distributed
Git is a distributed version control system.
Git is free software.
Copy the code
- Version 3:
append GPL
Git is a distributed version control system.
Git is free software distributed under the GPL.
Copy the code
- In practice, of course, it’s impossible to remember in your head what a document with thousands of lines has changed every time, otherwise you wouldn’t need a version control system. There must be some command in the version control system that tells us the history. In Git, we use
git log
Command view:
$ git logCommit 73396987016561 fb394bc3372a207c113ce42d7f (HEAD - > master) Author: user name < email > Date: Sat Aug 1 09:55:28 2020 + 0800 append the GPL commit 4 ee7803fd43a88438d6c24c308e74f5e2625e25e Author: user name < email > Date: Sat Aug 1 09:46:49 2020 + 0800 add distributed commit three f0724615d7e891dc9626491a3467657edfa3e89 Author: user name < email > Date: Sat Aug 1 09:38:45 2020 +0800 wrote a readme fileCopy the code
The git log command displays the latest and most recent commit logs. You can see three commits: append GPL, Add Distributed, and Wrote a Readme file.
- If the output information is too much, see dazzling, you can try to add
--pretty=oneline
Parameters:
$ git log --pretty=oneline
73396987016561fb394bc3372a207c113ce42d7f (HEAD -> master) append GPL
4ee7803fd43a88438d6c24c308e74f5e2625e25e add distributed
3f0724615d7e891dc9626491a3467657edfa3e89 wrote a readme file
Copy the code
- And just as a reminder, you see a bunch of similar things
7339698
… isCommit ID (version number)
Unlike SVN, Gitcommit id
notOne, two, three...
Increasing numbers, but oneSHA1
A very large number calculated withhexadecimal
And what you seecommit id
It’s not the same as mine. Yours will prevail. whycommit id
Why do I have to use this big string of numbers?Because Git is a distributed version control system, we will have to study how many people work in the same version library. If everyone uses 1,2,3… As a version number, that’s definitely a conflict. - Every time you commit a new version, Git actually strings them together into an automatic timeline. If you use a visual tool to view Git history, you can see the timeline of commit history more clearly:
- All right, now let’s fire up the time machine and get ready to
readme.txt
Go back to the previous version, which isadd distributed
How do you do that? - First, Git must know which version is currently available. In Git, use
HEAD
Represents the current version, which is the latest commit7339698
… (Note that my submission ID is definitely different from yours), the previous version wasHEAD^
The previous version wasHEAD^^
Of course up100
A version of the writing100个^
It’s a little bit harder to count, so let’s write itHEAD~100
. - Now, we’re going to put the current version
append GPL
Rollback to the previous versionadd distributed
, can be usedgit reset
Command:
$ git reset --hard HEAD^
HEAD is now at 4ee7803 add distributed
Copy the code
What is the value of the hard parameter? I’ll talk about that later, but for now, you can use it safely.
- Take a look at
readme.txt
The content is not versionadd distributed
:
$ cat readme.txt
Git is a distributed version control system.
Git is free software.
Copy the code
It was restored.
- You can continue to roll back to the previous version
wrote a readme file
But wait, let’s use itgit log
Take a look at the current state of the repository:
$ git logCommit 4 ee7803fd43a88438d6c24c308e74f5e2625e25e (HEAD - > master) Author: user name < email > Date: Sat Aug 1 09:46:49 2020 + 0800 add distributed commit three f0724615d7e891dc9626491a3467657edfa3e89 Author: user name < email > Date: Sat Aug 1 09:38:45 2020 +0800 wrote a readme fileCopy the code
- The latest edition
append GPL
It’s gone!It’s like you took a time shuttle from the 21st century to the 19th century, and you can’t go back. What do you do? - There is a way, as long as the command line window above is still open, you can just go up and up and down and find it
append GPL
thecommit id
is7339698...
, so you can specify a future version:
$ git reset --hard 73396
HEAD is now at 7339698 append GPL
Copy the code
- It is not necessary to write the full version number, just the first few digits will do, Git will automatically find it. Of course, you can’t just write the first one or two, because Git may find multiple version numbers and never know which one it is.
- And look carefully
readme.txt
Content:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Copy the code
Sure enough, the version went back to the third revision of the version content.
- Git version rollback is very fast because Git has an internal reference to the current version
HEAD
The pointer, when you roll back the version, Git is just a pointerHEAD
From the point toappend GPL
:
┌ ─ ─ ─ ─ ┐ │ HEAD │ └ ─ ─ ─ ─ ┘ │ └ ─ ─ > a. a. add distributed append the GPL │ │ demonstrate a readme fileCopy the code
- To point to
add distributed
:
┌ ─ ─ ─ ─ ┐ │ HEAD │ └ ─ ─ ─ ─ ┘ │ │.through append the GPL │ │ └ ─ ─ >.through the add distributed │ demonstrate a readme fileCopy the code
- I also updated the files in the workspace. So you get
HEAD
Whatever version number you point to, you locate the current version. - Now, what if you fall back to a version, turn it off, regret it the next morning, and want to go back to the new version? Can’t find the new version
commit id
How to do? - In Git, there is always a remedy for regret. When you use
$ git reset --hard HEAD^
Back to theadd distributed
Version, then want to restore toappend GPL
You have to find itAppend Commit ID of the GPL
. Git provides a commandgit reflog
To record every command you make:
$ git reflog
7339698 (HEAD -> master) HEAD@{0}: reset: moving to 73396
4ee7803 HEAD@{1}: reset: moving to HEAD^
7339698 (HEAD -> master) HEAD@{2}: commit: append GPL
4ee7803 HEAD@{3}: commit: add distributed
3f07246 HEAD@{4}: commit (initial): wrote a readme file
Copy the code
- Finally, a sigh of relief, and from the output,
Append Commit ID of the GPL
is7339698
Now you can travel back to the future in a time machine.
summary
HEAD
The version pointed to is the current version, so Git allows us to travel through the history of versions using the command:
git reset --hard commit_id
.- Before the shuttle, use
git log
You can view the commit history to determine which version you want to fall back to. - To go back to the future, use
git reflog
Review the command history to determine which version to go back to in the future.