I have a git project. The remote only have a
master branch, at local I have a
proj-structure branch. I created the
proj-structure branch using 'git branch -b proj-structure'. I am currently at
proj-structure branch at local, when I try to
git push, the git suggests me to use
git push --set-upstream origin proj-structure. But I read some articles, I should use
git push --set-upstream master proj-structure. What's the difference of
origin when I set the upstream? If I did use above command, the remote will create a exact branch named
proj-structure, and this branch will have all the commits as my local branch have, right?
Best How To :
You are mixing up two different concepts of git here. The remote and the branch.
Think of the Remote as another computer or another person. Specifying something like
origin will tell git who to send your current set of changes to.
You use branches to keep track of separate lines of development. For example you can use
master to keep a version of your software that is tested and stable and add more branches to try out new things.
Think of them as making a copy that you then modify.
And everybody keeps their copies separately.
Now of course it would be boring if we had to work on our new developments all alone.
That's why we don't just want to keep these branches on our own computer, but share some of them with others.
Usually you will have one central server used for exchange (for example github). This server is a remote an most likely named
origin. Pushing syncs the branch on your server with the changes on the branch on your computer and pulling syncs your computer with the changes on your server.
Now, I have made a change that I know is stable, on the master branch on my computer. I can directly push it to the master branch on our server (origin/master).
Why? Because git already knows that origin is the right remote to push changes on this branch to and it also knows that my
master branch is also called
master on that remote.
Now lets say I make a cool new experiment, that adds a pink rabbit to my software. Because I don't know yet if my boss thinks this is a good idea, I'll just keep it in a separate version for now. I make a new branch called
I can't just type
git push, because our server doesn't have this copy yet. Because the branch is newly created, there is no info who to send it to.
Now I type:
git push --set-upstream origin pink_rabbit
What this does is: I tell git, that the
origin remote is the one to push this branch to and to put the changes in a branch called
pink_rabbit on that remote. The next time git will already have this information saved in the branches upstream and
git push will work directly.
Of course I could have typed master here, but then I would be syncing my experimental version on my local machine directly with the main copy on the server. This kind of defeats the purpose of branching in the first place.
So why have these upstream links at all?
Lets say my company had a policy, that everybody has to prefix their branches on the server with their name.
On my local machine, I will mostly be working with my own branches. What I could do is:
branch -b pink_rabbit
git push --set-upstream origin tim/pink_rabbit
This would allow me to work on
pink_rabbit locally and publish it to the server as
Apart from some advanced usages, you should always name your branches the same (or at least very, very similar) as they are named on your server.