Xiaoding takes you into the world of git three-undo changes

Source: Internet
Author: User

I. Revocation of instructions
    • Features of the Git checkout restore workspace
    • Git Reset restore Staging area features
    • Git clean has not been added to staging area files that Git has not tracked yet can use this command to clear them
    • Git revert undo this commit.
Second, instruction explanation

Git Checkout

First, we modified the file by a modification to the master.txt.

Modify the result we use the above knowledge to carry on the view.

The new add test to this paragraph, if we want to add the contents of the workspace to staging area will use git add this command, if we want to restore the workspace content this time will use the git checkout command, actually do the work is to use staging area content to cover the content of our workspace. Look at the following actions:

Check out git status

found that our previous modifications were restored.

At this point we are modifying the master.txt and adding it to the staging area. Modify the content as follows

After the modification is complete, we will commit to staging area, using the git add command. Looking at the next state again becomes the state of changes to being committed.

We can use git reset HEAD (head~2) to restore to a single commit record. Because the commit that head points to is what we want to restore, that is, the commit that our head points to is that we want to overwrite the staging area data with this data.

By the time we look at git status, staging area's content is restored.

We can use this restore workspace

Git checkout V0-master.txt, V0 is the record that points to the commit that was first committed (here the V0 is a tag record for the commit). Let's look at the differences between workspaces and staging area:

Found that there was less than one line of submissions. We can then restore the workspace with the commit that the current head points to.

There is no difference at this time. Use commit record to restore the content format of staging area and checkout is the same. No demo is done here. (for example, restore staging area with v0 tag records, git reset v0–master.txt restore the specified file)

Create a new two file in the workspace and then do not add the file to staging area, and use git clean for cleanup.

This is when you use Git to clean–n the files you want to delete.

Then use git clean–f to delete these files.

Here we create a new vim. gitignore file that filters out files with the suffix O and a.

Add to the history, and then clean up the new file.

Three new files created TEST.O, test1.a, test2.c This is when we use Git clean–n to display a clear record is test2.c, by default he does not clean the files inside the Gitignore.

Here we are just the opposite we want to keep files other than ignore, clean out the files inside the ignore. That's when we're going to use the following command. Git chean-n-X followed by an uppercase X, and we found the result we wanted.

We execute it and clean out the files. Git chean-x–f This time clean up the. O and. A Files

We can use git revert to generate a new commit to overwrite the Ignore commit record we don't want. git revert HEAD

Below is a demonstration of how git revert works:

The first case: revert when we first submit a record. When we use touch to generate a file a, after the generation of a file into the staging area, this time will produce a Git object, this object is stored in the. git/objects directory, as shown in:

When we have add, there are two default folders under the objects file. A Git object is generated when Git is Add.

This is the Git object, a file that corresponds to a piece of content, with that content plus the specific header information together with the SHA-1 checksum to name the file. The first two characters of the checksum are used for naming subdirectories, and the remaining 38 characters are used as filenames. There is no explanation here. Then the overall sha-1 checksum is the filename + subdirectory name. When I call git commit, I generate a Commit object for a tree object, and the commit object points to the tree object.

Let's take a look at that sha-1. is the tree object which is the commit object, and the next commit object is verified that the following is not a tree object.

The structure of the overall generation of this commit is as follows:

When we git revert, we create a new tree object, which is a tree object that is created to undo the last modification.

It becomes the way it is now.

In the second case, when we revert the second time, we create a new commit that points to the original tree object. And then we're going to show you

The first step is to initialize a warehouse here is not much to say, the second is the following diagram of the steps to create a new two files, a. Two files, and then add a and B files separately to staging area, this time will produce Git object, we look at the type of Git object, determine the Blob type, The file of the staging area is then submitted to the record. The demo looks like this:

Next, we'll look at the object and the type of the object under the. git/objects file, and then we'll start preparing the second submission, first editing the a file added to the staging area, and then creating a Git object (BLOB). As shown in the following:

This is the time to see our newly added Git object content and Sha-1.

Next we'll look at the type of the new generated object, commit the Git commit for the second time, and when we commit, we'll generate a new object, and then look at the properties of the new object type, just to see if the first object is a commit object and look at his content. For details, see:

The next step is to see what the tree object and the elements under the tree are, see

So this is what we look at. The resulting record structure is as follows:

When we revert, a new commit is generated that points to the tree object that was committed for the first time. Let's take a look at the sha-1 of the newly generated commit object.

Now look at the record structure it produces as shown in the following:

Iii. Concluding remarks

This article is the research results and the study of the article after the summary, if the above problems have errors, please correct me, I found too many, should be a different way.

Xiaoding takes you into the world of git three-undo changes

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.