Simple talk about the rollback operation in git-related skills

Source: Internet
Author: User
Tags commit redis rollback

First, let's introduce the scene

First of all, be sure to think clearly about what you want to do.

Find Redis source code as an example to see where the branchgit branch

 3.0
 3.2
* unstable

Take the first 5 commits to see

git log--pretty=format: "%h-%an,%ar:%s"-5
E9d861e-antirez, hours ago:clear child data when opening the pipes.
E565632-antirez, hours ago:child-> Parent pipe for COW info transferring.
E1eccf9-antirez hours Ago:zmalloc:Make FP var non local to fix build.
945a2f9-antirez, Hours Ago:zmalloc:zmalloc_get_smap_bytes_by_field () modified to work for the any PID.
B13759e-antirez, Hours AGO:REDIS-CLI: "Allocator-stats"-> "Malloc-stats".

Temporarily switch to a commit

It is possible that you do not need to rollback the code, you just want to temporarily switch the code base to a previous version, see the implementation of that time, and then go back to write the unfinished tasks before. Like to see 945a2f9 this commit , do the operation

git checkout 945a2f9
Note:checking out ' 945a2f9 '.

You are are in ' detached head '. Can look around, make experimental
changes and commit them, and can discard any commits
Tate without impacting any branches by performing another.

If you are want to create a new branch to retain commits you create, your may does so
(now or later) by using-b with the Che Ckout command again. Example:

 git checkout-b <new-branch-name> head are now at

945a2f9 ... zmalloc:zmalloc_get_smap_bytes_by_ Field () modified to work for the any PID.

Re-executiongit branch

* (head detached at 945A2F9)
 3.0
 3.2
 unstable

You can see that you are on a temporary branch and if you want to develop new functionality on this branch as a base, you can create a new branch for development.

git checkout-b old-bottle 945a2f9

If you want to go back to the previous branch, just checkout go back.

Of course, if you commit have changed some code before you cut back, you may not be able to cut it so smoothly to the previous commit , you can

resetDiscard the modified code using the

stash->checkout->stash popbring the modified code back.

Generate a new branch to save the modified code

Delete an unpublished commit

If you are sure you want to delete a few commit , and the commit code has not yet been submitted to the remote version library.

Can do this

git reset--hard 945a2f9 head are now in

945a2f9 Zmalloc:zmalloc_get_smap_bytes_by_field () modified to work for any P Id.

If you look at git log again, you won't see the record before 945a2f9 commit .

Of course, if you want to save code that is not currently submitted, you can use stash as before

git stash
git reset--hard 945a2f9
git stash pop

Attention:

Reset--hard must be used with caution!!

This allows you to completely lose your local code (unless you have a backup)

Delete a published commit

Assuming that the code has been committed to the remote repository, you should use the revert command, which revert is actually submitted commit before a new roll back and forth commit . Or take 945a2f9 as an example, now want to let the code roll to the state of 945a2f9, then need to revert drop before thecommit

git revert e9d861e e565632 e1eccf9

Or you can do this, the revert top three of the head.commit

git revert head~3..head

You can then push the current head to the version library.

If you find that the revert operation was wrong, you can do it again revert to cancel the last one revert .

Summarize

The above is the entire content of this article, this article is simply a few of my personal practice. Hope that we can learn or work to bring certain help, if there is doubt you can message exchange.

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.