Git accelerated learning notes finishing in Liao Xuefeng teacher's Official Web site: https://www.liaoxuefeng.com/
- Modify management
First, we need to be clear, why Git is to modify the management rather than the document?
We first for existing readme.txt modify files, such as adding a single 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.
Then add:
$ git add readme.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
Then modify the readme.txt 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
submit:
$ git commit -m "git tracks changes" [master 519219b] git tracks changes 1 file changed, 1 insertion(+)
After submitting View status:
$ 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")
We found that the command line results show that the second modification did not submit?
Review Procedure: First Revision -> git the Add -> The Second Amendment -> git the commit
Earlier, we talked about, Git management is modified, when you use git add command, in the work area last modified was put into the staging area, ready for submission.
But in the second revision of the work area and not into the staging area. So git commit only responsible for the submission to modify the staging area, which is the first to submit revised
The second modification will not be submitted.
When you use git diff HEAD - readme.txt can view the workspace and repository command from the difference between the difference between the latest version:
$ git diff HEAD -- readme.txt diff --git a/readme.txt b/readme.txt index 76d770f..a9c5755 100644 --- a/readme.txt +++ b/readme.txt @@ -1,4 +1,4 @@ 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. +Git tracks changes of files.
How that submission of the second modification of it?
You can continue to git add and git the commit , can also be modified to git a second time, then git the commit , the equivalent of a two revisions submitted after the merger:
First Revision -> git the Add -> The Second Amendment -> git the Add -> git the commit
- summary
Now you should understand how Git is tracking changes, each modified, if not add git to the staging area, it will not be added to commit in.
git diff filename: compare the work area and staging area
git diff HEAD - filename: Compare the working area and the latest repository version
If the git diff output gaps to explain the work area is (should be the same clean and refers to the comparison of the area) clean
- Undoing Changes
When you modify a file in some wrong, but you still submitted. For example, you add in the readme.txt:
$ 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.
Because it is timely errors found, you can delete the last line, manually restore files to previous versions of the state. If git status to see:
$ 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 will find, Git tell you, git Checkout - File can be discarded modify the workspace:
$ git checkout -- readme.txt
This command means that the readme.txt file complete withdrawal of modifying the work area, there are two cases:
-
- readme.txt since has not been modified into the staging area, and now, back and undo changes to the repository exactly the same state;
- readme.txt post 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 this is to make the file back to the state at the time of a recent git commit or git add.
Now we look at the contents of readme.txt:
$ 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.
File really recovered.
git checkout -- file
Command --
is very important, is not --
, it becomes a command "Switch to another branch," we will meet again later in the management of the branch git checkout command.
Now is there, you will not only wrong document, 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
But fortunately, the commit until you find the problem. With git status check, modify only added 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, use 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 reset command either rollback version, can also modify the temporary area to fall back to the work area. When we use HEAD , it means that the latest version.
Then git status check, now staging area is clean, there is a modification of the work area:
$ 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
Now you not only make mistakes in something from scratch also submitted to the repository. You can fall back to the previous version, but it is conditional, that is, you do not have to push their own local repository to the remote.
Back to the remote repository will learn, once you put the wrong content submitted to a remote repository, then the GG ......
- Summary
- When you modify the contents of a file mess, like discarded directly modify the workspace, use the command git checkout - file
- When you modify not only upset the contents of a file, also added to the staging area, to discard the changes in two steps.
- git reset HEAD <file> Return to Scene 1
- A second step according to Scene 1 operation
- Has submitted an inappropriate changes to the repository, you want to withdraw this submission, referring to the first class , but the premise is not pushed to the remote repository
You help me please Alipay pair of Coke ~ ~ ~
$ git checkout -- readme.txt
$ git status
On branch master
nothing to commit, working tree clean