An active git repository will look like a tree:
- The master branch is the trunk of the tree.
- The side branches are...branches of the tree.
Before a branch can be created on the server, it must first be created on your computer. All branches in a repository are instigated in a client computer, in your local copy.
- Pull down an existing branch.
- Create your new branch from this branch.
- Make your changes to the new branch.
- Push your new branch to the server.
- This new branch will now be an existing branch if you want to repeat step #1.
By default, git will:
- Push and pull to and from the
masterbranch if you have not specified a branch. - Push and pull to and from your specified branch if you have specified a branch.
Example: If you have specified my-lovely-horse as your working branch, git push and pull go to this branch unless you override.
git [command] [user]@[server]:[repository] [optional: local folder]
git clone [email protected]:asongforeurope.git MyLovelyHorse
- Note: The branch you want to pull must already exist on the server. You cannot create a new branch on the server through the act of cloning.
- Note: Local folder name can be whatever you want. Git doesn't care-only the contents matter.
Once you have pulled down an existing branch, you can go into its folder to create a new branch:
cd MyLovely Horse
There are three ways to create a branch:
- Create a new branch, then switch to it
- Create a new branch and switch to it.
- Work on the new branch, then push to the new branch.
I mention these for completeness, because online tutorials will mention one of all three methods. I will stick with the simplest, #2:
git checkout -b my-lovely-horse
This will:
- Create the branch
my-lovely-horse - Change your local working branch to
my-lovely-horse.
Working branch can be verified with git branches:
$ git branch
Master
* my-lovely-horse
Do whatever-make all the changes you need/want-and commit the changes as normal:
git add my_horse_lives_in_a_field.rb
git commit -m 'Added award-winning lyric.'
You must push your changes to a new branch on the server:
git [command] [source] [destination branch]:[local working branch]
git push origin my-lovely-horse:my-lovely-horse
Your local working branch will now push and pull to the specified branch, so long as the remote branch exists.
