5

I am currently working on a team project, with many remote branches. And I cloned the project from our master's branch. Now, that I have made changes to this project locally, I would like to push it, but in-order to avoid pushing it to the projects main remote branch, I would like to create a new branch locally, and push this branch to remote.

The overall idea, is not to smash my local version of the project into the main remote project. I just need my branch to be separate.

So, I'm guessing I will first do

git checkout -b my_new_branch

then

git push -u origin my_new_branch

Will this guarantee, that my branch will not get mixed with anyone's?

GoZoner
  • 67,920
  • 20
  • 95
  • 145

3 Answers3

6

The -u option sets up the defaults in your repository to track my_new_branch on origin. Thus both push and pull will, by default, use origin and my_new_branch. Thereafter you won't need to provide those arguments. Here is an example:

$ mkdir foo; cd foo; git init; touch README; git add README; git commit -m 'README'
Initialized empty Git repository in /private/tmp/foo/.git/
[master (root-commit) 03f3d46] README
 0 files changed
 create mode 100644 README

$ cd ..; git clone foo bar; cd bar
Cloning into 'bar'...
done.

$ git checkout -b my-br
Switched to a new branch 'my-br'

$ touch INSTALL; git add INSTALL; git commit -m 'INSTALL'
[my-br 627d6db] INSTALL
 0 files changed
 create mode 100644 INSTALL

$ git push -u origin my-br
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 245 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /tmp/foo
 * [new branch]      my-br -> my-br
Branch my-br set up to track remote branch my-br from origin.

$ touch Makefile; git add Makefile; git commit -m 'Makefile'
[my-br f2390c1] Makefile
 0 files changed
 create mode 100644 Makefile

$ git push
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 255 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /tmp/foo
   627d6db..f2390c1  my-br -> my-br

Notice that the final git push uses the proper defaults. The main remote repository is safe from your repository smashing it.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
2

As others have pointed out. You are absolutely correct.


Just to sum up all the details.

So, I'm guessing I will first do

git checkout -b my_new_branch

That is correct approach to create new branch with name my_new_branch. You are adding all of the changes that you have made in master branch to this new branch.

then

git push -u origin my_new_branch

This then creates your new branch on origin which is being followed with your branch. The -u is the same as --set-upstream. More info in amazing answer for why always git branch --set-upstream.

From now on you can use git push and git pull on remote repository as you wish with your branch linked from local repository to remote repository.

Will this guarantee, that my branch will not get mixed with anyone's?

YES. Your master branch is absolutely safe from your push(es). And your branch is safe of mixed code, unless anyone else pushes to this branch. If this condition can be applied it's safe to say your code will not get mixed with anyone else's code.

Polostor
  • 187
  • 6
  • 25
1

In your example your pushing a local branch to the remote repo. In this way the branch will be separated from the master branch.

So unless other people voluntarily push in your branch it will not get mixed with anyone's.

Atropo
  • 12,231
  • 6
  • 49
  • 62
  • So, making a local remote and pushing it in the way I have stated does not cause problems to others? btw: Can we see the local branch when doing `git remote show origin` in the list of remote origins? Because I can't see mine. –  Sep 25 '13 at 15:57