We have successfully submitted and added a readme.txt file, now is the time to continue to work, so that we continue to modify readme.txt file, change the following:
Git is a distributed version control system. Git is free software.
Now, run the git status
command to see the results:
$ 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")
git status
Command allows us to always grasp the current state of the repository, the output of the above command tells us, readme.txt
has been modified, but not yet ready to modify submitted.
Although Git tell us readme.txt
is modified, but if we can look at what specific changes to the content, nature is good.
So, we need to use git diff
this command to see:
$ git diff readme.txt diff --git a/readme.txt b/readme.txt index 46d49bf..9247db6 100644 --- a/readme.txt +++ b/readme.txt @@ -1,2 +1,2 @@ -Git is a version control system. +Git is a distributed version control system. Git is free software.
git diff
As the name suggests is to look at difference, the display format is the common Unix diff format, you can see from the above command output, we've added a line in the first distributed
word.
To know readme.txt
after what changes were made, and then submit it to the warehouse worry more, commit changes and submit new documents are the same two steps, the first step is git add
:
$ git add readme.txt
Also no output. In the second step git commit
before we run git status
a look at the current state of warehouse:
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: readme.txt
git status
Tells us that changes will be submitted to include readme.txt
the next step, you can safely submitted:
$ git commit -m "add distributed" [master e475afc] add distributed 1 file changed, 1 insertion(+), 1 deletion(-)
Once submitted, we then git status
command to see the current state of the repository:
$ git status On branch master nothing to commit, working tree clean
Git tell us modify not currently required to be submitted, and that the working directory is clean (working tree clean) is.
summary
-
To keep abreast of the status of the work area, use the
git status
command. -
If
git status
told you have files have been modified, with thegit diff
modified content can be viewed.
Version rollback
Now that you have learned to modify the file, and then changes into Git repository now, then practice again, modify the readme.txt file as follows:
Git is a distributed version control system. Git is free software distributed under the GPL.
Then try to commit:
$ git add readme.txt $ git commit -m "append GPL" [master 1094adb] append GPL 1 file changed, 1 insertion(+), 1 deletion(-)
In this way, you constantly make changes to the file, and then continue to commit changes to the repository, like playing RPG games, each game will automatically save the state through a pass, if not some off the past, you can also choose to read before a take off state. Sometimes, before playing Boss, you will save manually, so that in case of failure to fight Boss, you can restart from the nearest place. Git is the same, whenever you feel file modification to a certain extent, can "save a snapshot" This snapshot is called in Git commit
. Once you file a change of chaos, or mistakenly deleted files, but also from a recent commit
restoration, and then continue to work, rather than the outcome of several months of work lost.
Now, we look at readme.txt
the file There are several versions of Git is submitted to the warehouse:
版本1:wrote a readme file
Git is a version control system. Git is free software.
Version 2: add distributed
Git is a distributed version control system. Git is free software.
Version 3: append GPL
Git is a distributed version control system. Git is free software distributed under the GPL.
Of course, in practice, we might remember how my mind a few thousand lines of each file have changed what, or to do version control system. Version control system must have a command history can tell us, in Git, we use the git log
View command:
git log
Command to display the most recent commit log from the farthest we can see the 3 submission, most recently append GPL
, the last time add distributed
, the first one was wrote a readme file
.
If the output too much information, see dazzling, you can try to add --pretty=oneline
parameters:
$ git log --pretty=oneline 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL e475afc93c209a690c39c13a46716e8fa000c366 add distributed eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
Tips required is that you see the long list of similar 1094adb...
is commit id
(version number), and not the same as SVN, Git is commit id
not 1,2,3 ...... incremental number, but a SHA1 calculated from a very large digital, hexadecimal, and you see commit id
, and I certainly do not like to your subject. Why commit id
we need to use such strings of numbers that represent it? Because Git is a distributed version control system, we have to study people back to work in the same repository, if everyone uses 1,2,3 ...... as the version number, it is certainly on the conflict.
Each submitted a new version, in fact, Git will automatically put them strung together a timeline. If you use visualization tool to view Git history, we can see more clearly the submission of the timeline of history:
First of all, Git must know the current version of which version, in Git, use HEAD
represents the current version, which is the latest submitted 1094adb...
(note my submission ID and you are certainly not the same), the previous version is HEAD^
, the previous version is HEAD^^
, Of course, up 100 write 100 version ^
is easier to count, so written HEAD~100
.
Now, we want the current version append GPL
to fall back to the previous version add distributed
, you can use git reset
the command:
$ git reset --hard HEAD^ HEAD is now at e475afc add distributed
Take a look at readme.txt
the content is not the version add distributed
:
$ cat readme.txt Git is a distributed version control system. Git is free software.
Sure enough, it was restored.
You can continue to fall back to the previous version wrote a readme file
, but wait a minute, and then we git log
look at the current state of the repository:
$ git log commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master) Author: Michael Liao <[email protected]> Date: Fri May 18 21:03:36 2018 +0800 add distributed commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 Author: Michael Liao <[email protected]> Date: Fri May 18 20:59:18 2018 +0800 wrote a readme file
The latest version that append GPL
has been to see! You sit like Time Machine from the 21 century came in the 19th century, already want to go back and go back, swollen do?
Way but it is still there, as long as the above command line window has not been turned off, you can follow up and searched and searched ah, found that append GPL
of commit id
Shi 1094adb...
, so he can return to specify a future version:
$ git reset --hard 1094a HEAD is now at 83b0afe append GPL
The version number is not necessary to write the whole, the first few on it, Git will automatically look for. Of course not write only the first one or two, because Git may find more than one version number, you can not determine which one is the.
Then carefully look at readme.txt
the contents:
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL.
Sure enough, I Hu Hansan back.
Git version rollback speed is very fast, because Git inside there is a point to the current version of the HEAD
pointer when you roll back version of the time, Git is just from the HEAD points append GPL
:
┌────┐
│HEAD│
└────┘
│
└──> ○ append GPL
│
○ add distributed
│
○ wrote a readme file
To point to add distributed
:
┌────┐
│HEAD│
└────┘
│
│ ○ append GPL
│ │
└──> ○ add distributed
│
○ wrote a readme file
Then the way to update the document workspace. So you get HEAD
points which version number, you put the current version of the positioning is.
Now, you fall back to a version, turn off the computer, you'll regret it the next morning, and would like to return to a new version of how to do? Can not find the new version of commit id
how to do?
In Git, there is always regret it can eat. When you use $ git reset --hard HEAD^
retreated back to the add distributed
time of release, would like to return to append GPL
, you must find append GPL
the commit id. Git provides a command git reflog
to record your every command:
$ git reflog e475afc HEAD@{1}: reset: moving to HEAD^ 1094adb (HEAD -> master) HEAD@{2}: commit: append GPL e475afc HEAD@{3}: commit: add distributed eaadf4e HEAD@{4}: commit (initial): wrote a readme file
Finally sigh, we can see from the output, append GPL
the commit id Shi 1094adb
, now, and you can ride a time machine back to the future.
summary
To summarize:
-
HEAD
Directed version is the current version, therefore, Git allows us to shuttle between the versions of the history, use the commandgit reset --hard commit_id
. -
Before the shuttle, with the
git log
commit history can be viewed, to determine which version to fall back. -
To return to the future, with a
git reflog
view command history in order to determine what you want to return to a future version.