In the previous post, I looked at the distributed nature and local history of Git. In this post I discuss another feature that makes Git particularly powerful: branching.
Branching in Git
If you look at the guidance for branching in TFVC, you will learn that branching is something you should try to avoid as long as possible. There are scenarios where you do need branching but branching in TFVC is hard and easily leads to merging conflicts and branching hell. Often the best branching strategy is having no branches at all.
Git is different. Git promotes branching. In Git it’s customary to create (and delete!) multiple branches a day. Branching can be done completely locally without having to contact the server. When you start working on a new feature, you can easily create a local branch, do your work and merge the changes back to the main line. This allows you to keep development on several features nicely isolated.
Branching in Git is fast. Creating a new branch and switching between branches are operations that run locally on your machine. You don’t have to contact a central server to branch. Merging between branches is also easier in Git then in TFVC. Since Git knows the completely history, it can see which changes where made since you branched and use this to do a merge as optimal as possible.
While working on some Git projects for VSTS extensions, I created a branch for every new feature I started working on. When finished, I did a pull request (a topic for a future post) or merged the changes locally to the main branch. This is a nice way of working. It makes it easy to have different versions of your application locally and switch between these when the need arises.
Do it yourself
The following script starts with a new Git repository. You then add two new files and commit these to the current master branch. After that, you create a new branch named dev and switch to it. You then add a third file and commit it. If you now switch back to master, you’ll see that the third file is not visible in your working directory.
git init
New-Item index.html -type File
New-Item readme.md -type File
git add .
git commit -a -m "Initial commit"
git checkout -b dev
New-Item script.js -type File
git add .
git commit -a -m "Added script"
git status
git checkout master
dir
What’s next
Gits distributed nature gives you a local history and lets you easily create and switch between branches. Another powerful feature that Git offers is pull requests. That’s what the next post will discuss.
So what do you think? Git: love it or hate it?