ilikechocolate
ilikechocolate

Reputation: 209

keep most recent commit and remove previous 3 comit?

For example I do a git log: showing that I have 5 commit:

commit 1

commit 2

commit 3

commit 4

commit 5.

Now I want to keep commit 1, but remove commit 2, 3, 4. So after do it, my git log should looks like:

commit 1,

commit 5.

What should I do? Thank you.

Upvotes: 0

Views: 45

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 22057

One way to proceed is to interactively rebase (doc).

The other would be to reset to 1 and cherry-pick 5 :

git reset --hard <hashOfCommit_1>
git cherry-pick <hashOfCommit_5>

(you can find the hashes with git log --oneline -5 for example)

However, note that it rewrites history of your branch. If the commits after 1 have already been pushed to a remote repo, you'll have to

git push --force origin HEAD

to update the old reference.

Upvotes: 2

Related Questions