With this post you'll learn how to rewrite the whole history of a branch. In other words: undoing all commits but keeping their changes to be committed again.

Let's say you have four commits:

f7f3f6d Create FileA

310154e Update FileA with x

a5f4a0d Create FileB

421265e Update files A and B with Y and Z

But you want to rewrite the whole history to have just two:

Create file A with X and Y

Create file B with Z

Sure you can manipulate the tree of changes with the git rebase -i command (reference), but there is a more straightforward way of rewriting a branch: with git reset --soft .

Let's see how to use this command.

Doing a Soft Reset

Assuming you're branch was created from a branch named master, just run (running a git fetch origin first is recommended):

$ git reset --soft origin/master

Now you're going to have all of your commits undone, but their changes will be kept as added to your stash. You can also use HEAD~4 instead of origin/master to undo the last four commits, for example.

So, running a git status should return something like

$ git status On branch your_branch_name Your branch is ahead of ‘origin/your_branch_name’ by X commits. (use “git push” to publish your local commits) Changes to be committed: (use “git reset HEAD <file>…” to unstage) modified: FileA

modified: FileB

Again, you can see that both FileA and FileB are added, but not commited. You can undo this by running git reset HEAD . (your changes will remain, don't worry).

Now you're free to rewrite your commit history as you want!