search

Home  >  Q&A  >  body text

git - If after committing, you find that something is not perfect and you want to improve it before committing again, how can you cancel the previous commit?

Or, you can commit later and merge with the previous one.

phpcn_u1582phpcn_u15822794 days ago636

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-05-02 09:27:39

    If you want to change commit message ,用 git commit -amend.

    If you want to add some content, don’t touch the last commit(就叫 commit-1 吧),接着修改,然后提交一个新的 commit (commit-2)。然后 git rebase -i HEAD~~, the following content will appear:

    pick commit-1-hash commit-1-message
    pick commit-2-hash commit-2-message

    Save the second pick 改成 squash and exit, and the following will appear:

    # first commit message:
    commit-1-message
    
    # second commit message:
    commit-2-message

    Delete these things and change them to what you want commit messageSave and exit.

    Then you can git log 查看一下,两个 commit 已经合并成为新的 commit.

    You can refer to:
    5. Use rebase -i to merge and submit

    ===

    BTW, if you can only commit the last time commit 已经推到远程分支了,而且该分支有人跟你合作,那么最好还是不要修改了。因为你修改之后,就跟远程分支不同步了,所以需要 git push -f origin your-local-branch, this may cause other people's branches to be out of sync with the remote.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-02 09:27:39

    git commit —amend

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-02 09:27:39

    Can be used to undo the last submission

    git reset HEAD^
    

    reply
    0
  • Cancelreply