Delete commits from a branch in Git

后端 未结 30 2002
醉话见心
醉话见心 2020-11-21 07:17

I would like to know how to delete a commit.

By delete, I mean it is as if I didn\'t make that commit, and when I do a push in the future, my changes wi

相关标签:
30条回答
  • 2020-11-21 07:55

    git reset --hard

    git push origin HEAD --force

    If one or more of the commits is tagged, delete the tag(s) first. Otherwise the tagged commit is not removed.

    0 讨论(0)
  • 2020-11-21 07:57

    If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:

    git reset HEAD~1
    

    This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.

    If you want to uncommit N commits, but keep the code changes in your working directory:

    git reset HEAD~N
    

    If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.

    git reset --hard HEAD~1
    

    Likewise, if you want to discard the last N commits, and do not want to keep the code changes:

    git reset --hard HEAD~N
    
    0 讨论(0)
  • 2020-11-21 07:57
    // display git commit log    
    $ git log --pretty=oneline --abbrev-commit
    
    // show last two commit and open in your default editor
    // then delete second commit line and save it
    $ git rebase -i HEAD~2
    

    Reference: How to delete a commit in git, local and remote

    0 讨论(0)
  • 2020-11-21 07:59

    Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.

    Assuming you are sitting on that commit, then this command will wack it...

    git reset --hard HEAD~1
    

    The HEAD~1 means the commit before head.

    Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

    git reset --hard <sha1-commit-id>
    

    If you already pushed it, you will need to do a force push to get rid of it...

    git push origin HEAD --force
    

    However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

    If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.


    FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.


    Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

    0 讨论(0)
  • 2020-11-21 07:59

    Removing an entire commit

    git rebase -p --onto SHA^ SHA
    

    Obviously replace "SHA" with the reference you want to get rid of. The "^" in that command is literal.

    http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep

    0 讨论(0)
  • 2020-11-21 07:59

    As you can see on above image i want to delete revert"test change 2" commit(SHA1 ID: 015b5220c50e3dfbb1063f23789d92ae1d3481a2(you can get SHA1 ID by using gitk command in git bash)).

    For that i can use(all below command work on local only. you need to push after delete):

    1. git reset --hard 515b5220c50e3dfbb1063f23789d92ae1d3481a2 //it back-up you to that commit (SHA1 ID of test change 4 commit is 515b5220c50e3dfbb1063f23789d92ae1d3481a2)
    2. git reset --hard HEAD~1 // it back-up you before one commit.
    3. git reset --hard HEAD^ // To remove the last commit from git

    after delete:

    0 讨论(0)
提交回复
热议问题