How to cancel a local git commit












558














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



 git status

# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
no changes added to commit (use "git add" and/or "git commit -a")


git add README

git commit -a -m 'To add new line to readme'


I didn't push the code to github, Now I want to cancel this commit.



For this I used



   git reset --hard HEAD~1


But I lost the newly added line 'this for my testing line' from the README file.
This should not happen. I need the content to be there. Is there a way to retain the content and cancel my local commit?










share|improve this question




















  • 3




    It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
    – Cascabel
    Jan 31 '11 at 16:11






  • 1




    NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
    – GKFX
    Feb 24 '14 at 17:08
















558














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



 git status

# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
no changes added to commit (use "git add" and/or "git commit -a")


git add README

git commit -a -m 'To add new line to readme'


I didn't push the code to github, Now I want to cancel this commit.



For this I used



   git reset --hard HEAD~1


But I lost the newly added line 'this for my testing line' from the README file.
This should not happen. I need the content to be there. Is there a way to retain the content and cancel my local commit?










share|improve this question




















  • 3




    It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
    – Cascabel
    Jan 31 '11 at 16:11






  • 1




    NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
    – GKFX
    Feb 24 '14 at 17:08














558












558








558


178





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



 git status

# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
no changes added to commit (use "git add" and/or "git commit -a")


git add README

git commit -a -m 'To add new line to readme'


I didn't push the code to github, Now I want to cancel this commit.



For this I used



   git reset --hard HEAD~1


But I lost the newly added line 'this for my testing line' from the README file.
This should not happen. I need the content to be there. Is there a way to retain the content and cancel my local commit?










share|improve this question















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



 git status

# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
no changes added to commit (use "git add" and/or "git commit -a")


git add README

git commit -a -m 'To add new line to readme'


I didn't push the code to github, Now I want to cancel this commit.



For this I used



   git reset --hard HEAD~1


But I lost the newly added line 'this for my testing line' from the README file.
This should not happen. I need the content to be there. Is there a way to retain the content and cancel my local commit?







git git-reset git-commit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '11 at 11:56







Amal Kumar S

















asked Jan 31 '11 at 12:14









Amal Kumar SAmal Kumar S

6,415164477




6,415164477








  • 3




    It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
    – Cascabel
    Jan 31 '11 at 16:11






  • 1




    NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
    – GKFX
    Feb 24 '14 at 17:08














  • 3




    It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
    – Cascabel
    Jan 31 '11 at 16:11






  • 1




    NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
    – GKFX
    Feb 24 '14 at 17:08








3




3




It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
– Cascabel
Jan 31 '11 at 16:11




It sounds like you're definitely not asking for git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".
– Cascabel
Jan 31 '11 at 16:11




1




1




NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
– GKFX
Feb 24 '14 at 17:08




NB: Might be worth mentioning that git-commit can abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.
– GKFX
Feb 24 '14 at 17:08












6 Answers
6






active

oldest

votes


















1076














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?.






share|improve this answer



















  • 11




    By the way, this is called --mixed in the manual.
    – Josh Lee
    Jan 31 '11 at 17:58






  • 7




    Newer versions of Git even allow @^ as a shorthand for HEAD^.
    – Koraktor
    Sep 15 '14 at 20:59










  • I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
    – FRR
    Feb 5 '15 at 14:05










  • @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
    – Koraktor
    Feb 6 '15 at 9:13










  • I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
    – RYO ENG Lian Hu
    Apr 28 '16 at 16:04



















141














Use --soft instead of --hard flag:



git reset --soft HEAD^





share|improve this answer



















  • 14




    can you explain the difference between the 2 flags?
    – John Giotta
    May 5 '15 at 1:09






  • 1




    If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
    – David Cornelson
    Dec 3 '15 at 18:30






  • 38




    @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
    – fider
    Apr 25 '16 at 15:52












  • @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
    – Babu
    Dec 27 '17 at 19:13



















24














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:



It should look like this.



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.






share|improve this answer



















  • 3




    This is exactly what I was looking for! This answer needs more upvotes :)
    – jdunk
    Jun 11 '17 at 20:09










  • FYI, this also works if you are trying to abort a rebase -i mybranchname
    – inostia
    Apr 30 '18 at 18:56



















5














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.






share|improve this answer





























    2














    The first thing you should do is to justify 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 you want to delete, no just the one you want to delete.



    If you want to keep the locally changed files, 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






    share|improve this answer



















    • 1




      I think it's the opposite.
      – Ruff9
      Sep 10 '18 at 13:57



















    1














    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






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4850717%2fhow-to-cancel-a-local-git-commit%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1076














      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?.






      share|improve this answer



















      • 11




        By the way, this is called --mixed in the manual.
        – Josh Lee
        Jan 31 '11 at 17:58






      • 7




        Newer versions of Git even allow @^ as a shorthand for HEAD^.
        – Koraktor
        Sep 15 '14 at 20:59










      • I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
        – FRR
        Feb 5 '15 at 14:05










      • @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
        – Koraktor
        Feb 6 '15 at 9:13










      • I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
        – RYO ENG Lian Hu
        Apr 28 '16 at 16:04
















      1076














      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?.






      share|improve this answer



















      • 11




        By the way, this is called --mixed in the manual.
        – Josh Lee
        Jan 31 '11 at 17:58






      • 7




        Newer versions of Git even allow @^ as a shorthand for HEAD^.
        – Koraktor
        Sep 15 '14 at 20:59










      • I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
        – FRR
        Feb 5 '15 at 14:05










      • @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
        – Koraktor
        Feb 6 '15 at 9:13










      • I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
        – RYO ENG Lian Hu
        Apr 28 '16 at 16:04














      1076












      1076








      1076






      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?.






      share|improve this answer














      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?.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 10 '15 at 18:42









      awilkinson

      922922




      922922










      answered Jan 31 '11 at 12:17









      KoraktorKoraktor

      25k65281




      25k65281








      • 11




        By the way, this is called --mixed in the manual.
        – Josh Lee
        Jan 31 '11 at 17:58






      • 7




        Newer versions of Git even allow @^ as a shorthand for HEAD^.
        – Koraktor
        Sep 15 '14 at 20:59










      • I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
        – FRR
        Feb 5 '15 at 14:05










      • @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
        – Koraktor
        Feb 6 '15 at 9:13










      • I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
        – RYO ENG Lian Hu
        Apr 28 '16 at 16:04














      • 11




        By the way, this is called --mixed in the manual.
        – Josh Lee
        Jan 31 '11 at 17:58






      • 7




        Newer versions of Git even allow @^ as a shorthand for HEAD^.
        – Koraktor
        Sep 15 '14 at 20:59










      • I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
        – FRR
        Feb 5 '15 at 14:05










      • @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
        – Koraktor
        Feb 6 '15 at 9:13










      • I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
        – RYO ENG Lian Hu
        Apr 28 '16 at 16:04








      11




      11




      By the way, this is called --mixed in the manual.
      – Josh Lee
      Jan 31 '11 at 17:58




      By the way, this is called --mixed in the manual.
      – Josh Lee
      Jan 31 '11 at 17:58




      7




      7




      Newer versions of Git even allow @^ as a shorthand for HEAD^.
      – Koraktor
      Sep 15 '14 at 20:59




      Newer versions of Git even allow @^ as a shorthand for HEAD^.
      – Koraktor
      Sep 15 '14 at 20:59












      I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
      – FRR
      Feb 5 '15 at 14:05




      I don't know what this did, but a lot of files appeard on my change list, files I didn't touch
      – FRR
      Feb 5 '15 at 14:05












      @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
      – Koraktor
      Feb 6 '15 at 9:13




      @feresr If you really did not touch those files in the last commit or in the working tree this is caused by other inconsistencies in your working tree, e.g. you're on Windows and file endings do not match.
      – Koraktor
      Feb 6 '15 at 9:13












      I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
      – RYO ENG Lian Hu
      Apr 28 '16 at 16:04




      I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^
      – RYO ENG Lian Hu
      Apr 28 '16 at 16:04













      141














      Use --soft instead of --hard flag:



      git reset --soft HEAD^





      share|improve this answer



















      • 14




        can you explain the difference between the 2 flags?
        – John Giotta
        May 5 '15 at 1:09






      • 1




        If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
        – David Cornelson
        Dec 3 '15 at 18:30






      • 38




        @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
        – fider
        Apr 25 '16 at 15:52












      • @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
        – Babu
        Dec 27 '17 at 19:13
















      141














      Use --soft instead of --hard flag:



      git reset --soft HEAD^





      share|improve this answer



















      • 14




        can you explain the difference between the 2 flags?
        – John Giotta
        May 5 '15 at 1:09






      • 1




        If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
        – David Cornelson
        Dec 3 '15 at 18:30






      • 38




        @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
        – fider
        Apr 25 '16 at 15:52












      • @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
        – Babu
        Dec 27 '17 at 19:13














      141












      141








      141






      Use --soft instead of --hard flag:



      git reset --soft HEAD^





      share|improve this answer














      Use --soft instead of --hard flag:



      git reset --soft HEAD^






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 10 '12 at 9:24









      sloth

      73.3k14127168




      73.3k14127168










      answered Aug 10 '12 at 8:02









      TpEDTpED

      1,411182




      1,411182








      • 14




        can you explain the difference between the 2 flags?
        – John Giotta
        May 5 '15 at 1:09






      • 1




        If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
        – David Cornelson
        Dec 3 '15 at 18:30






      • 38




        @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
        – fider
        Apr 25 '16 at 15:52












      • @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
        – Babu
        Dec 27 '17 at 19:13














      • 14




        can you explain the difference between the 2 flags?
        – John Giotta
        May 5 '15 at 1:09






      • 1




        If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
        – David Cornelson
        Dec 3 '15 at 18:30






      • 38




        @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
        – fider
        Apr 25 '16 at 15:52












      • @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
        – Babu
        Dec 27 '17 at 19:13








      14




      14




      can you explain the difference between the 2 flags?
      – John Giotta
      May 5 '15 at 1:09




      can you explain the difference between the 2 flags?
      – John Giotta
      May 5 '15 at 1:09




      1




      1




      If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
      – David Cornelson
      Dec 3 '15 at 18:30




      If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).
      – David Cornelson
      Dec 3 '15 at 18:30




      38




      38




      @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
      – fider
      Apr 25 '16 at 15:52






      @JohnGiotta - git reset --soft HEAD^ will remove last local (unpushed) commit but will keep changes you have done
      – fider
      Apr 25 '16 at 15:52














      @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
      – Babu
      Dec 27 '17 at 19:13




      @fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.
      – Babu
      Dec 27 '17 at 19:13











      24














      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:



      It should look like this.



      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.






      share|improve this answer



















      • 3




        This is exactly what I was looking for! This answer needs more upvotes :)
        – jdunk
        Jun 11 '17 at 20:09










      • FYI, this also works if you are trying to abort a rebase -i mybranchname
        – inostia
        Apr 30 '18 at 18:56
















      24














      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:



      It should look like this.



      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.






      share|improve this answer



















      • 3




        This is exactly what I was looking for! This answer needs more upvotes :)
        – jdunk
        Jun 11 '17 at 20:09










      • FYI, this also works if you are trying to abort a rebase -i mybranchname
        – inostia
        Apr 30 '18 at 18:56














      24












      24








      24






      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:



      It should look like this.



      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.






      share|improve this answer














      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:



      It should look like this.



      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 23 '18 at 9:53

























      answered Mar 9 '17 at 21:09









      inostiainostia

      2,83611421




      2,83611421








      • 3




        This is exactly what I was looking for! This answer needs more upvotes :)
        – jdunk
        Jun 11 '17 at 20:09










      • FYI, this also works if you are trying to abort a rebase -i mybranchname
        – inostia
        Apr 30 '18 at 18:56














      • 3




        This is exactly what I was looking for! This answer needs more upvotes :)
        – jdunk
        Jun 11 '17 at 20:09










      • FYI, this also works if you are trying to abort a rebase -i mybranchname
        – inostia
        Apr 30 '18 at 18:56








      3




      3




      This is exactly what I was looking for! This answer needs more upvotes :)
      – jdunk
      Jun 11 '17 at 20:09




      This is exactly what I was looking for! This answer needs more upvotes :)
      – jdunk
      Jun 11 '17 at 20:09












      FYI, this also works if you are trying to abort a rebase -i mybranchname
      – inostia
      Apr 30 '18 at 18:56




      FYI, this also works if you are trying to abort a rebase -i mybranchname
      – inostia
      Apr 30 '18 at 18:56











      5














      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.






      share|improve this answer


























        5














        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.






        share|improve this answer
























          5












          5








          5






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 29 '18 at 10:22









          Nesha ZoricNesha Zoric

          1,5031420




          1,5031420























              2














              The first thing you should do is to justify 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 you want to delete, no just the one you want to delete.



              If you want to keep the locally changed files, 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






              share|improve this answer



















              • 1




                I think it's the opposite.
                – Ruff9
                Sep 10 '18 at 13:57
















              2














              The first thing you should do is to justify 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 you want to delete, no just the one you want to delete.



              If you want to keep the locally changed files, 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






              share|improve this answer



















              • 1




                I think it's the opposite.
                – Ruff9
                Sep 10 '18 at 13:57














              2












              2








              2






              The first thing you should do is to justify 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 you want to delete, no just the one you want to delete.



              If you want to keep the locally changed files, 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






              share|improve this answer














              The first thing you should do is to justify 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 you want to delete, no just the one you want to delete.



              If you want to keep the locally changed files, 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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 12 '18 at 3:04

























              answered Sep 2 '18 at 3:43









              mistdonmistdon

              48945




              48945








              • 1




                I think it's the opposite.
                – Ruff9
                Sep 10 '18 at 13:57














              • 1




                I think it's the opposite.
                – Ruff9
                Sep 10 '18 at 13:57








              1




              1




              I think it's the opposite.
              – Ruff9
              Sep 10 '18 at 13:57




              I think it's the opposite.
              – Ruff9
              Sep 10 '18 at 13:57











              1














              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






              share|improve this answer


























                1














                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






                share|improve this answer
























                  1












                  1








                  1






                  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






                  share|improve this answer












                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 14 '18 at 13:25









                  OmkarOmkar

                  113




                  113






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4850717%2fhow-to-cancel-a-local-git-commit%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...