12

I have 2 branches in my local, for example:

  • FirstApp
  • SecondApp

How to push both of them to remote repo? Do I also need to create two branches in remote as well?

Many thanks!

Rendy
  • 5,572
  • 15
  • 52
  • 95
  • Is this not the default behavior? `git push origin` (assuming your remote is called origin) will push any un-synchronized commits in all local branches. – Austin Mullins Jan 21 '13 at 03:11
  • Hmm is it? I do not dare to try because the repo is very important. – Rendy Jan 21 '13 at 03:13

3 Answers3

31

You can do it by executing the following command.

git push [remote name] [branch1] [branch2]

For example if you want two put branch FirstApp and branch SecondApp to the remote origin, you can execute

git push origin FirstApp SecondApp

If you want push more branches, just add the branch name that need to be pushed to the end.

For more information about git. You can checkout this book from the following link - http://git-scm.com/book

jww
  • 97,681
  • 90
  • 411
  • 885
Suracheth Chawla
  • 984
  • 1
  • 9
  • 20
  • Btw, if someone clone the repo, by right, both branches will be pulled to local right? And btw, how about the pull activity? – Rendy Jan 21 '13 at 03:26
  • Thanks for your ebook, I've quite some time working on git but never on two branches local and remote. – Rendy Jan 21 '13 at 03:29
  • Yes, in this case both of them will be fetched to the local repo. but actually all the branched will be fetched. – Suracheth Chawla Jan 21 '13 at 03:29
  • I've tried it to push it while I'm in master but seems the commit was pushed but not in remote since I've re-cloned it and checkout to the branch by "git checkout -b remotes/origin/FirstApp" but there wasn't my commit in the track list :( – Rendy Jan 21 '13 at 05:01
  • oops, i've checked it using git show , there is my commit but it doesn't appear in my local, do you have any idea how to see it in my local? – Rendy Jan 21 '13 at 05:14
  • From your comment You said that you cloned the repository and then you try to checkout with the code - git checkout -b remotes/origin/FirstApp You should remove the -b and the "remotes" from the code. The code should look like this git checkout origin/FirstApp And then you should checkout new local branch with the same name to work on it locally- git checkout -b FirstApp – Suracheth Chawla Jan 21 '13 at 06:43
  • hi Dev, I managed to solve it around 1 hour ago from this post: http://stackoverflow.com/questions/67699/how-do-i-clone-all-remote-branches-with-git Thanks Dev for your help! – Rendy Jan 21 '13 at 07:34
  • i have tried above command with two branch, but i am able to push changes only in one branch. i am not able to track why second branch is not getting update. Is there any hacks to identify problem? – Jayesh Agarwal May 03 '17 at 11:27
  • Hi @Conqueror, Please show what error are you getting – Suracheth Chawla Sep 23 '17 at 06:49
4

With recent change in the default push policy, I would advise:

 git push -u origin FirstApp
 git push -u origin SecondApp

That way, even with the new 'simple' policy, it will push and create an upstream branch named after your local branches.

Now keep in mind that if you clone back your remote repo, it will not create local branches for all the remote branches: see "Track all remote git branches as local branches".

To see if your branches were pushed after a new clone, check out the result of:

git branch -a
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • hmm what does it mean by "upstream"? I usually do "git push" not "git push -u", does it affect something in my repo? – Rendy Jan 21 '13 at 07:38
  • 1
    @Rendy see http://stackoverflow.com/a/2749166/6309 and the reponse just below http://stackoverflow.com/a/6244487/6309. The `git push -u` is to be done only once, the first time you are pushing a branch, in order to establish an upstream branch. See http://stackoverflow.com/questions/6089294/git-why-do-i-need-to-do-set-upstream-all-the-time – VonC Jan 21 '13 at 08:01
0

Now I use SourceTree to help managing my local repository.

It helps to push all local branches that have not been created at repo by:

  • Press Push button on top bar
  • Check the Push and Track columns for each branches that have not been pushed to remote
  • Press Ok button
Rendy
  • 5,572
  • 15
  • 52
  • 95