74

I created a repo on GitHub and only have a master branch so far. My local working copy is completely up to date with the remote/origin master on GitHub.

I now want to create a development branch on GitHub so that other people on my team can start pushing changes to development (instead of directly to master) and submit PRs, request code reviews, etc.

So I tried creating a new development branch locally and pushing it:

git checkout -b development
git push origin development:master

But git just says Everything up-to-date. So I ask:

If I'm current with master, how do I just create a remote development branch that contains an exact copy of master?

smeeb
  • 27,777
  • 57
  • 250
  • 447
  • Possible duplicate of [How to push a new local branch to a remote Git repository and track it too?](http://stackoverflow.com/questions/2765421/how-to-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too) – rajuGT Sep 13 '16 at 20:33

4 Answers4

131

Creating a git develop branch

You can list all of your current branches like this:

git branch -a

This shows all of the local and remote branches. Assuming you only have a single master branch, you'd see the following:

* master
  remotes/origin/master

The * means the current branch.

To create a new branch named develop, use the following command:

git checkout -b develop

The -b flag creates the branch. Listing the branches now should show:

* develop
  master
  remotes/origin/master

Changing branches

You shouldn't commit anything directly to the master branch. Instead do all your work on the develop branch and then merge develop into master whenever you have a new public release.

You are already in your develop branch, but if you weren't, the way to switch is as follows:

git checkout develop

That's the same way you create a branch but without the -b.

Making changes on develop

When making changes, add and commit as usual:

git add .
git commit -m "whatever"

The first time you push to your remote do it like so:

git push -u origin develop

The -u flag stands for --set-upstream. After the first time you only need to do it like this:

git push

Merging develop to master

Once your develop is ready to merge into master you can do it like so:

First switch to your local master branch:

git checkout master

To merge develop into master do the following:

git merge develop

Then push the changes in local master to the remote master:

git push

Done.

Deleting a branch

If you don't need the develop branch anymore, or you just want to delete it and start over, you can do the following:

Delete the remote develop branch:

git push -d origin develop

Then delete the local branch:

git branch -d develop

The -d means delete.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
91

When you do

$ git push origin development:master

What's actually happening is git is taking <local>:<remote> and updating <remote> to whatever the <local> branch is.

Since you executed git checkout -b development from master, your local development has all the commits master does; hence it shows as everything is up to date.

You can just do

$ git checkout -b development
$ git push origin development

to push the new branch

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Is `develop` branch same with the other branches? I mean, is there any configuration that we need to setup for the `develop` branch? Just a bit confused – Blues Clues May 15 '21 at 15:26
  • @Jonjie not that I know of. Any branch is the same in Git versioning - master could just easily be called 'main' or a feature-branch could just easily be used as 'master'. – Yashank Jul 22 '21 at 14:43
9

You can also use git flow. This will automatically create the branch with the right naming convention, it's good practice.

git flow init

and to push

git push --set-upstream origin develop
6

This works for me

git push origin development
somesingsomsing
  • 3,182
  • 4
  • 29
  • 46