Reputation: 4343
I have my main branch and a feature branch but the feature branch contains too many commits (it was branched from an earlier point and then merged and diverged, etc).
I want diff my feature branch against the main branch and then whatever the changes are, I just stash it. Then I can create a new branch and git stash pop
it, which will give me a much cleaner branch log history.
Is there an easy way to do this?
Upvotes: 5
Views: 2141
Reputation: 164809
What I want to achieve is to just diff my wip branch agains the prod branch and then whatever the changes I just stash it. Then I can create a new branch and git stash pop it, which will give me a much cleaner branch log history. Is there an easy way to do this?
If I understand, what you want is your wip
branch to be on top of prod
and with a cleaner history. You don't need the stash for this. Instead, make a patch with git diff
and apply it to a new branch with git apply
.
# Make a new wip branch from prod
git checkout -b new-wip prod
# Get the diff between prod and wip
git diff prod wip > wip.patch
# Apply the patch
git apply wip.patch
Then delete the old branch and rename the new one.
git branch -D wip
git branch -m new-wip wip
But in general when you want to clean up history you use rebase
. First, use rebase
to rewrite the wip
branch so all its changes are on top of prod
.
git checkout wip
git rebase prod
This will smooth out any merges or other history artifacts in wip
. Now it's as if you wrote all the changes in wip
on top of the latest version of prod
all along.
Then use an "interactive rebase" to modify the wip
branch as you like. This can include using fix
and squash
to mash multiple changes together into one.
# Interactively rewrite the changes since prod
git rebase -i prod
This is a more general and elegant way to accomplish your goal. It allows you greater control over your commits and history. You don't just have to mash everything together, rather you can be selective.
Upvotes: 5
Reputation: 30212
Let me see if I get this straight. You would like to start a new branch starting from prod that has all changes between wip and prod and that you will commit as a single revision following prod forgetting about the detailed history of wip?
If that is the case, I would use reset for that:
git checkout -b new-wip wip # place new wip where old wip is
git reset --soft prod # set branch pointer where prod is
At this point you should be able to commit and you will get branch new-wip that has a single commit after prod and that has the tree exactly like that of old-wip.
Upvotes: 2