I have a feature branch that has been merged multiple times with a development branch over the the last few weeks. Before we merge it to master branch we want to remove the feature branches code thats been merged into the development branch over the last few weeks. Is there a unified way of doing this?
The merges were created with --no-ff, so the commits in the new branch should have their own objects.
_______ master ______________________________
\ /
\ /
\__hofix-1_____C/
\
_ development _______________________\____________
\ / / /
\ / / /
\_feature-test__A/_________B/________D/
So I want to remove the merged commits A, B and D from development, while still retaining hotfix-1, merge C, and still continue to work on the feature branch.
Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.
There's no problem in deleting branches that have been merged in. All the commits are still available in the history, and even in the GitHub interface, they will still show up (see, e.g., this PR which refers to a fork that I've deleted after the PR got accepted).
Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.
The question is somewhat fuzzy. You have a dev branch and want to unmerge several incoming merges, leaving the rest? If so, you can use rebase -i -p
with some low point, deleting the merge commits form the todo list. Note that you shall re-check all the commits you pulled stuff under, as next work might build on it.
Another approach is to use git revert
on the top for the unwanted changes. It gets the final result done, but likely leave you with a poor quality history.
If I understand your situation correctly, I don't think you need anything as heavy as rebase, revert and the like. You can simply do this:
look through the development log to find the commit hash just before merge A, and create a new branch there called "new-dev":
git checkout -b new-dev somecommithash
now merge in C from the hotfix-1 branch:
git merge C
... and you're done. new-dev branch now has the contents of development, merged with C, minus A B and D.
If you now want to make the developer branch point to the same place as the new-dev branch, that's as simple as:
git branch -f developer new-dev
But for safety you might want to mark the old developer branch location with a tag or branch before forcibly moving it to the new location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With