Deleting a git commit

Source: Internet
Author: User

When working with Git you'll find that sometimes commits need to be removed as they has introduced a bug or need to be Reworked.

If It is, the last commit of this is very straight forward. Simply Run:

git reset HEAD

This would pop off the latest commit but leave all of your changes to the files intact.

If you need-to-delete more than just, the last commit there is both methods you can use. The first is a using rebase this would allow you to remove one or more consecutive commits the other Ischerry-pick which allo WS you to remove non consecutive commits.

Example git log Number
Hash Commit Message Author
1 2c6a45b (HEAD) Adding public method to access protected method Tom
2 Ae45fab Updates to Database interface Contractor 1
3 77b9b82 Improving Database Interface Contractor 2
4 3c9093c Merged develop branch into master Tom
5 B3d92c5 Adding New Event CMS Module Paul
6 7feddbb Adding CMS class and files Tom
7 a809379 Adding Project to Git Tom
Using Rebase

Using The git log above we want to remove the following commits; 2 & 3 (Ae45fab & 77b9b82). As they is consecutive commits we can use rebase.

git rebase --onto <branch name>~<first commit number to remove> <branch name>~<first commit to be kept> <branch name>

E.g to remove commits 2 & 3 above

git rebase --onto repair~3 repair~1 repair

Using Cherry Pick

Step 1:find the commit before the commit want to removegit log

Step 2:checkout that commitgit checkout <commit hash>

Step 3:make A new branch using your current checkout commitgit checkout -b <new branch>

Step 4:now need to add the commit after the removed commitgit cherry-pick <commit hash>

Step 5:now Repeat step 4 for all and commits you want to keep.

Step 6:once All commits has been added to your new branch and has been commited. Check that everything are in the correct state and working as intended. Double Check everything has been commited:git status

Step 7:switch to your broken branchgit checkout <broken branch>

Step 8:now perform a hard reset on the broken branch to the commit prior to the one your want to removegit reset --hard <commit hash>

Step 9:merge Your fixed branch into this branchgit merge <branch name>

Step 10:push The merged changes back to Origin. Warning:this'll overwrite the remote repo!git push --force origin <branch name>

You can does the process without creating a new branch by replacing Step 2 & 3 with step 8 then no carry out Step 7 &am P 9.

Example

Say we want to remove commits 2 & 4 from the repo.

    1. git checkout b3d92c5Checkout the last usable commit.
    2. git checkout -b repairCreate a new branch to work on.
    3. git cherry-pick 77b9b82Run through commit 3.
    4. git cherry-pick 2c6a45bRun through commit 1.
    5. git checkout masterCheckout Master.
    6. git reset --hard b3d92c5Reset Master to the last usable commit.
    7. git merge repairMerge our new branch onto master.
    8. git push --hard origin masterPush Master to the remote repo.
Final Note

Git rebase & Cherrypick is dangerous but powerful solutions the should only being used as a last option and only being und  Ertaken by someone who knows what they is doing. Beware that both solutions could has adverse effects on other users who is working on the same repository/branch.

Finally remember to be careful and good luck!

Deleting a git commit

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.