The product says we don't want this feature. -Three minutes of programming "
Part 7
Undo temporary files and switch file versions arbitrarily
Sometimes we want to take out the files in the temporary storage area.
1. Temporary storage area to work area
If we have just performed git reset --soft
or add
waited for an operation and added something to our temporary storage area, such as log files, we will take them out of the temporary storage area.
git status
Check the temporary storage area, there is a mysql.log in itgit reset -- mysql.log
Tomysql.log
take outgit status
You can see that it is really taken out and then rm off if you don’t want this file, but if these files are automatically generated every time you have to take out the temporary storage area in this way, it’s really tired, we can ignore it with git Documents that you don't want to submit (detailed description in follow-up tips)
Two, roll back the file to a commit
What do we do when we want to arbitrarily roll back a certain file to a certain commit without changing the status of other files? We have two situations. One is that we just want to have a modified file in the work area and directly discard his current modification; the second is that we want to roll back this file to a previous submission. Let’s talk about the first
1. Cancel the modification of the file in the workspace
The updated
time.txt
content, you canstatus
see that he has changedgit checkout -- time.txt
, Cancel this modification in the work area. If it has beenadd
added to the temporary storage area, then this command is useless. What it means is to cancel this modification in the work area and go to the place where it was last saved. If not,add
it will return to the same state as the version library; if it has been added to the temporary storage area and has been modified, then it will be added to the temporary storage area.
2. Roll back the file to any version
What we are talking about here is to roll back the file to the state of a previous version. The complete meaning is to keep the content of other files unchanged, change this file to a previous version, and then modify it to your satisfaction and do the next time Submission.
Core command
git checkout [<options>] [<branch>] -- <file>...
Let’s use time.txt
this file for experimentation. First, we will produce three versions. Here I have done it. Let’s take a look: Version 1, time.txt content 00:50
Version 2, time.txt content 18:51
Version 3, time.txt content 10:41
The current version is version 1. Let's check out version 3. Reach the state shown in the figure below
Use
checkout
+commit id
+-- filename
combination, spanning the history of version 2 version 3time.txt
do outCheck the status, time.txt has been changed
Let's restore time.txt to version 1. The same method, because version 1 is the last submission, we can omit the version number
See it! As long as you use git checkout commit_id -- filename
the combination , you can get the historical version of the file you want.
When you get here, you may be confused , reset
andcheckout
it really seems like the order! Can be used to undo
checkout
Semantically, something is taken out, so this command is used to copy files from the historical commit (or temporary storage area) to the working directory, and it can also be used to switch branches.reset
Semantically it is reset, so this command points the current branch to another location, and selectively changes the working directory and index. It is also used to copy files from the historical repository to the index without moving the working directory.
Small summary
Remove files in the temporary storage area: git reset - file name
Cancel this modification in the workspace: git checkout - file name
Roll back the file to the state of a previous version: git checkout
+ commit id
+-- filename