Amy Pellegrini Amy Pellegrini - 1 year ago 56
Git Question

Is there a proper way to remove git commits selectively?

Let's say I have a

branches, one with the stable/released version (master) of the code, the other unstable (dev) with unfinished work in progress.

I started coding a test suite branching from DEV, which now should be included (or "released") into master.

The problem is that for a while the branch has been kept updated with DEV, and now there are merged commits from other branches that should not be merged to MASTER.

The commits are properly marked and we know for sure specifically which they are.

The new code to be included in release is part of a test suite, so it doesn't change the source code, therefore it should not be any major problem in rolling back all the unwanted changes/commits from the source code.

The question(s) would be:

Is there a pre-defined methodology to do this? And where is this methodology outlined, if any?

I can research a bit on git to find "my way" to do this safely, but it sounds like a classic case of something which is likely to happen in any standard development process, and in my experience usually there is already a predefined methodology outlined by former (and heroic) developers in their quest for a solid development methodology.

If no methodology is available, or known, any advice would be appreciated.

Answer Source

Are you OK with modifying the history of your branch? If so, I might recommend an interactive rebase of your branch.

git rebase -i <commit_hash>

The commit hash is the first commit you made on your branch. You can pick the commits you care about and skip the ones you don't want (just delete the line if you don't want the commit). After the interactive rebase, you'll have a branch containing only the commits you want to merge into master.

And in the future, be careful about including code in your branch that you don't want to be released with your branch so you can avoid doing this extra work.