Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

Here is the scenario.

I have created a branch like git checkout -b <branch_name>. I then worked on some changes and did

  1. git add .
  2. git commit -m "comment"

Now all those changes are in my uncommitted local <branch_name> branch. What i want to do is to push these changes upstream but I want to push it to a branch which doesn't exist e.g.,<branch_name>_test

I don't want to push it to my <branch_name>How can I do so?

1 Answer

0 votes
by (62.9k points)

The command is git push remote refspec.

In particular, a refspec has two colon-separated parts. For git push, the part on the left of the colon identifies what to push(this doesn't necessarily be a branch name, you can supply HEAD,), and the part on the right side of colon identifies the name to give to the remote. The part on the left side of the colon, in this case, would be branch_name and the part on the right of the colon would be branch_name_test. For instance:

git push origin foo:foo_test

As you are doing the push, you can tell your git push to set your branch's upstream name at the same time, by adding -u to the git push options. Setting the upstream name makes your git save the foo_test (or whatever) name, so that a future git push with no arguments, while you're on the foo branch, can try to push to foo_test on the remote (git also saves the remote, origin in this case, so that you don't have to enter that either).

You need only pass -u once: it basically just runs git branch --set-upstream-to for you. (If you pass -u again later, it re-runs the upstream-setting, changing it as directed; or you can run git branch --set-upstream-to yourself.)

git push -u origin branch_name:branch_name_test

Browse Categories

...