N
N
nashivancom2022-01-13 00:07:09
git
nashivancom, 2022-01-13 00:07:09

How to undo the last two commits without losing local changes?

Prehistory: there
was commit_1, it was pushed to github
was commit_2, it was not pushed to github, because there was an error that the large file
was commit_3, in it I deleted this file via git rm dump.sql, I pushed to github, but it did not go through, because commit_2 still remembers this file

. How can I undo the last two commits without losing local changes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Kuznetsov, 2022-01-13
@nashivancom

Very simple. Here is one way.

git reset --soft HEAD~2 # отменить два последних коммита
# с опцией --soft состояние проекта в рабочем каталоге не изменится и ничего не потеряешь
# и все локальные изменения после первого коммита автоматически поместятся в индекс
git commit # создать коммит заново.

Next time, instead of creating an additional fix commit, just recreate the previous "bad" commit with git commit --amend

D
Dmitry Gordinskiy, 2022-01-13
@DmitriyGordinskiy

You can also simply merge these commits through rebase:

git rebase -i HEAD~2

And mark one of the commits on squash.
In this case, it's no better than reset --soft, just a more flexible tool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question