Git Branching

Updated . Posted . Visible to the public.

##Local Branching
Use the Git branch command to create a new branch:

Serenity:blog_pg$ git branch
* master

Serenity:blog_pg$ git branch new_branch

Serenity:blog_pg$ git branch
* master
  new_branch

Switch to the new branch:

Serenity:blog_pg$ git checkout new_branch
Switched to branch 'new_branch'

Serenity:blog_pg$ git branch
  master
* new_branch

Or, create the branch and switch to it all in one go:

Serenity:blog_pg$ git checkout -b new_branch
Switched to a new branch 'new_branch'

Serenity:blog_pg$ git branch
  master
* new_branch

Create a new branch from an arbitrary commit:

Serenity:blog_pg$ git checkout -b new_branch <*sha1 hash of arbitrary commit*>
Switched to a new branch 'new_branch'

Serenity:blog_pg$ git branch
  master
* new_branch

Rename a local branch:

Serenity:blog_pg$ git branch
  master
* new_branch

Serenity:blog_pg$ git branch -m new_branch my_branch

Serenity:blog_pg$ git branch
  master
* my_branch

Delete a branch (Note: You can't delete the branch you currently have checked out.):

Serenity:blog_pg$ git branch
  master
* new_branch

Serenity:blog_pg$ git branch -D new_branch
error: Cannot delete the branch 'new_branch' which you are currently on.

Serenity:blog_pg$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

Serenity:blog_pg$ git branch
* master
  new_branch

Serenity:blog_pg$ git branch -D new_branch
Deleted branch new_branch (was 2e111ea).

Serenity:blog_pg$ git branch
* master

##Remote Branching
Add a new branch to your local repository and then push it to the remote repository:

Firefly:blog_pg$ git checkout -b new_branch
Switched to a new branch 'new_branch'

Firefly:blog_pg$ git push --set-upstream origin new_branch
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
To http://<<host>>:<<port>>/git/blog_pg.git
 * [new branch]      new_branch -> new_branch
Branch new_branch set up to track remote branch new_branch from origin.

To start using a remote branch, pull from the remote repository into your local repository. The new branch is automatically created for you:

Firefly:blog_pg$ git branch
* master
  
Firefly:blog_pg$ git pull
From http://<<host>>:<<port>>/git/blog_pg
  * [new branch]       new_branch         -> origin/new_branch
Already up-to-date  

Firefly:blog_pg$ git branch
* master  
  new_branch

Checkout the new branch to start working on it:

Firefly:blog_pg$ git checkout new_branch
Branch new_branch set up to track remote branch new_branch from origin
Switched to new branch 'new_branch'

Firefly:blog_pg$ git branch
  master
* new_branch

Delete a remote branch:

Firefly:blog_pg$ git push origin :new_branch
To http://<<host>>:<<port>>/git/blog_pg.git
 - [deleted]         new_branch
 
Firefly:blog_pg$ git branch
  master
* new_branch

Rename a local branch and the remote branch it tracks

Firefly:blog_pg$ git branch
  master
* new_branch

Firefly:blog_pg$ git branch -m new_branch my_branch

Firefly:blog_pg$ git branch
  master
* my_branch

Firefly:blog_pg$ git push origin :new_branch
To http://<<host>>:<<port>>/git/blog_pg.git
 - [deleted]         new_branch

Firefly:blog_pg$ git push --set-upstream origin my_branch
Total 0 (delta 0), reused 0 (delta 0)
To http://<<host>>:<<port>>/git/blog_pg.git
 * [new branch]      my_branch -> my_branch
Branch my_branch set up to track remote branch my_branch from origin by rebasing.

##Additional information can be found at:

The Git Guys

Profile picture of jarsen
jarsen
Last edit
jarsen
Posted by jarsen to Ruby on Rails (2013-03-28 17:39)