Study Notes --- git undo changes

Naturally, you will not make mistakes. But now two in the morning, you are to catch a work report, you readme.txtadd the line:

$ 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 are ready to submit a cup of coffee played a role, you suddenly discovered stupid bossmay make you lose bonus this month!

Since the errors found very timely, you can easily correct it. You can delete the last line, manually restore the file to a version of the state. If a git statuslook:

$ 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 find, Git will tell you, git checkout -- fileyou can be discarded modify the workspace:

$ git checkout -- readme.txt

Command git checkout -- readme.txtmeans that, put readme.txtall the documents revoked modify the workspace, there are two cases:

One is readme.txtfrom the modification has not been placed in temporary storage area, and now, back and undo changes to the repository exactly the same state;

One is readme.txtpost has been added to the staging area, has been modified and now returns to undo changes added to the state after the staging area.

In short, it is to make the file back to the last git commitor the git addstate of.

Now, look at readme.txtthe 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.

  

Sure enough, the contents of the file recovery.

git checkout -- fileCommand --is very important, is not --, it becomes a command "Switch to another branch," we will meet again later in the branch management git checkoutcommands.

 

Now suppose is 3:00, you not only wrote some nonsense, but also git addto 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 commitbefore, you find the problem. With a git statuslook, just add modifications to the staging area, has not yet submitted:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt

  

Git also told us that the command git reset HEAD <file>can modify the staging area off of revocation (unstage), back into the workspace:

$ git reset HEAD readme.txt
Unstaged changes after reset:
M	readme.txt

  

git resetCommand can either rollback version, can also modify the temporary area to fall back to the work area. When we use HEAD, it indicates the latest version.

And then git statuslook, now staging area is clean workspace Edit:

$ 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

  

Remember how it discarded modify the workspace?

$ git checkout -- readme.txt

$ git status
On branch master
nothing to commit, working tree clean

  

The whole world finally quiet!

 

Now, suppose you not only make mistakes in something from scratch also submitted to the repository, how to do it? Remember the version rollback one do? You can fall back to the previous version. However, this is conditional, that is, you do not have to push their own local repository to the remote. Remember Git is a distributed version control system? We'll come back a remote repository, once you stupid bosssubmit push to a remote repository, you really miserable ......

summary

He went to the summary time.

Scenario 1: When you change the contents of a file chaotic workspace want direct discard modify the workspace with the command git checkout -- file.

Scenario 2: When you change not only upset the contents of a file workspace, also added to the staging area, to discard the changes in two steps, the first step command git reset HEAD <file>, returned to the scene 1, the second step by scene 1 operation.

Scenario 3: You have submitted improper modifications to the repository, you want to withdraw this submission, reference version rollback one, but the premise is not pushed to the remote repository.

Guess you like

Origin www.cnblogs.com/saryli/p/11368003.html