2

I have a branch in git which would apply cleanly to master. It contains one commit with 10 features which would apply cleanly to master. But i would like to merge the changes into master features based. The features changes are not on file level (so one change file may contain changes for 2-3 features, but they can be split up easily fortunatly). But any merge strategy on whole file content fails therefore, it would be more like merge lines 10-20 from file 1 and lines 70-80 from file and lines 100 from file 3 for one feature. Is there any kind of editor which would

  • allow me to pick the lines in the huge commit
  • allow me to merge the picked lines into a small commit into master

--> in the end it would be "one large commit on a branch" converted into 10 commits on master, feature by feature

Is there any tool (chain) which would support such a workflow visually?

Mandragor
  • 4,684
  • 7
  • 25
  • 40
  • 1
    Possible duplicate of [Break a previous commit into multiple commits](http://stackoverflow.com/questions/6217156/break-a-previous-commit-into-multiple-commits) – Oliver Charlesworth May 14 '16 at 12:54
  • this seems to be all very clunky. The workflow would be more like fire up something like kdiff3 , pick first set of changes, commit, rerun kdiff3 (now less changes, pick set of changes, commit, and so on... – Mandragor May 14 '16 at 13:30

2 Answers2

3

Given that the work you'd like to merge piecemeal into master is in one commit on a non-master all-in-one branch, I would recommend breaking up the all-in-one commit first, then merging one smaller commit at a time into master. That way, as opposed to merging in a patch, your Git history will be able to track where the changes came from.

There are multiple ways to accomplish this. One way would be to create a second piecemeal branch off of master from the same start point as your all-in-one branch, merge all-in-one into the piecemeal branch without committing, unstage any staged changes, then start breaking up the unstaged work into smaller commits on piecemeal.

To break unstaged work into multiple commits with visual assistance, git add --patch . allows you to stage (or not) each current unstaged 'hunk' of code to the Git index in the console.

git gui provides a graphical way to do the same: select unstaged file, right click section of file, click Stage Hunk For Commit. See http://www.adamfranco.com/2009/01/13/git-tip-of-the-day-stage-hunks/ for stage by stage pictures and further details.

Mark A. Fitzgerald
  • 1,249
  • 1
  • 9
  • 20
  • 1
    the commit on a dev branch has already happened. i could of course create a patch out of the branch, apply the patch to master (clean) and then use this method.... does not feel like a "natural" git workflow... hmm – Mandragor May 14 '16 at 13:53
  • Sorry for missing that you'd already committed. I'll revise my answer. Your idea is probably the most idiomatic way to accomplish your goal in Git. – Mark A. Fitzgerald May 14 '16 at 14:38
  • Actually, the patch approach can likely be improved upon. I've detailed a way to do so in my updated answer. – Mark A. Fitzgerald May 14 '16 at 14:50
0

Suppose the branch is like -A-B and you want to split B into several commits.

git reset A
git add some_file -p
git commit
git add some_file -p
git commit
...

Reference: git-add take a look at the EDITING PATCHES part and have a try

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • the problem with this approach is, that you need keep track "in your mind" which individual patches (in several files) belong together to one feature. This is maybe a solution for a intellectual master mind ;) but for day to day business I would prefer the _git gui_ solution proposed here, where i actually could browse over several files and compose my commit – Mandragor May 14 '16 at 15:41
  • Hmm... Perhaps replace @ElpieKay's `git add some_file -p` with `git gui` to get what you want, @Mandragor? – Mark A. Fitzgerald May 14 '16 at 19:58
  • Agree with Mandragor and Mark. The better solution is to git commit as often as you could. After all, it's easier to compose several commits into one than to split one into several. – ElpieKay May 17 '16 at 01:45