Skip to content

Instantly share code, notes, and snippets.

@bhalash
Last active December 1, 2015 14:36
Show Gist options
  • Select an option

  • Save bhalash/38375ad472916e17c07c to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/38375ad472916e17c07c to your computer and use it in GitHub Desktop.
How to Branch a Git Repository

Git Branches

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.

Branching a Repository

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.

Quick steps:

  1. Pull down an existing branch.
  2. Create your new branch from this branch.
  3. Make your changes to the new branch.
  4. Push your new branch to the server.
  5. This new branch will now be an existing branch if you want to repeat step #1.

Longer steps:

1. Pull Existing Branch

By default, git will:

  1. Push and pull to and from the master branch if you have not specified a branch.
  2. 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.
2. Create New Branch

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:

  1. Create a new branch, then switch to it
  2. Create a new branch and switch to it.
  3. 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:

  1. Create the branch my-lovely-horse
  2. Change your local working branch to my-lovely-horse.

Working branch can be verified with git branches:

$ git branch
Master
* my-lovely-horse
3. Make Changes

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.'
4. Push Changes

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
5. Repeat

Your local working branch will now push and pull to the specified branch, so long as the remote branch exists.

Further Reading

  1. https://www.atlassian.com/git/tutorials/using-branches/
  2. https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
  3. http://nvie.com/posts/a-successful-git-branching-model/
  4. https://pcottle.github.io/learnGitBranching/
  5. http://rypress.com/tutorials/git/branches-2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment