Adnan
Adnan

Reputation: 8190

Remove last 2 commits without delete all data

I need to remove my last two commits but also need to save some data from these commits (one folder). And merge that into my 3rd commit. How can i do that ?

Upvotes: 1

Views: 62

Answers (3)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

You can reset back to two-commits ago using

git reset --mixed HEAD~2

This will reset the branch but keep all the changes those two commits. Then simply add and commit what you need to save

git add <important folder>
git commit -m'added important folder'

Upvotes: 2

pedrorijo91
pedrorijo91

Reputation: 7865

git rebase -i HEAD~2 allows you to edit previous 3 commits.

You probably want to apply the squash command on the interactive shell

Upvotes: 0

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36472

Simply stop thinking about it in the terms of "removing commits". I'd say:

Note down the current commit ID:

git log |head -n1 

go back two commits,

git checkout HEAD~2

then check out only your directory

git checkout <noted ID> -- yourdirectory/

and commit the result:

git commit -m "kept the future of yourdirectory/" yourdirectory

And if you really want to meddle with an existing commit, use

git rebase -i HEAD~10

to squash these two commits.

If you really want to (you don't have to) you can git prune the dangling commits later on.

Upvotes: 2

Related Questions