Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.4k points)

I flubbed up my history and want to do some changes to it. Problem is, I have a commit with two unrelated changes, and this commit is surrounded by some other changes in my local (non-pushed) history.

I want to split up this commit before I push it out, but most of the guides I'm seeing have to do with splitting up your most recent commit, or uncommitted local changes. Is it feasible to do this to a commit that is buried in history a bit, without having to "re-do" my commits since then?

1 Answer

0 votes
by (27.5k points)

To split a commit <commit> and add the new commit before this one, and save the author date of <commit>, — the steps are following:

1. Edit the commit before <commit>

$ git rebase -i <commit>^^

NB: perhaps it will be also needed to edit <commit> as well.

2. Cherry pick <commit> into the index

$ git cherry-pick -n <commit>

3. Interactively reset unwanted changes from the index and reset the working tree

$ git reset -p && git checkout-index -f -a

As an alternative, just stash unneeded changes interactively: 

git stash push -p -m "tmp other changes"

4. Make other changes (if any) and create the new commit

git commit -m "upd something".

Optionally you can repeat the items 2-4 to add more intermediate commits.

5. Continue rebasing

git rebase --continue

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories