tenshi
tenshi

Reputation: 618

git - delete a previous commit but keep changes from last commit

How to delete a previous commit but keep changes from last commit in git?

First commit:

nano first //writes "this is first" and save
git add first
git commit -m "first commit"

Second commit:

nano second //writes "this is second" and save
git add second
git commit -m "second commit"

Git log:

commit 014179d534ed3185dba994d4df3713334893a2c7 (HEAD -> master)
Author: Email <[email protected]>
Date:   Fri Mar 18 20:29:20 2022 +0800

    second commit

commit 22b9f9a0a60b5b52c2b8d3e1981b1db524c9d2fd
Author: Email <[email protected]>
Date:   Fri Mar 18 20:22:46 2022 +0800

    first commit

I want to delete the first commit but keep the files. How can I do that?

Upvotes: 0

Views: 173

Answers (1)

eftshift0
eftshift0

Reputation: 30156

Like, you want to convert that into a single commit where you keep both files?

There are many ways... the simplest (least effort):

git reset --soft HEAD~ # set branch _pointer_ in previous revision
# files are not changed. second will be in index (check with git status)
git commit --amend -m "this is the squashed version"

And you are done.

You could also do:

git rebase -i --root
# you will get a list of two revisions
# set the second to be s (or squash)
# save and exit
# rebase starts
# git will show you a commit windows telling you about the squash
# and the comment from both revision
# set it to whatever you want
# save and exit

And you are done.

Upvotes: 1

Related Questions