Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i delete the last 3 commits?

How do i delete the last 3 commits? there is probably many examples but I think i ma being very specific here with what I want.

So for example I have done a git add . and git commit -m "message" and a git push -u origin master for all these.

I have the following output from all my commits(with 21 being the most recent commit)

$git log --pretty=oneline [[33mab18ba3884244e99d99122e788062b2aaf6ba372[[m commit-21 [[33m9625c19e03a747457162d7c0274d75b154a0b926[[m commit-20 [[33m8668df12af9eee540cc29f9deb6d31d873bb8f24[[m commit-19 [[33m8315cd7962c902d39160fcf2fd018f249e7cf744[[m commit-18 [[33m3bcecc602a70fb60324777831d51c81b6f9ccaa7[[m commit-17 [[33m0485a39c37e72eabb4003e07a6b8cbae7a0d4e25[[m commit-16 [[33m9d3b04a7bcbb82be8b6b7882511f8133745c93b6[[m commit-15 [[33m22c259497571390fdd1461f0dd6b77244851984b[[m commit-14 [[33m39fb965fc245be55bb86491b5871dd2def08fdf4[[m commit-13 [[33m299f6bb77e922d707c9981a48d4d75f57b724719[[m commit-12 [[33me7285377d7afc08c7a5fb0ca44154d2de7e1d275[[m commit-11 [[33mb3360786ef13044d13b6e58c2239cce5595a1abf[[m commit-10 [[33m9af4c134e0d239d1c34fc6bb6087f0473c187fd5[[m commit-9 [[33md55ac3370f506a4bbf8a4690b9285e5de9c6a671[[m commit-8 [[33me50ef5f58b18d05c36343114804d8be180d26bcb[[m commit-7 [[33m962e4a8de7649e06df29f9058a600f8318caf023[[m commit-6 [[33m37f5363f62a3f973fe6e0d516e47b4324186d998[[m commit-5 [[33m0033e32339e4dc0cce8bd208d43b18a4e9ab43d9[[m commit-4 [[33m345239c740a408826f1df0dc5592d5d6b355f019[[m commit-3 [[33m8c85bf2592f52302ff389a5b6af4127fbe04c73b[[m commit-2 [[33ma4077b6c2b6a491af72ae3afc3b5c6260090b605[[m commit-1 

What I want to do is effectively delete the last 3 commits and end up with this:(So it was like git commit 19, 20 and 21 never happened)

$git log --pretty=oneline [[33m8315cd7962c902d39160fcf2fd018f249e7cf744[[m commit-18 [[33m3bcecc602a70fb60324777831d51c81b6f9ccaa7[[m commit-17 [[33m0485a39c37e72eabb4003e07a6b8cbae7a0d4e25[[m commit-16 [[33m9d3b04a7bcbb82be8b6b7882511f8133745c93b6[[m commit-15 [[33m22c259497571390fdd1461f0dd6b77244851984b[[m commit-14 [[33m39fb965fc245be55bb86491b5871dd2def08fdf4[[m commit-13 [[33m299f6bb77e922d707c9981a48d4d75f57b724719[[m commit-12 [[33me7285377d7afc08c7a5fb0ca44154d2de7e1d275[[m commit-11 [[33mb3360786ef13044d13b6e58c2239cce5595a1abf[[m commit-10 [[33m9af4c134e0d239d1c34fc6bb6087f0473c187fd5[[m commit-9 [[33md55ac3370f506a4bbf8a4690b9285e5de9c6a671[[m commit-8 [[33me50ef5f58b18d05c36343114804d8be180d26bcb[[m commit-7 [[33m962e4a8de7649e06df29f9058a600f8318caf023[[m commit-6 [[33m37f5363f62a3f973fe6e0d516e47b4324186d998[[m commit-5 [[33m0033e32339e4dc0cce8bd208d43b18a4e9ab43d9[[m commit-4 [[33m345239c740a408826f1df0dc5592d5d6b355f019[[m commit-3 [[33m8c85bf2592f52302ff389a5b6af4127fbe04c73b[[m commit-2 [[33ma4077b6c2b6a491af72ae3afc3b5c6260090b605[[m commit-1 

Note: I have looked into this and can't quiet get what I want. I have tried git revert(this is advise if I have published which I presume is the same as git push -u origin master) git rebase git reset
I am not concerned with my remote at this stage I jsut want to get my local repo put back a couple of repos.

like image 246
HattrickNZ Avatar asked Oct 24 '16 22:10

HattrickNZ


People also ask

Can I delete previous commits?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

What is the last 3 commit command in git?

The most basic and powerful tool to do this is the git log command. By default, with no arguments, git log lists the commits made in that repository in reverse chronological order; that is, the most recent commits show up first.

How do I undo the last few commits?

You can do this using git revert command. (arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).


1 Answers

use can use git reset for that

git reset --hard HEAD~3  git push --force origin master 

where HEAD~3 means go back 3 commits

You can also use the commit number instead of 3 commits, so that you are sure where you are going back in time, like

git reset --hard 8315cd7962c902d39160fcf2fd018f249e7cf744 

EDIT:

When rewriting history, prefer using

git push origin +master 

To be safe if you are ever pushing more than one branch. You can read more at Git Force push syntax, "-f" versus "+branch"

(kudor to Josef Kufner comment pointing it out)

EDIT 2:

I just noticed that your log is printing colors as codes, So

git reset -- hard 33m8315cd7962c902d39160fcf2fd018f249e7cf744  

will not work, [[33m and [[m are color codes! So your commit number is actually 8315cd7962c902d39160fcf2fd018f249e7cf744

If you do

git reset --hard 8315cd7962c902d39160fcf2fd018f249e7cf744 

it should work as well.

(Fixed the commit code on previous examples as well)

like image 161
Felipe Sabino Avatar answered Oct 14 '22 03:10

Felipe Sabino