13

I have two branch,

  • master
  • new-features

New features have a commit I want to add to master but I want the control how much I want to merge.

I would like the option to add/reject changes for each line. Similar I can do with git add -p

I have searched a lot probably I was searching wrong term. This seems quite obvious task.

cn007b
  • 16,596
  • 7
  • 59
  • 74
Sisir
  • 2,668
  • 6
  • 47
  • 82
  • 1
    There's only one staging are in a working directory, the isn't one per branch. – choroba Jul 04 '16 at 06:34
  • @choroba: in fact, there's only one *standard* staging area (aka "the" index), but you can define your own temporary index using the environment variable `GIT_INDEX_FILE`. That's not a useful thing to do for this particular task. :-) But unfortunately it's something one needs to know in order to explain some odd corners of `git commit -a` and commits with `--only` or `--include`. – torek Jul 04 '16 at 06:55
  • @torek You can have multiple working directories with one local repository, each of which will have its own index. So you’re both right :P – poke Jul 04 '16 at 06:58
  • @poke: he said one "in a working directory" (one per work-tree is how I put it, when I talk about `git worktree add`). But, for example, `git commit file.ext` when you've staged a *different* version of `file.ext`, wipes out your staged version after committing the work-tree version, and the reason for that is that it copies the standard index to a temporary index, adds the work-tree version of `file.ext` to the temporary index, commits, and then *copies back* the newly-committed `file.ext` from the temporary index to the standard index. (I'd argue that this is a bug.) – torek Jul 04 '16 at 07:08
  • Another way to see this temporary-index behavior is to use `git status` while paused in the editor on a commit being made with `git commit -a`, `git commit file.ext`, and the like. – torek Jul 04 '16 at 07:10
  • Possible duplicate of [partly cherry-picking a commit with git](http://stackoverflow.com/questions/1526044/partly-cherry-picking-a-commit-with-git) – Leon Jul 07 '16 at 19:26

2 Answers2

13

Use git cherry-pick with the -n|--no-commit option and then interactively select what to commit:

git cherry-pick

...

-n, --no-commit

Usually git cherry-pick automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

This is useful when cherry-picking more than one commits' effect to your index in a row.

So the sequence of commands will be the following:

git cherry-pick -n <commitid>  # merge commitid into the index and working-tree
git reset                      # clear the index
git add -p                     # selectively add merged changes to the index

Alternatively, you can use git reset -p to remove undesired hunks from the staging area:

git cherry-pick -n <commitid>  # merge commitid into the index and working-tree
git reset -p   # interactively remove from the index changes brought by commitid
Leon
  • 31,443
  • 4
  • 72
  • 97
  • Thanks, It worked. I tried with `cherry-pick` before but I didn't used `-n` flag. Appreciate your help! – Sisir Jul 04 '16 at 09:44
5

I think you can try to use patch file.
To create patch file run:

git checkout master
git diff ..new-features > patch.diff

In file patch.diff you have difference between branches master and new-features.
Now you can apply patch file, run:

git apply patch.diff

Now you can manage your changes in any desired way.

cn007b
  • 16,596
  • 7
  • 59
  • 74