Now, let's consider a situation when you modify a file, but you want to undo it at this point, what do you do? Below, let's experiment with the code:
(1) Add a new line to the Readme.txt:
is are free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. My stupid boss still prefers SVN.
(2) "My stupid boss still prefers SVN." is an unwise choice, and we can actually undo it:
$ git status# on branch master# Changes not staged for commit:# ( use"git add <file> ..."To update what'll be committed)# ( use"Git checkout-<file>, ..." To discard changes in working directory)# # Modified:readme.txt#no changes added to commit ( use"git add"and/or"git commit-a")
(3) We can find that git checkout-<file> You can discard changes to the workspace, code:
$ git checkout--readme.txt
There are two things to be aware of:
One is that readme.txt
since the modification has not been put into the staging area, now, undo changes back to the same state as the repository;
One is readme.txt
added to the staging area, and then modified, and now, undo changes back to the state after adding to staging area.
All in all, let this file go back git commit
to git add
the last state or time.
(4) Now let's see if this is the case:
is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files.
The fact is that it restores the previous content.
(5) for git checked-file This command requires special attention: If you don't add "--", then git checked becomes " switch to another branch "command.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------
We thought about it, when you wrote something you shouldn't have, but you found the problem before add, and then you undo it, so what if you get excited about the add?
is are free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. My stupid boss still prefers svn.$ git add readme.txt
At this point, let's git status and look at the situation:
$ git status# on branch master# changes to be committed:# " git reset HEAD <file> " to Unstage) # # modified: readme.txt#
At this point, we can't undo it with Git checked-readme.txt. There is still some way, Git is very powerful!
Git also tells us to use the git reset HEAD file
command to undo the staging area changes (Unstage) and re-put them back into the workspace:
$ git reset HEAD readme.txtunstaged changes after reset:m readme.txt
Here we can see that git reset has two functions: (1) fallback version, (2) staging area The changes back to the workspace; head indicates the latest version
Let's git status a bit:
$ git status# on branch master# changes not staged for commit:# (use " git add <file>.. " Span style= "color: #800000;" >git Checkout--<file> ..... to discard changes in working directory) # # Modified:readme.txt#no changes added to commit (use git add " and/or " git commit-a " )
OK, now the modification is the workspace modification, what to do next? Undo the changes to the workspace, of course? How does the workspace change? Do you still need me to teach you?
$ git checkout-- readme.txt$ git status# on branch masternothing to commit (working directory clean)
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------
OK, no add, add, we all analyzed, then suppose you add, and commit? Do you remember the version fallback that was mentioned in the previous posting? We can go back to the previous version .
Of course, if you commit, and also push the local repository to the remote repository, OK, I can not save you. (Git is a distributed operating system, what is the remote repository, the post will be explained later)
---------------------------------------------------------------------------------------------------Summary-------------------- -----------------------------------------------------------------------------------------
Scenario 1: When you mess up the contents of a file in your workspace and want to discard the workspace changes directly, use the command git checkout -- file
.
Scenario 2: When you not only changed the contents of a file in the workspace, but also added to the staging area, want to discard the changes, two steps, the first step with the command git reset HEAD file
, back to Scene 1, the second step by scene 1 operation.
Scenario 3: when an inappropriate modification to the repository has been submitted, you want to undo this commit, refer to the Git Learning Note (ii), but only if it is not pushed to the remote library.
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------
Finally, let's take a look at the delete, delete is also the modification operation. We use the code to combat a bit:
" Add Test.txt " 11 insertion (+100644 test.txt
If you want to perform a delete operation, we can either delete it directly in the file Manager or use the following command:
$ RM test.txt
At this point, the workspace and version area are inconsistent, and git status tells us which files have been deleted:
$ git status# on branch master# changes not staged for commit:# (use " git add/rm <file>.. " Span style= "color: #800000;" >git Checkout--<file> ..... to discard changes in working directory) # # Deleted:test.txt#no changes added to commit (use "
git add " and/or " git commit-a " )
Now, we have two options:
(1) Decide to delete this file from the repository, then command git rm to delete it, then git commit:
' Test.txt ' "remove test.txt"11 deletion (- 100644 test.txt
(2) The other is wrong, because the repository is still there, it is easy to restore the deleted files to the latest version:
$ git checkout--test.txt
git checkout
Instead, replace the workspace version with the version in the repository, which can be "one-click Restore", regardless of whether the workspace is modified or deleted.
Learn git every day-notes (iv)