'Get changes from another branch without affecting current branch at all

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)?

git


Solution 1:[1]

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

git merge --no-ff feature
git reset HEAD~1

Solution 2:[2]

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.

Solution 3:[3]

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 -

Solution 4:[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

Solution 5:[5]

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/

Solution 6:[6]

To grab changes without a merge, you can use:

git fetch

to grab changes without changing your current branch.

Solution 7:[7]

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.

Solution 8:[8]

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

Solution 9:[9]

  • 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

Solution 10:[10]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 1615903
Solution 2 rofrol
Solution 3 njam
Solution 4
Solution 5 ktamlyn
Solution 6 Community
Solution 7 Phill
Solution 8 Ganeshrajhan
Solution 9 Daniel
Solution 10 Mustafa Mujahid