Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git push to a different branch with Visual Studio Code?

I realized that after a commit in VSCode there's a "Push" menu option that pushes the commit to the default branch.

However, I often need to push it as well to different branches. Is there a way to do this or run git push --progress "origin" DEFAULT_BRANCH:OTHER_BRANCH through VSCode?

like image 237
CPHPython Avatar asked Jul 13 '16 11:07

CPHPython


People also ask

How do I push to a different branch in Visual Studio?

To push a local branch to the remote, right click on that branch in Team Explorer. From the context menu, that pops up on the screen, select Push Branch.

How do I push one branch to another code?

Push Branch to Another Branch In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

How do I push only to a specific branch?

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details.

How do I commit and push in git using Visual Studio code?

Just enter your commit message and then select Commit All. The equivalent command for this action is git commit -a . Visual Studio also makes it easy to commit and sync with one click by using the Commit All and Push and Commit All and Sync shortcuts.


1 Answers

Update 2021-02-12

On the Source Control tab (Ctrl+Shift+G), press to access Git commands dropdown menu, or simply use F1 and type the Git command:

  1. Switch to the branch to push – Branch > Create Branch OR Checkout to... > ➕ Create New Branch
  2. Stage your changes – button above the files list OR Changes > Stage All Changes (several Git: options available)
  3. Commit your changes – top left button OR Commit
  4. Push the new branch: Branch > Publish Branch

Push command is now for branches already pushed to remote.

Update 2019-08-16

VS Code now has a specific option to allow a Push to... a specific remote (not a branch) through the ... (hover the repo in the source control tab Ctrl+Shift+G) OR through F1+type push to.

TLDR; (re-answering the question)

  1. switch to the branch to push – F1+ type Checkout to... (instead of the old Git Branch option) or choose a Create Branch option
  2. Stage your changes – F1+ type stage (choose one of the Git: options)
  3. Commit your changes – F1+commit
  4. F1+Git: Push pushes the branch you previously created or checked out (using the same name).

Original answer

After all it's possible, this is a non direct way of pushing into another branch with VSCode, so answering my own question:

  1. Switch to the other branch – F1+Git Branch+ENTER, type the name of the branch and press ENTER again (once the exact name of the branch is typed, it will show its id in the autocomplete dropdown or inform you that that's the current branch);
  2. Stage your changes by selecting the files for commit;
  3. Commit the staged files (or all) and click ... and Push.

You can later switch back to your initial branch with F1+Git Branch+ENTER+[branch name]+ENTER. The only problem with this is that VSCode does not allow you to push again those same changes back to the initial branch (even with F1+Git Push).

like image 107
CPHPython Avatar answered Sep 28 '22 07:09

CPHPython