Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git, How to push local branch into the specific remote

Could you explain how to push a local branch to a specific remote branch?

$ git branch -vv 
dev 4d46c96 [origin/dev] Merge branch '1783' into dev
dev_3_feature 226b914 second commit in dev_3_feature
dev_second_feature 6b5f10f second commit in dev_2_feature
master baf5fc0 [origin/master: ahead 1] master feature
* myFeature da5cc64 second commit in dev_1_feature
test 334cf7e commiting my super changes locally
  1. I want my DEV features to be pushed into origin/dev and stay there as branches, how can I do that ?

  2. What/where/how should I set up locally to push into origin/dev by default instead of origin/master?

like image 531
Eugene Avatar asked Aug 24 '16 16:08

Eugene


People also ask

Can I push from a local branch to a different remote branch?

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.


2 Answers

Update :

So, generally when you work with Remote, First of all you need to pull the repository or branch.

If its repository then

git pull origin

if its branch then

git pull origin <yourRemoteBranchName>

after you pulled it, it will be on your machine. Now your current branch is yourRemoteBranchName.

If you are new and want to know what "origin" is, then run command git remote -v. It is in a sense alias for github repository so you can remember it. It can be either origin or anything of your choice.


Now, you have above Remote branch, then you can create your local branch from that pulled remote branch. It will create a new local branch from your current Remote branch.

git checkout -b your_branch

The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can just do:

git push <remote-name> <branch-name>

Where <remote-name> is typically origin, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name>

But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!

So that a subsequent git pull will know what to do, you might instead want to use:

git push -u <remote-name> <local-branch-name>

As described below, the -u option sets up an upstream branch:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.

If you want to merge directly with upstream branch,

git merge branchName

You can refer to this documentation : https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging . It has pretty good examples.

like image 200
Ajay P. Prajapati Avatar answered Sep 18 '22 16:09

Ajay P. Prajapati


Switch to the dev branch locally and then push to the dev branch on the origin remote:

git checkout dev
git push -u origin dev

The -u option on git push sets upstream tracking such that when you are on the dev branch, git push and git pull automatically do the same thing as git push origin dev and git pull origin dev.


If I misunderstood your question and you want to push all your branches with "dev..." into their respective branches on origin, you can do the above step for each of those branches, or you can do git push origin --all to push all your branches to the origin remote. So on origin, you'd have origin/dev, origin/dev_3_feature, etc.


If I doubly misunderstood your question and you want to push all your branches with "dev..." into a single remote branch, well, I'd advise not doing that. It's probably best if you merge/rebase all your dev branches into one branch and then push that to origin. Let's say you want to use the one branch called dev:

git checkout dev
git merge dev_3_feature
git merge dev_second_feature
git push -u origin dev

After each merge, you might have to resolve merge conflicts, so be warned.

As a last note, you may want some more descriptive branch names for future feature branches, as names like dev_second_feature doesn't really tell you what the feature is.

like image 31
Lawrence Lee Avatar answered Sep 21 '22 16:09

Lawrence Lee