How to Create Branch in Git Repository

undefined

Branching is very useful part of project management and software development life cycle. Branches are generally useful when we start work on new modules of any project. It also uses when two development teams works on same project but from different location. They can create separate branch for each team and merge them on task completed.

This tutorial will help you will simple steps to how to create Git branches and switching between branches.

  • Create New Branch:

    Use -b switch to create new branch with ‘git checkout’ command’. As per below command will create new branch and switch to new branch automatically.

    $ git checkout -b stage1
    
    Switched to a new branch 'stage1'
    
  • List Branches:

    This will list all branches used in current working git repository. The branch name start with “ * ” shows your current active branch, in which you are working on.

    $ git branch
    
      master
    * stage1
    
  • Switch to Other Branch:

    Use the following command to switch to any other branch. Change ‘master‘ with your new branch name in below command to switch.

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

    Now again list branches, you will get that master will be your active branch.

    $ git branch
    
    * master
      stage1
    
  • Push New Branch:

    Now push your newly created branch ‘stage1‘ to remote git repository. To push new branch first switch to that branch.

    $ git checkout stage1
    
    Switched to branch 'stage1'
    

    Now use following command to push your branch ‘stage1’ to remote git repository.

    $ git push origin stage1
    
    
    Username for 'https://github.com': your_git_user_name
    Password for 'https://your_git_user_name@github.com':
    Total 0 (delta 0), reused 0 (delta 0)
    To https://github.com/tecadmin/firstrepo.git
     * [new branch]      stage1 -> stage1
    

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *