How to cancel a local git commit

前端 未结 6 2017
抹茶落季
抹茶落季 2021-01-29 17:20

My issue is I have changed a file eg: README, added a new line \'this for my testing line\' and saved the file, then I issued the following commands

 gi         


        
相关标签:
6条回答
  • 2021-01-29 17:28

    Use --soft instead of --hard flag:

    git reset --soft HEAD^
    
    0 讨论(0)
  • 2021-01-29 17:28

    If you're in the middle of a commit (i.e. in your editor already), you can cancel it by deleting all lines above the first #. That will abort the commit.

    So you can delete all lines so that the commit message is empty, then save the file:

    You'll then get a message that says Aborting commit due to empty commit message..

    EDIT:

    You can also delete all the lines and the result will be exactly the same.

    To delete all lines in vim (if that is your default editor), once you're in the editor, type gg to go to the first line, then dG to delete all lines. Finally, write and quit the file with wq and your commit will be aborted.

    0 讨论(0)
  • 2021-01-29 17:32

    The first thing you should do is to determine whether you want to keep the local changes before you delete the commit message.

    Use git log to show current commit messages, then find the commit_id before the commit that you want to delete, not the commit you want to delete.

    If you want to keep the locally changed files, and just delete commit message:

    git reset --soft commit_id

    If you want to delete all locally changed files and the commit message:

    git reset --hard commit_id

    That's the difference of soft and hard

    0 讨论(0)
  • 2021-01-29 17:37

    Use below command:

    $ git reset HEAD~1
    

    After this you also able to view files what revert back like below response.

    Unstaged changes after reset:
    M   application/config/config.php
    M   application/config/database.php
    
    0 讨论(0)
  • 2021-01-29 17:41

    You can tell Git what to do with your index (set of files that will become the next commit) and working directory when performing git reset by using one of the parameters:

    --soft: Only commits will be reseted, while Index and the working directory are not altered.

    --mixed: This will reset the index to match the HEAD, while working directory will not be touched. All the changes will stay in the working directory and appear as modified.

    --hard: It resets everything (commits, index, working directory) to match the HEAD.

    In your case, I would use git reset --soft to keep your modified changes in Index and working directory. Be sure to check this out for a more detailed explanation.

    0 讨论(0)
  • 2021-01-29 17:46

    Just use git reset without the --hard flag:

    git reset HEAD~1
    

    PS: On Unix based systems you can use HEAD^ which is equal to HEAD~1. On Windows HEAD^ will not work because ^ signals a line continuation. So your command prompt will just ask you More?.

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