2

Currently I have the following branch

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

What I want to do is

  • Move my master branch into branch feature-1
  • Copy a new master branch from existing master, and make change

So at the end, both my local and remote will have 2 branches - master & feature-1, and when I push from the local master branch, remote master branch will be updated. While when I do push in feature-1 branch, the remote feature-1 will also be updated.

Ryan
  • 10,041
  • 27
  • 91
  • 156

2 Answers2

2

You don't need to "copy" your current master branch, you simply create a feature-1 branch on top of the current master and push it to origin.

git checkout master
git checkout -b feature-1
git push -u origin feature-1 # only needed once

Note the -u option for the first push of feature-1 branch. You won't need a git set-upstream to link your local branch to a remote branch of the same name on origin.
See "Git: Why do I need to do --set-upstream all the time?" for more.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

All you need to do is

git checkout -b feature-1 master
git push -u origin feature-1

Git will automatically push feature-1 next time you issue git push. It does not matter what branch you are on. When you issue the git push command, git will push whatever branches you have explicitly pushed before to the first remote - but only the ones you have explicitly pushed to that remote. If you have more than one remote you can specify which one git push origin or git push upstream. If you explicitly want to push just one branch to a remote you need to git push origin branch-name.

Tracking (what branch gets updated on the remote when you git push) is handled more implicitly with the latest versions of git.

For open source, typically someone may have 2 remotes:

  1. a remote for their own fork of a projet.
  2. a second remote that's read only and can't be pushed to for the main repo of the project.

This is where you would want to be explicit about which repo you are referring to at certain times.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141