A merge of "about Git" history submissions

Source: Internet
Author: User
Tags commit using git
if, at present, we are in the demand branch feature-test, made several mentions, git log view commit information as follows:

Each commit is independent, but the last three commits are related, adding the contents of the Read.txt file, in this article, as an example to explain how to modify, merge, fallback commit. View git history

If you want to view git history you can use git log or git reflog command: git log: View the current branch's commit history, excluding commits such as deleted or merged; Git reflog: View all the operation history of the current branch, such as history commit record, Undo, Detailed historical records such as merge submissions; git rebase-i

The instructions that need to be used are git rebase-i commithash,commithash is Commitid, which is the ID of the previous commit node of the commit that needs to be merged, and for the purposes of this example, the fourth commit df11bf944, So execute the following command:

Git rebase-i df11bf944

The command line terminal will output the following:

By far and near the list of three commits we expect to handle, the previous pick represents the default use of the commit commit, we can now press I into the edit mode, modify the field value, the value can be described in the figure, often used as follows: pick: Shorthand p, enable the commit; Reword: abbreviated R , use this commit, but modify the submission information, modify the following can continue to edit the submission information; edit: Shorthand E, use commit, stop merging the commit; Squash: shorthand s, use the commit, and merge the commit into the previous commit; Drop: Abbreviated D, remove the commit; Modify the Commit information

We now attempt to modify the most recent commit commit information and modify the previous pick to reword:

After editing, press ESC to exit edit mode, then enter: Wq, save the current edit, will output the following:

We can start editing the commit information we need to change, press I to enter edit mode, and modify the submission information as:

Feature (Read.txt) Add read.txt third line (reword modify commit message)

After saving exit, there will be a successful prompt for modification:

Merge History Submission

The previous modification of commit succeeds if you expect to merge multiple commits into one commit, making the entire commit history cleaner and how to handle it.

Execute the following instruction, df11bf944 is the commitid of the previous commit node that requires a merge submission:

Git rebase-i df11bf944

Then modify the pick value to squash:

Save exit, will enter the final merge commit commit information edit state, here will be listed merge commit all the message, we can manipulate:

We can keep three submissions at the same time, or we can modify them, here we only keep the first submitted information, and then save the exit, when we re-use git log to view the history of the submission of information, we will find that only a combined commit and previous uncommitted commits:

git rebase-i head~{num}

The previously mentioned Git rebase-i commithash directive can merge commit history, but can also be replaced by a shortcut, such as when it is necessary to merge the last two commits:

Git rebase-i head~2

The same effect:

Subsequent modifications, merges, and fallback operations are consistent. Revoke a submission

When a requirement finds a change, we find that there is no need for a history submission, what to do, how to discard the modification submission. There are two cases: the revocation of a submission in the middle of history, the revocation of a recent submission, and the revocation of intermediate submissions .

When a submission that needs to be discarded is merged and we want to discard the submission, we need to first view the submitted information using the execution instructions:

Git reflog

This command outputs a detailed history of operations, including submission, operation, modification, etc.:

We find the commits that need to be revoked, such as the most recent second commit, commit Commitid to Dcbdde2, index head@{19}:

Dcbdde2 head@{19}: Commit:feature (read.txt): Add read.txt second row
git revert

Execute the following command to revoke the commit:

git revert head@{19}

The above head@{19} directive is also equivalent to:

git revert dcbdde2

git revert to undo a commit and create a new commit. This is a safe method because it does not rewrite the commit history. It creates a new commit to undo the specified changes and then adds the new commit to the project.

When you revoke a commit, if multiple commits modify the same file, a conflict may occur, and after the conflict is processed, staging:

git Add.

Then proceed to the revert operation:

git revert--continue

Then look at the commit history and discover one more record:

At this point, the content of the last second commit (that is, the second line of the Read.txt file) has been revoked. Undo Recent Submissions

If the expected revocation of the commit is recently independent, and there is no merge to undo the previous section git revert the newly generated commit as an example:

5a7b985 Revert "Feature (read.txt): Add read.txt second line"
Df11bf9 Commit: "Feat (rn-publish-up): React native release, Introduction to Hot Update principle"
git reset

You only need to use the git reset command:

git Reset Commithash/head~{num}

Commithash is the commitid of the last commit expected to revoke the submission, which is equivalent to specifying the desired revocation of the last few commits, and the NUM value equals the expected number of undo commits.

Specifically submitted Commitid can use git log or git reflog command to find, delete execution instructions:

git reset head~1

Equivalent to, DF11BF9 is the last commit commitid that needs to undo the commit commit:

git reset df11bf9

Finally, the submission becomes uncommitted, using git checkout. Instructions to undo the change:

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.