511

I have a branch called develop in my local repo, and I want to make sure that when I push it to origin it's merged with the origin/master. Currently, when I push it's added to a remote develop branch.

How can I do this?

picardo
  • 23,016
  • 32
  • 98
  • 148
  • Is there a particular reason you don't want to merge it locally into `origin` and then push that to the remote? – bhamby Mar 24 '11 at 18:15
  • I think @galador means merge locally into `master` - but it's a valid point. If it's stable enough for origin's master branch, then *surely* it's stable enough for your master branch! – Cascabel Mar 24 '11 at 18:31
  • 1
    @Jefromi Yeah, whoops, typo. -_- – bhamby Mar 24 '11 at 18:33
  • 2
    @Jefromi imagine you were working on something in your local master and you made some commits which are not yet ready to push. Then it turns out that you have urgent task. You create a local branch from remote master, work and push your changes back to master. Maybe it's not the best way but that's what happened to me and thats what I did. It worked flawlessly as intended. Well, I should've been working in separate local branch in the first place, but it's pain in the ass to branch all the time and it was too late. – pinkeen Feb 25 '13 at 10:21
  • 21
    I'm pushing a development branch to a non-public-facing testing app on Heroku to see how things work in Heroku's environment. Definitely don't want to merge my code into `master`, but Heroku won't run the code unless it's on `master` within the app. This is a perfectly reasonable request with legitimate use cases! – JacobEvelyn Mar 01 '14 at 17:50
  • I used it all the time when push local working branch into heroku test server. – ecleel Apr 15 '14 at 16:47
  • i encountered what i consider a good reason to do this. I add a remote called "questions" which goes to a separate repo than the main project. I then add only certain files to a "questions" branch and I want them to go to the master branch of the questions repo. This is an easy way to do this. – max pleaner Jul 10 '14 at 21:07
  • My main use case is because I sometimes work on multiple branches and create code reviews for different features simultaneously. Merging back into local master seems like an unnecessary step. – Juan Mendes Mar 30 '17 at 15:31
  • This question is also more in line with how the developers at GitHub use GitHub, where they promote code from feature branches not master. This came from a presentation on Oct 4th 2018 by Christian Weber from GitHub called "Continuous Delivery: How GitHub deploys GitHub" (https://dogfoodcon.com/session/?id=11) One benefits is that the master branch become a quick and stable recovery in the situation that @JacobEvelyn mentioned. – SMAG Oct 26 '18 at 19:24

5 Answers5

893
$ git push origin develop:master

or, more generally

$ git push <remote> <local branch name>:<remote branch to push into>
mipadi
  • 359,228
  • 81
  • 502
  • 469
  • 30
    Don't do "git push origin :master" that can remove your existing branch o the remote – Mangirdas Skripka Jan 12 '15 at 16:27
  • 54
    True @MangirdasSkripka! Just use `git push origin head:master` if you don't want to specify the name of the current branch :) – Francesc Rosas May 29 '15 at 12:01
  • 26
    Good Idea @FrancescRosàs, it's HEAD (capital letters) though. – smokku Aug 06 '15 at 09:09
  • @Mipadi if i have mender permission, can i move this – Neeraj Sharma May 18 '16 at 05:15
  • 11
    @NeerajSharma: I don't know what mender permission is, or what you want to move. – mipadi May 18 '16 at 18:21
  • This answer was very helpful. In my case, I was pushing from local master to remote non-master branch. It worked beautifully. Then I could checkout the remote branch to get myself synced with where I should have been working in the first place, and moved forward with working from there, without issues. – Speeddymon Nov 16 '19 at 16:03
214

As people mentioned in the comments you probably don't want to do that... The answer from mipadi is absolutely correct if you know what you're doing.

I would say:

git checkout master
git pull               # to update the state to the latest remote master state
git merge develop      # to bring changes to local master from your develop branch
git push origin master # push current HEAD to remote master branch

 

Travis
  • 10,236
  • 3
  • 21
  • 38
Eugene Sajine
  • 7,646
  • 2
  • 21
  • 26
  • 25
    Just to mention, there is some sense in his request, I am using that case to deploy(push) my development to Heroku master – Fabiano Soriani Jul 07 '12 at 07:47
  • 5
    Again, this technique is IMHO for advanced git users only. Not that it is too difficult, it just requires people to understand what are they doing and why it works the way it works. Everybody is free to use git to their liking, but I think following the approach i suggested is much better for educational purposes, especially for git newcomers. Clean "topic branches" is the way to start, you can optimize your workflow later on if needed. – Eugene Sajine Jul 10 '12 at 17:57
  • 1
    It's actually a fairly common thing to do. The master branch usually is your deployment branch. If two teams intentionally diverge their deployment, then chances are you're going to use a cherry-picked local branch to push patches upstream. Now you might argue that the changes should be pushed upstream to a branch and then merged from there into master, but it certainly isn't that odd to do it the other way, IMO. – dgatwood Jul 31 '13 at 01:35
  • +1 This is the most logical way to "build" a production release. – Ryan McCullagh Nov 26 '13 at 07:09
  • How would you do it if you don't want to merge with develop but have develop rebased to master? – user239558 Mar 17 '15 at 12:22
  • This seems like the most correct answer: 1) switch to master, 2) pull any changes, 3) merge _locally,_ and finally 4) if you're happy with the result push changes to master. – Jared Feb 10 '20 at 19:08
0

You can also do it this way to reference the previous branch implicitly:

git checkout mainline
git pull
git merge -
git push
Eric Woodruff
  • 5,907
  • 3
  • 31
  • 32
-1

As an extend to @Eugene's answer another version which will work to push code from local repo to master/develop branch .

Switch to branch ‘master’:

$ git checkout master

Merge from local repo to master:

$ git merge --no-ff FEATURE/<branch_Name>

Push to master:

$ git push
bunbun
  • 2,516
  • 3
  • 28
  • 51
Ram
  • 73
  • 1
  • 7
-6

Follow the below steps for push the local repo into Master branch

$ git status

Adam
  • 1,068
  • 1
  • 11
  • 28