Study Notes --- git version return

 

 

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 statuscommand 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 statusCommand allows us to always grasp the current state of the repository, the output of the above command tells us, readme.txthas been modified, but not yet ready to modify submitted.

Although Git tell us readme.txtis modified, but if we can look at what specific changes to the content, nature is good.

So, we need to use git diffthis 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 diffAs 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 distributedword.

To know readme.txtafter 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 commitbefore we run git statusa 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 statusTells us that changes will be submitted to include readme.txtthe 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 statuscommand 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 statuscommand.

  • If git statustold you have files have been modified, with the git diffmodified 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 commitrestoration, and then continue to work, rather than the outcome of several months of work lost.

Now, we look at readme.txtthe 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 logView command:

git logCommand 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=onelineparameters:

 

$ 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 idnot 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 idwe 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 HEADrepresents 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 GPLto fall back to the previous version add distributed, you can use git resetthe command:

$ git reset --hard HEAD^
HEAD is now at e475afc add distributed

Take a look at readme.txtthe 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 loglook 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 GPLhas 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 GPLof commit idShi 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.txtthe 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 HEADpointer 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 HEADpoints 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 idhow to do?

In Git, there is always regret it can eat. When you use $ git reset --hard HEAD^retreated back to the add distributedtime of release, would like to return to append GPL, you must find append GPLthe commit id. Git provides a command git reflogto 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 GPLthe commit id Shi 1094adb, now, and you can ride a time machine back to the future.

summary

To summarize:

  • HEADDirected version is the current version, therefore, Git allows us to shuttle between the versions of the history, use the command git reset --hard commit_id.

  • Before the shuttle, with the git logcommit history can be viewed, to determine which version to fall back.

  • To return to the future, with a git reflogview command history in order to determine what you want to return to a future version.

 

Guess you like

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