Tuesday, July 30, 2013

Git branching and merging

It is always helpful and a best practice to work on a new git local branch, when working on multiple features. Be it a minor defect or a full fledged feature.
Here are a few commands that is useful for a developer for git branching and merging.


Checkout git project from remote repository:


We need to clone a project to use one that already exists in git remote repository. Remember, you can also work on projects from local git repository that does not exist in a remote repository.
To check out a remote git project, run the below command from the git shell:
git clone git@host:MyRepo.git

The project is now cloned to our local git repository. We can start making changes.

Git branching and merging:


Creating a local git branch:
Lets assume we are currently pointing to branch master of MyRepo git repository.
To check out the remote project and create a local branch, run the below command from the git shell:
 git checkout -b mybranch    
  
This creates a new local branch mybranch. We can start making any changes on this branch.
Run the bleow command from git shell to list all branches.
git branch

Now, lets push this local branch to remote, so other team members can use it. Note, in many cases you would create a local branch and not push into remote repository.
Run the bleow command from the git shell:
git push origin mybranch    

This creates a remote branch mybranch.

To check out this newly created branch our friends would run the below command from the git shell:

git fetch
git checkout mybranch
               //Alternative, git checkout --track origin/mybranch   

Merge local branch mybranch to master:


To merge the changes made on the local branch into master we need to first switch to master branch and then perform a merge (ie merge from another branch into master branch). Remember all local changes on the branch mybranch should be committed before merging.

Run the below commands from the git shell:
git checkout master    //switch to master branch
git merge mybranch   //merge mybranch branch into the current branch ie, master.

Do not forget to run a complete (maven) build after merge and before pushing into remote repository.

Run the below commands from the git shell to commit and push the newly merged code to remote repository
:
git add .
git commit -m "useful comments here"
git push origin master

Deleting a git branch:


Now lets get rid of mybranch branch since all the changes are merged back to the master branch and pushed to the remote repository. Be sure that any merges are complete and you want to delete the branch. To delete the local branch run the below command from the git shell:
git branch -d mybranch

Now lets delete this branch from the remote repository. 
To delete remote branch run the below command from the git shell:
git push origin :mybranch    
//indicates to push empty local branch to remote, effectively deleting the remote branch.
or
git push origin --delete mybranch


Hope this post was helpful to the reader. Please leave your valuable feedback/comments.

Read other useful blogs http://techuserhadoop.blogspot.com/