Using Git Branches

Software Engineers deal with git branches pretty much every day. Git branches are an integral part of our everyday workflow. To keep it as a reference, we will talk about some everyday required git commands for branching including how to create branch, checkout branch and finally, how git merge can integrate the history of independent branches.

[ Color coded serial numbers represent Git Flow ]
1. List all of the branches in a repository.
     $ git branch
2. Create a new branch called "MyBranch".
     $ git branch {MyBranch}
     This following command tells Git to publish current local branch on the "origin"      remote under the name "MyBranch". The "-u" flag establishes a tracking connection      between that newly created branch on the remote and our local "MyBranch" branch.
     $ git push -u origin {MyBranch}
3. Rename "MyBranch" as needed.
     $ git branch -m {MyBranch}
4. Delete local "MyBranch". This is the safest option for deleting a local branch since branch doesn't get deleted if there is any unmerged changes exist.
     $ git branch -d {MyBranch}
5. This is for force delete. It deletes the branch with any existing unmerged changes. Watch out there is a shark!
     $ git branch -D {MyBranch}
6. Checkout "MyBranch". It makes "MyBranch" current/working branch.
     $ git checkout {MyBranch}
7. It creates new branch "NewBranch" from old "MyBranch". Now "NewBranch" is current/working branch.
     $ git checkout -b {NewBranch} {MyBranch}
     This following command creates a remote "NewBranch". [See #2 above]
     $ git push -u origin {NewBranch}
8. These two commands on this step is to commit whatever changes have done on "NewBranch". First, we need to add the edited file with the first command, and second command is to commit the change with appropriate commit message.
     $ git add {FileName}
     $ git commit -m "Commit Message"
     If we have more than one file changes on our current branch, we can you use following      commands to stage files to commit. The second command stages deletions too.
     $ git add .
     $ git add -u
     The most convenient command is to stage files is the command mentioned below.
     It stages all file changes including deletions.
     $ git add -A
9. Once we are done fixing our code, we can merge two branches together. First, we need to checkout the old {MyBranch} with the first command, and then run the second command to merge the {NewBranch} into {MyBranch}.
     $ git checkout {MyBranch}
     $ git merge {NewBranch}
10. By the way, this is the ideal way to merge a branch since it always generates a merge commit.
     $ git merge --no-ff {NewBranch}


TIP: Run this command to list merged branches while on main branch.
     $ git branch --merged | grep -v "\*"
     Once merging is complete, we can delete {NewBranch} using delete command mentioned above. These are some useful commands for as needed branching that we need everyday at our work or to control our projects. We will remember more than these as time goes by but I hope this list will be helpful for a starter somewhere in this world. Keep coding and keep your code under control.

No comments:

Post a Comment

Grab The Button...

Grab the above button and add to your blog or website to get easy access to Virtual Species. Simply click the textbox, copy the code and paste where you want to see the button. That's it!