Sunday 26 March 2017

Git Commands Part 1 - Create, Rename and Delete Branches

This post will help you to play around the Git Commands for creating a branch in both local and remote repository, also guides to rename and deleting the branches.

1.      Create a Branch: Creating a branch in local machine and push that to the remote repository.

Initiate to create a branch & checkout i.e. switches to make use of the branch, the branch has been available in the local 

git branch branch1    
git checkout branch1  
git checkout -b branch1  

To push this in to the remote use the below command.
git push origin branch1

In case the branch is already created in remote either on Bitbucket or GitHub Portal or any, so now we are need to fetch that & checkout the same into our local.

git fetch origin branch2  
git checkout branch2   
git fetch && git checkout branch2

2.      Renaming a Branch: We are in the branch2, but want to rename the branch1 as branch0, the below command will reflect on local only, not impact on the remote

git checkout branch1
git branch -m branch0
The below command will rename the branch and not necessary the user to
be in the branch which is intended to rename. $git branch -m
<Old Name> to <New Name>

git branch -m branch1 branch0

To reflect the above changes to the remote. 
$git push origin :<old_name> <new_name>

git push origin :branch1 branch0

3.      View the list of branches:
git branch

4.      To-do a self-check, which branch is currently we are working on
git status

5.      Delete a Branch: The below command will help to delete the branch from local.
git branch -d branch0

To delete the same from remote.
git push origin --delete branch0

*To delete the branch, we need not to be in that deleting branch.

          Continue to read Part-2 

1 comment:

Unknown said...

Great work, made it very simple.
Waiting for you article on maven and Junit