How to cancel a local git commit?
Asked Answered
E

8

918

My issue is I have changed a file e.g.: 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?

Ephraim answered 31/1, 2011 at 12:14 Comment(2)
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".Restoration
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.Tori
C
1699

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

Commutable answered 31/1, 2011 at 12:17 Comment(7)
By the way, this is called --mixed in the manual.Roaring
Newer versions of Git even allow @^ as a shorthand for HEAD^.Commutable
I don't know what this did, but a lot of files appeard on my change list, files I didn't touchPlenish
@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.Commutable
I’ve 10 commits pending there due to size issue... and your solution is the isolated storm during dry summer... ^_^Irena
yes! this line avoided a mayor disaster... good one, a keeper!Topsoil
is it possible to reset all commit on one hand?Newtonnext
P
279

Use --soft instead of --hard flag.

Unix:

git reset --soft HEAD^

Windows:

git reset --soft HEAD~

It will remove the last local (unpushed) commit, but will keep changes you have done.

Preachment answered 10/8, 2012 at 8:2 Comment(3)
If you open Package Manager Console and run this "git reset --soft HEAD^", it does what you want (and what I needed).Mosa
@fider Saviour! Not sure if the accepted answer still works, but this should be the exact answer the OP needed.Slicker
git reset --soft HEAD~ on windowsHulky
C
63

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

Cherianne answered 9/3, 2017 at 21:9 Comment(2)
This is exactly what I was looking for! This answer needs more upvotes :)Provencal
FYI, this also works if you are trying to abort a rebase -i mybranchnameCherianne
A
35

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

Armstrong answered 2/9, 2018 at 3:43 Comment(1)
I think it's the opposite.Annmarieannnora
J
12

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.

Jarid answered 29/5, 2018 at 10:22 Comment(0)
L
8

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
Laris answered 14/5, 2018 at 13:25 Comment(0)
A
3

As @Amal Kumar say. Just use git commmand:

git reset HEAD~1
Astrid answered 21/6, 2022 at 9:46 Comment(1)
This will delete your local master branch and all your changes will be lost. It's better not to delete the branch. You can use git reset HEAD~1 this will cancel your last git commit that was not pushed to remote and the changes won't be lost. You can also go through the different answers posted here for different use cases.Ephraim
F
1

Here I ran into another situation.

I have 2 branchs, one is 'dev', one is 'new', I fixed one bug on dev, then on new:

git rebase origin/dev
git push -f

The new branch now is running on server, while I update new on server, git add some local commits for me:

git pull

# On branch new
# Your branch and 'origin/new' have diverged,
# and have 7 and 10 different commits each, respectively.
#  (use "git pull" to merge the remote branch into yours)

# nothing to commit, working tree clean

'git pull' not work...

'git reset HEAD~1' not work...

I use

git reset origin/new

to get work done.

Freberg answered 22/2, 2023 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.