How to cancel a local git commit
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
add a comment |
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
3
It sounds like you're definitely not asking forgit 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 thatgit-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
add a comment |
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
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
git git-reset git-commit
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 forgit 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 thatgit-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
add a comment |
3
It sounds like you're definitely not asking forgit 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 thatgit-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
add a comment |
6 Answers
6
active
oldest
votes
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?
.
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 forHEAD^
.
– 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
add a comment |
Use --soft
instead of --hard
flag:
git reset --soft HEAD^
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
add a comment |
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.
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 arebase -i mybranchname
– inostia
Apr 30 '18 at 18:56
add a comment |
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.
add a comment |
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
1
I think it's the opposite.
– Ruff9
Sep 10 '18 at 13:57
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
.
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 forHEAD^
.
– 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
add a comment |
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?
.
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 forHEAD^
.
– 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
add a comment |
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?
.
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?
.
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 forHEAD^
.
– 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
add a comment |
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 forHEAD^
.
– 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
add a comment |
Use --soft
instead of --hard
flag:
git reset --soft HEAD^
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
add a comment |
Use --soft
instead of --hard
flag:
git reset --soft HEAD^
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
add a comment |
Use --soft
instead of --hard
flag:
git reset --soft HEAD^
Use --soft
instead of --hard
flag:
git reset --soft HEAD^
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
add a comment |
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
add a comment |
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.
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 arebase -i mybranchname
– inostia
Apr 30 '18 at 18:56
add a comment |
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.
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 arebase -i mybranchname
– inostia
Apr 30 '18 at 18:56
add a comment |
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.
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.
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 arebase -i mybranchname
– inostia
Apr 30 '18 at 18:56
add a comment |
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 arebase -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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered May 29 '18 at 10:22
Nesha ZoricNesha Zoric
1,5031420
1,5031420
add a comment |
add a comment |
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
1
I think it's the opposite.
– Ruff9
Sep 10 '18 at 13:57
add a comment |
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
1
I think it's the opposite.
– Ruff9
Sep 10 '18 at 13:57
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered May 14 '18 at 13:25
OmkarOmkar
113
113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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