57

Is there a simple way to get changes from another branch without merge or rebase. And keep those changes as untracked (for new files) or not staged for commit (for existing files)?

1615903
  • 32,635
  • 12
  • 70
  • 99
Andrii Zymohliad
  • 1,237
  • 1
  • 11
  • 17
  • 1
    Why do you want to do this? Merging and rebasing are basically the only two options for bringing in changes from another branch. If you don't want to do this, why not just switch to the other branch? – Tim Biegeleisen Oct 26 '16 at 04:05
  • 1
    Because the branch from which I want to update (source branch) needs to be reviewed and then merged to master. I would like to update my current branch (destination branch) directly from master after this code will be reviewed. But I need to test new features from source branch in my destination branch. – Andrii Zymohliad Oct 26 '16 at 06:23

10 Answers10

97

do a merge to get the change then cancel the merge but keep modification:

git merge --no-ff feature
git reset HEAD~1
1615903
  • 32,635
  • 12
  • 70
  • 99
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • 6
    This is actually the best answer in my opinion. Feature branches usually have a ton of their own commits, so unless those are squashed first, you can't do it with `cherry-pick` – Jerry Brady Feb 26 '19 at 22:35
  • @JerryBrady, thanks for the explanation. Somehow I missed your comment. Marking this as an answer. – Andrii Zymohliad Jul 15 '19 at 10:12
  • 1
    It seems it doesn't work well with branches which have many own commits either. If my current branch is far behind the branch I want to get changes from, it would still end-up bringing the commits to my git log. I think @njam's answer is the most reliable solution since it doesn't involve working with git history at all. – Andrii Zymohliad Dec 17 '19 at 16:54
  • 2
    Best answer! If you get `Git refusing to merge unrelated histories on rebase` when doing `git merge feature` then do `git merge feature ----allow-unrelated-histories` – Joaquin Iurchuk Jan 17 '20 at 13:33
  • I don't understand @IvanCastellanos this will not bring a lot (the trick is to use `--no-ff`) this will create one merge commit, then reset the merge but keep the file modified – Ôrel Mar 15 '22 at 13:06
28
git cherry-pick -n <commit>...
git reset

git cherry-pick -n <commit>... takes the changes from one or more commits and applies them to your current working tree without making a commit.

Documentation for -n flag:

-n

--no-commit

Usually the command 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.

git reset will remove picked files from staging.

rofrol
  • 14,438
  • 7
  • 79
  • 77
1615903
  • 32,635
  • 12
  • 70
  • 99
  • 1
    The question was about all the differences from a different branch into the current branch, not about applying specific commits – Ivan Castellanos Mar 10 '22 at 19:18
  • 1
    @IvanCastellanos Nope, OP did not specify that they want to bring ALL the changes from another branch. And in any case, `cherry-pick` accepts a commit range as parameter, which you can utilize to bring all the changes if you wish. – 1615903 Mar 11 '22 at 05:07
  • For example if in the future you want to take the changes from the same branch you cannot just run the same command, you have to find out the current branch's commit, njam answer is completely agnostic to such info and therefore better fit for this question – Ivan Castellanos Mar 15 '22 at 00:05
  • @IvanCastellanos if you check the documentation, you will notice that `` in this command can be specified according to [gitrevisions](https://git-scm.com/docs/gitrevisions), which accepts branch names too. – 1615903 Mar 15 '22 at 05:53
  • Using the branch name only will bring the last commit at branch, not all changes, this may surprise git newbies, the equivalent way of using cherry-pick (to get the same results as the accepted answer) would need two dots before the branch name to include all changes: `git cherry-pick -n ..feature` – Ivan Castellanos Mar 16 '22 at 06:39
21

You can use git diff <another-branch> ^HEAD to print a diff of the changes that are in "another-branch", but not in your current branch (HEAD). And then apply those changes to the current index by passing them to git apply -.

git diff <another-branch> ^HEAD | git apply -
njam
  • 1,233
  • 14
  • 16
9

Checkout the new branch you want to place the changes uncommitted over.

git merge --squash sourcebranch

Works similarly to the accepted answer but is one line.

https://blog.oddbit.com/post/2019-06-17-avoid-rebase-hell-squashing-wi/

ktamlyn
  • 4,519
  • 2
  • 30
  • 41
4

This requires your working tree to be clean (no modifications from the HEAD commit)1.

git cherry-pick <commit>
git reset --soft HEAD~1
git reset .

Will apply changes from another branch to your current branch if commit exists keeping the new files untracked and existing files unstaged.


If you are interested to know how to apply changes from an another branch in an another repository to your current repository. This can be done here.

  1. https://git-scm.com/docs/git-cherry-pick
2

You could use git checkout from the branch you want to transfer the changes to:

git checkout <branch name> . 

That will change all files to match the version in the desired branch. Then you can commit, change, discard whatever you want.

Phill
  • 184
  • 7
0

Do a checkout from your current branch and pull from another branch. This pulls all the commits from the other branch into the current branch. You can work on all the changes without changes being committed to actual branch. Optionally you can commit and push if these changes needs to be tracked.

git checkout <current branch>
git pull origin <another branch>

git commit
git push HEAD
0
  • I have two local branches b1 and b2.
  • b2 was branched from b1.
  • I then did two commits in b2.

Now I want to pull these changes, from b2 into b1, without commiting them in b1:

(b2)$ git checkout b1

(b1)$ git pull . b2
(b1)$ git reset HEAD~2
Daniel
  • 2,380
  • 29
  • 44
0

One way that I use:

Say I have two separate branches. feat-a and feat-b (already pushed to origin)

I need to test something on feat-b but it depends on a change i made on feat-a. Doing the following will reset all your changes to staging for feat-a.

git checkout feat-a
git reset --soft origin/master

Now I have the changes needed to work on feat-b locally.

git checkout feat-b
...changes...

once done, you could commit and push those changes or stash them for later.

And now we can reset the feat-a as though nothing changed.

git checkout feat-a
git reset --hard origin/feat-a

and this will set everything back to the way it was on feat-a.

Mustafa Mujahid
  • 65
  • 1
  • 2
  • 10
-1

To grab changes without a merge, you can use:

git fetch

to grab changes without changing your current branch.

Community
  • 1
  • 1
Kelsey Hannan
  • 2,857
  • 2
  • 30
  • 46
  • 3
    This only fetches remote changes on the current branch. The question is about bringing in changes from a different branch. – Théophile Oct 05 '22 at 16:28