Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to un-commit the last n commits?

Tags:

git

I'm working on a tree which has the following commits:-

aaaaaaa Implement function A
bbbbbbb Implement function B
ccccccc Implement function C

I would like to un-commit the first two , that is , to put these changes again into the staging area. I've learnt that git reset --soft HEAD^ would un-commit the last commit. But I would like to know the way to do it for many commits at once. I'm not sure if git reset --soft HEAD~10 would un-commit the 10th from the last commit or un-commit the last 10 commits.

like image 735
SloppyJoe Avatar asked Jun 04 '16 17:06

SloppyJoe


People also ask

How do I get rid of last few commits?

Removing the last commit To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Can I Uncommit in git?

Git uncommit command A hard reset will delete any new files that were added to the index, and undo any updates or changes made to any files that were part of the repository when the last commit occurred. This command typically fulfills the need for a git uncommit command.


1 Answers

First, be sure that none of the commits you're trying to change have been pushed to the public repository. (From the sounds of it, they have not yet been made public.)

You're on the right track with git reset --soft HEAD^.

HEAD^ is one commit before HEAD. HEAD~4 is four commits before HEAD - or, altogether, five commits back.

Found on GitHub blog

like image 97
Briana Swift Avatar answered Oct 07 '22 14:10

Briana Swift