naturally, you are not going to make a mistake. But it's two o'clock in the morning, and you're working on a job report, and you 've readme.txt added a line to it:
$ cat readme.txt
Git 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.
My stupid boss still prefers SVN.
before you're ready to submit, a cup of coffee has played a role, and you suddenly found "Stupid boss" you might lose this month's bonus!
now that the error has been found in time, it can be easily corrected. You can delete the last line, manually restore the file to the previous version of the state. If you take a git status look:
git status
# on Branch Master
# changes not staged for commit:
# (use "git add <file> ..." To update what would 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")
as you can see, Git will tell you that git checkout -- file you can discard modifications to the workspace:
git checkout -- readme.txt
Command git checkout -- readme.txt it means to put readme.txt The files in the workspace are all undone, here are two things:
one is 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 after it has been added to the staging area, it has been modified, and now the undo changes go back to the state that was added to the staging area.
Anyway, let's get this file back to the last time. git commit or git add state of the time.
now, look at readme.txt the contents of the file:
$ cat readme.txt
Git 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 contents of the document were restored.
git checkout-- filein the command -- It's important, No. -- , it becomes " Switch to another branch " command, we will encounter git checkout again in the later branch management command.
It's supposed to be morning . 3 point, you have not only written some nonsense, but also git add to the staging area:
cat readme.txt
Git 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.
My stupid boss still prefers SVN.
git add readme.txt
Fortunately, in commit before, you discovered this problem. With git status A look, the changes were added to the staging area, not yet committed:
git status
# on Branch Master
# Changes to be committed:
# (use "git reset HEAD <file> ..." to Unstage)
#
# Modified:readme.txt
#
Git also tell us to use the command git reset HEAD file You can undo the staging area changes ( Unstage ) and re-put it back in the workspace:
$ git reSet HEAD Readme.txt
Unstaged changes after Reset:
M Readme.txt
git resetThe command can be rolled back to the version, or the staging area's modifications can be rolled back to the workspace. When we use HEAD it, the latest version is indicated.
re-use git status Look, now the staging area is clean and the workspace has been modified:
git status
# on Branch Master
# changes not staged for commit:
# (use "git add <file> ..." To update what would 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")
Remember how to discard changes to the workspace?
git checkout -- readme.txt
git status
# on Branch Master
nothing to commit (working directory clean)
The whole world is finally quiet!
now, suppose you have not only changed the wrong thing, but also submitted it from staging area to the repository, what should I do? Do you remember the version fallback section? Can be rolled back to the previous version. However, this is conditional, that you have not yet pushed your local repository to remote. Remember Git is a distributed version control system? We'll talk about the remote repository later, and once you push the "Stupidboss" commit to the remote repository, you're really miserable ...
Summary
It's time to summarize again.
Scene 1 : When you mess up the contents of a file in your workspace and want to discard changes to the workspace directly, use the command git checkout -- file .
Scene 2 : When you have not only changed the contents of a file in the workspace, but also added to the staging area, want to discard the changes, in two steps, the first step with the command git reset HEAD file , it's back to the scene 1 , the second step by scene 1 operation.
Scene 3 : If you have submitted an inappropriate modification to the repository, you want to revoke this submission, refer to version Fallback section, but only if it is not pushed to the remote library.
git5--undo Changes