iamjonesy
iamjonesy

Reputation: 25122

Git push specific commit to remote branch

I have a Git repo hosted on Bitbucket. If I have 3 branches say master, branch_2, and branch_3. I have pulled down the master branch to my local machine which I've made a change on, committed and pushed. Now to keep things up to date I want to push that commit onto branch_2 and branch_3 how do I do that? I don't have those branches set up locally.

I've tried git pull origin branch_2 to set up that branch locally but I'm now getting

Auto-merging .gitignore CONFLICT (add/add): Merge conflict in .gitignore Automatic merge failed; fix conflicts and then commit the result.

Upvotes: 2

Views: 1848

Answers (1)

Sergiu Dumitriu
Sergiu Dumitriu

Reputation: 11601

To checkout another branch, you have to do:

git checkout --track origin/branch_2

This creates a new local branch named branch_2, which is set up to be tracking the equivalent remote branch on origin.

Then git pull to fetch changes on that branch, in case you didn't pull already.

Next, you cannot push a commit, you can only push heads. What you want to do is to copy the commit between branches. And there's an easy command to do just that:

git cherry-pick master

This will copy the latest commit from the master branch into the current one. If you want a specific commit instead of the branch head, you can specify its SHA:

git cherry-pick 3f8917a

I would recommend using the -x parameter, which adds a comment indicating that this is a copied commit, and where it was copied from:

git cherry-pick -x master

After that, just git push.

Upvotes: 2

Related Questions