How to revert initial git commit?

前端 未结 9 518
死守一世寂寞
死守一世寂寞 2020-11-30 16:14

I commit to a git repository for the first time; I then regret the commit and want to revert it. I try

# git reset --hard HEAD~1

I get thi

相关标签:
9条回答
  • 2020-11-30 16:43

    This question was linked from this blog post and an alternative solution was proposed for the newer versions of Git:

    git branch -m master old_master
    git checkout --orphan master
    git branch -D old_master
    

    This solution assumes that:

    1. You have only one commit on your master branch
    2. There is no branch called old_master so I'm free to use that name

    It will rename the existing branch to old_master and create a new, orphaned, branch master (like it is created for new repositories) after which you can freely delete old_master... or not. Up to you.

    Note: Moving or copying a git branch preserves its reflog (see this code) while deleting and then creating a new branch destroys it. Since you want to get back to the original state with no history you probably want to delete the branch, but others may want to consider this small note.

    0 讨论(0)
  • 2020-11-30 16:46

    I wonder why "amend" is not suggest and have been crossed out by @damkrat, as amend appears to me as just the right way to resolve the most efficiently the underlying problem of fixing the wrong commit since there is no purpose of having no initial commit. As some stressed out you should only modify "public" branch like master if no one has clone your repo...

    git add <your different stuff>
    git commit --amend --author="author name <author.name@email.com>"-m "new message"
    
    0 讨论(0)
  • 2020-11-30 16:46

    All what you have to do is to revert the commit.

    git revert {commit_id}'
    

    Then push it

    git push origin -f
    
    0 讨论(0)
  • 2020-11-30 16:48

    I will throw in what worked for me in the end. I needed to remove the initial commit on a repository as quarantined data had been misplaced, the commit had already been pushed.

    Make sure you are are currently on the right branch.

    git checkout master

    git update-ref -d HEAD

    git commit -m "Initial commit

    git push -u origin master

    This was able to resolve the problem.

    Important

    This was on an internal repository which was not publicly accessible, if your repository was publicly accessible please assume anything you need to revert has already been pulled down by someone else.

    0 讨论(0)
  • 2020-11-30 16:50

    git reset --hard make changes, then do

    git add -A
    git commit --amend --no-edit 
    

    or

    git add -A
    git commit --amend -m "commit_message"
    

    and then

    git push origin master --force
    

    --force will rewrite that commit you've reseted to in the first step.

    Don't do this, because you're about to go against the whole idea of VCS systems and git in particular. The only good method is to create new and delete unneeded branch. See git help branch for info.

    0 讨论(0)
  • 2020-11-30 16:51

    You just need to delete the branch you are on. You can't use git branch -D as this has a safety check against doing this. You can use update-ref to do this.

    git update-ref -d HEAD
    

    Do not use rm -rf .git or anything like this as this will completely wipe your entire repository including all other branches as well as the branch that you are trying to reset.

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