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:
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 prepare to submit, a cup of coffee has played a role, you suddenly found that "stupid boss" may let you 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 will 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")
You can see that git will tell you that you git checkout -- file
can discard changes to the workspace:
$ git checkout -- readme.txt
The command git checkout -- readme.txt
means to readme.txt
undo all changes to the file in the workspace, here are two things:
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.
In short, let this file go back to git commit
git add
the last state or time.
Now, look at readme.txt
the contents of the file:
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 -- file
The command is --
important, no --
, it becomes the "Create a new branch" command, we will encounter the command in the later branch management git checkout
.
Now suppose it is 3 o'clock in the morning, you have not only written some nonsense, but also git add
to the staging area:
$ cat readme.txtGit 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, commit
before, you discovered the 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 tells us to use the command to git reset HEAD file
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
git reset
The 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.
Again with a git status
look, now staging area is clean, the workspace has been modified:
$ git status# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will 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 masternothing 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 "stupid boss" commit to the remote repository, you're really miserable ...
Summary
It's time to summarize again.
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 revoke this commit, refer to the version fallback section, but only if it is not pushed to the remote library.
Git tutorial-undo changes