Git entry 2: Version history/backtracking

Source: Internet
Author: User

The introduction of git is not much said. Here is just a simple entry-level operation.

This section focuses on operations related to viewing and controlling historical versions.
Everything is simple, not reasonable, but simple operations.
Original article, if reproduced, please indicate the source: http://blog.csdn.net/yihui823/article/details/6681214

With Copper as the mirror, you can wear the crown;

Taking history as the mirror, you can know xingti;

With human eyes, you can understand the gains and losses

 

We want to use the old version as a mirror to know our own growth history

 

Go to the directory controlled by git and try gitk.

(Wow ~ With images)

Don't be excited.

 

Let's briefly describe an example: a) initialization operation

There are two files: file1.txtand file2.txt.

1. These two files are available during initialization.

Operation:

Git init

Git status

Git add.

Git commit-M "init version"

2. Modify file1.txt on the master branch and submit

Operation:

(Modify file1.txt)

Git add file1.txt

Git commit-m "Change file1"

3. Create a new branch banana and switch to the banana branch.

Operation:

Git branch banana

Git checkout banana

4. Modify and submit file1.txtand file2.txt.

Operation:

(Modify file1.txtand file2.txt)

Git add file1.txt file2.txt

Git commit-M "change by banana"

At this time, we can enter gitk to check the current version. Such as: git-001

5. Then, switch to the master branch, modify file2.txt, and submit.

Operation:

Git checkout master

(Modify file2.txt)

Git add file2.txt

Git commit-M "change by Master"

Enter gitk to view the current version, such as the git-002

 

B) Check the historical version to locate the problem.

We now find that the current version is faulty and cannot be submitted to the version library.

1. We need to return from git commit.

Enter:

Git reset -- soft head ^

The head is the latest version of the current branch. ^ Indicates the parent node. The parent node of the current node is the version submitted last time. That is, the version marked as "Change file1.

Why not change by banana? Different branches ." Change by banana is the latest code of the banana branch, which is different from that of the master branch.

Enter

Git status

See, it does not show that file2.txt has been modified and has not been submitted.

2. We need to return from git add

After careful consideration, we found that file2.txt was written incorrectly and needs to return to the status before git add.

Input:

Git reset-Q file2.txt

At this time, file2.txt is back before liberation. Use git status, and file2.txt is in the "change not staged for commit" status.

3. Return to the situations that have not been done

We are final, and the latest modified file2.txt is useless code, and we need to discard it.

Note that this operation cannot be recovered.

Git reset -- hard

This command cannot specify a specific file. Clears all current modifications and restores them to the last submitted version.

At this time, use gitk to view: git-003

You have completely replied to the "change file 1" version.

4. Directly reply to a version

Now we switch to the banana branch.

Git checkout banana

Then you can use gitk to check it out. We can see that our previous operations have no impact on the banana branch at all. Now we need to restore the banana branch to the initial state, but the current changed code still needs to be kept. We can see that the init version is the parent node of the current version. We can do this:

Git reset -- soft head ^

Then use gitk to look at: git-004

The latest version has changed to init version. All changes are in the "add" unsubmitted state.

5. Get the latest code.

Last. Delete both file1.txtand file2.txt. We need to obtain the latest code from the version library.

Simple:

Git checkout master

If you want the latest code for the banana branch, then:

Git checkout banana

 

In the above operations, we know how to view the version branches and how to reply to previous versions.

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.