Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a commit in Between other commits

Tags:

git

github

I have a local and its corresponding github repo. I have some commits as

A <- B <- C <- D

A being the latest.

This is situation on both the repos. I want to delete commit C as if it never happened.

So it should be like on git log

A <- B <- D

on both repos.

I tried git reset --soft <sha-commit-C> and am now stuck. I can not see A, B. Before doing any further steps i want to be fully sure that i know what i am doing.

git reflog gives me :

git reflog
73ea54d HEAD@{0}: reset: moving to 73ea54d8
a594699 HEAD@{1}: rebase -i (finish): returning to refs/heads/unique_ptr_release
a594699 HEAD@{2}: checkout: moving from unique_ptr_release to a594699fb6f7d85bc8
a594699 HEAD@{3}: checkout: moving from unique_ptr_release to unique_ptr_release
a594699 HEAD@{4}: commit (merge): Merge branch 'master' into unique_ptr_release

HEAD@{4} is what my Head was before reset --soft command.

How do i get out of this situation ?

like image 571
Ashish Negi Avatar asked Feb 15 '23 08:02

Ashish Negi


2 Answers

Git-rebase is what are you looking for.

in your case of commit A-B-C-D, and want to remove C, try:

git rebase -i HEAD~3

it will show the last 3 of your commits via editor (mine is vim), just delete (in vim: dd) the line of commit you want to remove, then save it (in vim: :wq).

Done, view the git log and you will see that the C commit is removed.

*don't forget to backup your code or .git folder.

like image 74
waskito Avatar answered Feb 17 '23 23:02

waskito


  1. Ensure you back up your .git folder.
  2. Ensure commit D , don’t rely upon the changes made in C.
  3. git rebase --onto D B~1 (as A is the latest commit)
like image 25
Shunya Avatar answered Feb 17 '23 23:02

Shunya