Home > database >  Removing all local commits while preserving all the work
Removing all local commits while preserving all the work

Time:02-03

My remote repository looks like -

A->B->C

My local repository looks like -

A->B->C->D->E

The commits D and E were made by me. I also have some other local changes which have not been committed. I now want to remove commits D and E, but none of my work should get affected. When I'm done with everything, I want my local repository to look like -

A->B->C->X

What should I do from here?

CodePudding user response:

Use interactive rebase

git stash
git rebase -i HEAD~2

In the opened editor set:

pick <COMMIT D>
squash <COMMIT E>

Than pop your stash

git stash pop

This will make your project like this A->B->C->X(D E) with uncommited changes.

CodePudding user response:

You can use git stash to stash your local uncommitted work and then after you remove commits D and E using git reset you can perform git stash pop to bring back your uncommitted work from the stash

OR

If you want those commits D and E as a backup for later, you can create a new branch; say new_branch by checking out from the current branch

git checkout -b new_branch.

Now you have A->B->C->D->E in the new_branch

You can now commit your current work on top of this as commit X. So now it becomes A->B->C->D->E->X

Now you can checkout back to your previous branch, remove commits D and E using git reset and then cherry pick commit X from new_branch.

  •  Tags:  
  • Related