Git learning Notes (2)-version fallback, and staging area's understanding

Source: Internet
Author: User

This article mainly records the version fallback, as well as the work area, staging area concept understanding.

// before you begin, review the previous content, modify the file as follows, and submit it to the repository. free software distributed under the GPL.  

1. View repository Information

git log
    //as you can see, there are 3 versions belowcommit 71038bf6cbd05ef559549a13a351aa8465be6c4d //This line is for each version of Idauthor:joe<[email protected]163.com>Date:sat May9  the: .: -  -+0800Append gplcommit 942f575de9e925d256ca4a704e1a37de18697bafauthor:joe<[email protected]163.com>Date:sat May9  -:Wuyi: -  -+0800Add Distributedcommit B401faf255e6a9480b1ed903a2b392ccf4628910author:joe<[email protected]163.com>Date:sat May9  One: on: -  -+0800Joe's first txt[Email protected]:~/joe/learngit$
git log--pretty=oneline
    // we can also simply display the information  71038bf6cbd05ef559549a13a351aa8465be6c4d Append gpl942f575de9e925d256ca4a704e1a37de18697baf add distributedb401faf255e6a9480b1ed903a2b392ccf4628910 Joe's first txt

2, version of the fallback

// in Git, the head represents the current version, the previous version is head^, the last version is head^^, of course, 100 versions of the Write 100 ^ more easy to count, so written head~100. 
git reset--hard head^
HEAD is now located in 942f575 add Distributed[email protected]:
Cat Readme.txt
     // Check the contents of the file and find that it has been rolled back. free Software[email protected]:  
git log--pretty=oneline
    // re-check the version information, found that the remaining 2 942f575de9e925d256ca4a704e1a37de18697baf add distributed    /// that is, after returning, I can't come back. Actually, it's possible. b401faf255e6a9480b1ed903a2b392ccf4628910 Joe's first txt
git reset--hard 71038bf6
        // as long as you know the ID of a certain version, we can go. (ID is a string in front of the version information, we just remember the first few can, GIT will automatically assign detection.) HEAD now at 71038BF append gpl[email protected]:cat  readme.txt Free Software distributed under the Gp[email protected]:
git reflog
    // If you do not know the ID and there is no version information, we can view the command log records. 71038bf [email protected]{0}: reset:moving to 71038bf6942f575 [email protected]{1}: Reset:moving to head^71038bf [email protected]{2}: Commit:append gpl942f575 [Email protected]{
    3}: Commit:add distributedb401faf [email protected]{4}: Commit (initial): Joe'  s first txt

3. Concept Understanding:

Workspaces (Working Directory): For example, learngit a folder is a workspace.

Repository (Repository): The workspace has a hidden directory .git , not a workspace, but a git repository. Git's repository contains a lot of things, the most important of which is called the stage (or index) of the staging area, as well as git for us to automatically create the first branch master , and pointing master to a pointer called HEAD .

The previous time we added the file to the Git repository, it was executed in two steps:

The first step is to add the file git add , in effect, to add the file to the staging area;

The second step is to commit the git commit changes, in effect, to commit all the contents of the staging area to the current branch.

Since we created the Git repository, Git automatically created the only branch for US master , so now it's time to git commit commit the changes to the master branch. You can simply understand that the file changes that need to be submitted are all put to staging area, and then all changes to staging area are submitted at once.

(For a deep understanding of these ideas, we recommend that you write some documents to test, modify the next file, check the status, add and then review the status, submitted to review the status. )

4. Why Git is better designed than other version control systems because git tracks and manages changes, not files.

Test ideas: Modify content, add (do not submit), and then modify, submit. You will find that the second modification has not been submitted.

This is because git manages the modification, when you use the git add command, the first modification in the workspace is put into staging area, ready to commit, however, the second modification in the workspace is not put into staging area, so it is git commit only responsible for staging area changes submitted, that is, the first modification was submitted, The second modification will not be submitted.

Once submitted, use git diff HEAD -- readme.txt the command to see the difference between the latest version of the workspace and the repository:

// first look at the workspace is clean [Email protected]:~/joe/git status in branch Master no file to submit, clean workspace [email protected]:lsabc.c  Readme.txt // modified the file VI  Readme.txt [email protected]:
git diff HEAD--Readme.txt
    // See some differences diff --git a/readme.txt b/6398377100644---A/readme.txt+ + +  -3,4 +3,3 free software distributed under the IS You OK? yes,i am fine. What's about you?
-I am bad!
     // The difference is marked in red, obviously [Email protected]:~/joe/learngit$
git status
 //  View status, found without staging add   The branch master has not  staging  pending changes: (using   " git add <file> ...   "  update the content to be submitted) (using the   " git checkout-<file> ...   "  change to discard workspace) Modified: Readme.txt Modification has not yet joined the commit (using the   " git add   " and/or "   git commit-a   " ) [email protected]:  ~/joe/learngit$ 
git add readme.txt
     // add later, view status, is not submitted [Email protected]:~/joe/git status at branch master to commit the change:  "git reset HEAD <file>, ..... " withdrawal staging area)    modified:     readme.txt[email protected]:~/joe/learngit$
git diff HEAD--Readme.txt
 //  Look at the contrast, there are still differences, although add, but it is different from the repository.  diff --git a/readme.txt b/ Readme.txtindex  6398377 .. 3F762EE 100644 ---a/readme.txt  + + + b/< Span style= "color: #000000;" >readme.txt@@ -3 , 4  +3 , 3  @@ Git is free   software distributed under the is you OK ?  yes,i am fine. What are you ?-I am bad!  [email protected]:  ~/joe/learngit$ 
git commit-m "del aline"
        // There is no difference until it is submitted.  1file1 deletion (-) [email protected]:
git diff HEAD--
Readme.txt [Email protected]:
git status
    // The work area is clean. in Branch Master no file to submit, clean workspace [email protected]:~/joe/learngit$

Git learning Notes (2)-version fallback, and staging area's understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.