Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get changes from another branch without affecting current branch at all

Tags:

git

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

like image 625
Andrii Zymohliad Avatar asked Oct 26 '16 04:10

Andrii Zymohliad


3 Answers

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

git merge --no-ff feature
git reset HEAD~1
like image 55
Ôrel Avatar answered Oct 26 '22 01:10

Ôrel


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.

like image 24
1615903 Avatar answered Oct 26 '22 01:10

1615903


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 -
like image 15
njam Avatar answered Oct 26 '22 00:10

njam