Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a commit from one branch to another in git without any merging?

I have 2 branches a master and an experimental. A shown:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x

My experimental is quite outdated and i was hoping to update it by simply copying the last commit in the master branch (Y) to experimental:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x-Y

If possible, I don't want to have to do any merging I want to overwrite anything in the experimental (master is my main priority).

Edit: Let me briefly explain the situation: When i try to merge commits at the tips of the master and experimental branch, I get a lot of merge conflicts! The same happens if i try to cherry-pick from the experimental onto the master! I was hoping to avoid them as i simply don't want any of the changes on the experimental! Up until now, I have been cherry-picking from master to experimental and when there are merge conflicts, I just keep changes of master branch. But after doing it many times, i was hoping that there may be some way in which i can do something like a merge except where (i am not prompted with any merge conflicts as master changes is all i need (for all I know it wouldn't matter what was previously on the experimental branch!

like image 797
reubenjohn Avatar asked Sep 12 '13 19:09

reubenjohn


People also ask

Can I copy a commit from one branch to another?

You can cherry-pick a commit on one branch to create a copy of the commit with the same changes on another branch. If you commit changes to the wrong branch or want to make the same changes to another branch, you can cherry-pick the commit to apply the changes to another branch.

How do you bring changes from one branch to another without commit?

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.

How do I pull data from one branch to another in git?

After running the stash command for a branch, if the git user wants to pull the branch's changes to another branch, it can be done easily by using the `git stash pop` command that works like the `git merge` command.


1 Answers

To cherry-pick just commit Y from master onto experimental:

git checkout experimental
git cherry-pick Y

Alternatively:

git checkout experimental
git cherry-pick master

...Will apply the change introduced by the commit at the tip of the master branch and create a new commit in experimental with this change.

like image 109
Johnsyweb Avatar answered Sep 30 '22 01:09

Johnsyweb