623

Is it possible to commit and push changes from one branch to another.

Assume I commited changes in BRANCH1 and want to push them to BRANCH2.

From BRANCH1, is it valid to do:

git push origin **BRANCH2**

And then reset BRANCH1?

Antu
  • 2,197
  • 3
  • 25
  • 40
jviotti
  • 17,881
  • 26
  • 89
  • 148

11 Answers11

1107

That will almost work.

When pushing to a non-default branch, you need to specify the source ref and the target ref:

git push origin branch1:branch2

Or

git push <remote> <branch with new changes>:<branch you are pushing to> 
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 40
    Do both `branch1` and `branch2` need to be on the remote? What if you want to push from local `branch1` to the remote `origin branch2`? – orad Oct 07 '15 at 20:10
  • 18
    @orad: No. The first part is actually just an identifier for a local commit; it doesn't even need to be a branch. – SLaks Oct 07 '15 at 20:18
  • 2
    What if I want to push one remote branch to another without checking out the former branch first? – abhisekp Oct 18 '15 at 00:05
  • 7
    @abhisekp: Use the same syntax. To refer to the source branch, use `/` – SLaks Oct 18 '15 at 00:06
  • 2
    And what if I want to push another branch to it's tracking remote branch without first checking out to make it the current branch? – abhisekp Oct 18 '15 at 00:09
  • 4
    @abhisekp: Do exactly what I just said. The current branch is completely irrelevant. – SLaks Oct 18 '15 at 00:10
  • @SLaks, Lets say I have pushed 2 commits (C1, C2) to dev branch and now I want to push only C2 (not C1) to BugFix branch. – Sukhjinder Singh Sep 12 '16 at 08:29
  • 1
    @SukhjinderSingh: Use `git cherry-pick` to create a local branch from the remote branch that also has that commit, then push that branch – SLaks Sep 12 '16 at 14:46
  • what would happen if there would be merge conflicts with the remote branch? simply the usual error message? – lucidbrot Nov 26 '17 at 17:13
  • @lucidbrot: If the remote ref is not strictly behind the source, you'll get a fast-forward error (even if there wouldn't be merge conflicts). – SLaks Nov 27 '17 at 15:20
  • 70
    Be aware anyone tempted (like myself) to run `git push origin :branch2` thinking that it would just push the current local branch to the remote `branch2`, that it will instead **delete** the remote `branch2`! The correct way is `git push origin HEAD:branch2`. – Helder Pereira Sep 04 '18 at 18:10
  • 1
    It was not a good suggestion, one must know that it will not only just push the last commit but also the previous unsent pushes as well – Rishikesh Chandra Sep 16 '18 at 19:19
  • 1
    @SLaks I tried it, but `branch2` was deleted! and the changes triggered on `branch1` – Benyamin Jafari Oct 28 '18 at 08:03
  • 2
    Be aware that `git push origin :` pushes "matching" branches to origin i.e. : for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side.. This situation could arise with, for example, a shell script with undefined variables. – user650654 Feb 22 '20 at 04:28
  • In second variant does equal ? Suppose Im pushing from my_feature branch to remote dev branch, what should I type instead of ? – Evgeny Nov 09 '21 at 09:54
  • @user650654 Thanks you so much for pointing that out. That could be very bad, particularly if one of the 2 vars is not empty so that `git push origin :branch3` would delete the remote. With the right circumstances that could be catastrophic. – Benjamin Mar 22 '22 at 13:10
  • 1
    @Evgeny use `git push origin my_feature:dev` – iWasCloud May 05 '22 at 08:57
102

Certainly, though it will only work if it's a fast forward of BRANCH2 or if you force it. The correct syntax to do such a thing is

git push <remote> <source branch>:<dest branch> 

See the description of a "refspec" on the git push man page for more detail on how it works. Also note that both a force push and a reset are operations that "rewrite history", and shouldn't be attempted by the faint of heart unless you're absolutely sure you know what you're doing with respect to any remote repositories and other people who have forks/clones of the same project.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
42

It's very simple. Suppose that you have made changes to your Branch A which resides on both place locally and remotely but you want to push these changes to Branch B which doesn't exist anywhere.

Step-01: create and switch to the new branch B

git checkout -b B

Step-02: Add changes in the new local branch

git add . //or specific file(s)

Step-03: Commit the changes

git commit -m "commit_message"

Step-04: Push changes to the new branch B. The below command will create a new branch B as well remotely

git push origin B

Now, you can verify from bitbucket that the branch B will have one more commit than branch A. And when you will checkout the branch A these changes won't be there as these have been pushed into the branch B.

Note: If you have commited your changes into the branch A and after that you want to shift those changes into the new branch B then you will have to reset those changes first. #HappyLearning

Bilal Ahmed Yaseen
  • 2,506
  • 2
  • 23
  • 48
11
  1. Commit your changes on BRANCH1.
  2. Open terminal and enter the command -> "git push <remote_name> <branch1_name>:<branch2_name>"

Done.

For this question: the command is

git push origin BRANCH1:BRANCH2

Edit: Now that I realize, GitHub Desktop has good UI for bringing your changes from one branch to another. Of course not a good option than hitting the commands in the terminal with our hands.

Baalamurgan
  • 211
  • 3
  • 8
10

I got a bad result with git push origin branch1:branch2 command:

In my case, branch2 is deleted and branch1 has been updated with some new changes.

Hence, if you want only the changes push on the branch2 from the branch1, try procedures below:

  • On branch1: git add .
  • On branch1: git commit -m 'comments'
  • On branch1: git push origin branch1

  • On branch2: git pull origin branch1

  • On branch1: revert to the previous commit.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
8

Props for the answer of @SLaks, that mostly works for me. But in the case that the branches have different heads. A convenient method is using cherry-pick.

  1. git log - on Branch1 with your changes
  2. copy commit SHA of your changes
  3. git checkout branch2 - where to apply your changes
  4. git cherry-pick SHA_OF_COMMIT_FROM_STEP_2
John Doe
  • 173
  • 2
  • 10
4

In my case I had one local commit, which wasn't pushed to origin\master, but commited to my local master branch. This local commit should be now pushed to another branch.

With Git Extensions you can do something like this:

  • (Create if not existing and) checkout new branch, where you want to push your commit.
  • Select the commit from the history, which should get commited & pushed to this branch.
  • Right click and select Cherry pick commit.
  • Press Cherry pick button afterwards.
  • The selected commit get's applied to your checked out branch. Now commit and push it.
  • Check out your old branch, with the faulty commit.
  • Hard reset this branch to the second last commit, where everything was ok (be aware what are you doing here!). You can do that via right click on the second last commit and select Reset current branch to here. Confirm the opperation, if you know what you are doing.

You could also do that on the GIT command line. Example copied from David Christensen:

I think you'll find git cherry-pick + git reset to be a much quicker workflow:

Using your same scenario, with "feature" being the branch with the top-most commit being incorrect, it'd be much easier to do this:

git checkout master
git cherry-pick feature
git checkout feature
git reset --hard HEAD^

Saves quite a bit of work, and is the scenario that git cherry-pick was designed to handle.

I'll also note that this will work as well if it's not the topmost commit; you just need a commitish for the argument to cherry-pick, via:

git checkout master
git cherry-pick $sha1
git checkout feature
git rebase -i ... # whack the specific commit from the history

testing
  • 19,681
  • 50
  • 236
  • 417
3

when you pushing code to another branch just follow the below git command. Remember demo is my other branch name you can replace with your branch name.

git push origin master:demo
HandyPawan
  • 1,018
  • 1
  • 11
  • 16
2

you can do this easily

git status
git add .
git commit -m "any commit"
git pull origin master 
git push origin master:development # assuming 'development' is the target branch name.
Paul Irish
  • 47,354
  • 22
  • 98
  • 132
Ramiz Khan
  • 53
  • 6
-1

git init 
#git remote remove origin
git remote add origin  <http://...git>
echo "This is for demo" >> README.md 
git add README.md
git commit -m "Initail Commit" 
git checkout -b branch1 
git branch --list
****add files***
git add -A
git status
git commit -m "Initial - branch1"
git push --set-upstream origin branch1
#git push origin --delete  branch1
#git branch --unset-upstream  
-4

You have committed to BRANCH1 and want to get rid of this commit without losing the changes? git reset is what you need. Do:

git branch BRANCH2

if you want BRANCH2 to be a new branch. You can also merge this at the end with another branch if you want. If BRANCH2 already exists, then leave this step out.

Then do:

git reset --hard HEAD~3

if you want to reset the commit on the branch you have committed. This takes the changes of the last three commits.

Then do the following to bring the resetted commits to BRANCH2

git checkout BRANCH2

This source was helpful: https://git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch

Sasa
  • 1
  • 1