0

I have two branches in my local machine namely master,firstbranch.I have two branches in my github repository namely master and firstbranch_1.Now i want to push from my localbranch(firstbranch) to remote repository of branch firstbranch_1.How should i do it?Note:I have not cloned anything from github.I have first created it my local machine and pushed it into the repo and then made a branch in github.

Sainath
  • 45
  • 4
  • Possible duplicate of [How do you create a remote Git branch?](https://stackoverflow.com/questions/1519006/how-do-you-create-a-remote-git-branch) – phd May 30 '18 at 19:29

2 Answers2

0

Just push. If the branches are not related, then you will probably have to -force it.

git push origin firstbranch:firstbranch_1

(Assuming that the remote is called origin)

eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

Pushing your local branch to GitHub

$ git push --set-upstream firstbranch

The --set-upstream flag is needed "connect" the local and remote branch, thus origin/firstbranch will be shown in your Git log and git status will show an

Your branch is ahead/behind 'origin/firstbranch' by XY commits

message if the local and remote branches differ (so you need to push/pull).

"Cloning" (fetching) branch from GitHub

$ git fetch
$ git checkout firstbranch_1
Branch 'firstbranch_1' set up to track remote branch 'firstbranch_1' from 'origin'.

This will "clone" the branch and creates a local firstbranch_1, connected to the remote one.


Just realized, that I misunderstood your question. To connect local firstbranch and remote firstbranch_1, use

$ git push origin firstbranch:firstbranch_1

But this command would fail if the two branches are differ:

To https://github.com/whatever/repo.git
! [rejected]        firstbranch -> firstbranch_1 (non-fast-forward)
error: failed to push some refs to 'https://github.com/whatever/repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

If you want to...

Replace remote branch with local

$ git push --force-with-lease origin firstbranch:firstbranch_1

This will force the push, the remote firstbranch_1 will be replaced with commits of local firstbranch.

Replace local branch with remote

$ git checkout firstbranch
$ git reset --hard origin/firstbranch_1

This will drop the changes of local firstbranch and move the tip of it to remote firstbranch_1.

Rebase local branch on top of remote branch

$ git rebase origin/firstbranch_1 firstbranch

This will keep the commits of firstbranch_1 and apply firstbranch on it.

bimlas
  • 2,359
  • 1
  • 21
  • 29