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<
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.
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
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:
branch1
: git add .
branch1
: git commit -m 'comments'
On branch1
: git push origin branch1
On branch2
: git pull origin branch1
On branch1
: revert to the previous commit.