Wednesday, July 15, 2015

How to create or delete branch using Git Bash

In order to create new branch, you have to first clone existing repository to your local machine and then modify/add your cloned codebase as required. Once you are done with changes, commit changes to repository with new branch name.

Below are the steps to create new branch

Create New Branch

Clone existing repository:
  • Open git bash window
  •  Move to your work location or where you want to clone your code
              $ cd “c:\users\<loginId>\GitWorkArea"
  • Clone repository to local
             $ git clone <git project url>
  • Get inside your repository folder, in my case it is TestRepo
             $ cd TestRepo
  • Checkout branch to be clone, for me it is develop_test
            $ git checkout develop_test
            Branch develop_test set up to track remote branch develop_test from origin.
            Switch to a new branch develop_test
  •  Fetch latest from repository
            $ git fetch
            $ git pull origin

Now you are done with cloning.
Its time to push newly created clone to repository to create new branch
  • Create New branch
             $ git checkout –b <branchName>
  •  Push new branch to repository
            $ git push origin
  • Commit file to repository
            Step 1 : Add modified/added files to staging area
             $ git add .
            Step 2 : commit changes to local repository
             $ git commit –m “commit message”
            Step 3 : push commit changes to remote repository
             $ git push origin


Delete branch

It is two step procedure, first delete from local and then push to repository to delete from remote.
  • Delete from local file system
                $ git branchName –d
  •  Delete branch from repository
                $ git push origin


No comments:

Post a Comment