Can I add changes to staging area from another branch? Can I add changes to staging area from another branch? shell shell

Can I add changes to staging area from another branch?


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

git checkout mastergit 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.


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-treegit reset                      # clear the indexgit 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-treegit reset -p   # interactively remove from the index changes brought by commitid