Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

git - Deleting a commit in Between other commits

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...