Undo temporary files and switch file versions arbitrarily-play Git for three minutes a day (7)

 The product says we don't want this feature. -Three minutes of programming "

image


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 

image

If we have just performed git reset --softor addwaited 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.

image

  • git status Check the temporary storage area, there is a mysql.log in it

  • git reset -- mysql.logTomysql.log take out

  • git statusYou 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.txtcontent, you canstatus see that he has changed

  • git checkout -- time.txt , Cancel this modification in the work area. If it has been addadded 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.txtthis 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

image

Version 3, time.txt content 10:41

image

The current version is version 1. Let's check out version 3. Reach the state shown in the figure below

image

image

  • Use checkout+ commit id+ -- filenamecombination, spanning the history of version 2 version 3 time.txtdo out

  • Check 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

image

image

See it! As long as you use git checkout commit_id -- filenamethe combination , you can get the historical version of the file you want.

When you get here, you may be confused , resetandcheckout it really seems like the order! Can be used to undo

  • checkoutSemantically, 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.

  • resetSemantically 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


Guess you like

Origin blog.51cto.com/15076235/2608432