Click here to Skip to main content
13,146,365 members (43,884 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.6K views
10 bookmarked
Posted 17 Feb 2015

Git Branching Essentials

, 17 Feb 2015
Rate this:
Please Sign up or sign in to vote.
Git Branching Essentials

I’ve been using Git for well over a year to manage my software projects. I was blown away by its fantastic branching capabilities, which helped me try new ideas and prototype stuff at breakneck speed.

So I thought I’d share my experiences with it. This is the first article in a series about development with Git.

Git is a Distributed Version Control System, and each computer has the full repository and commit history, not just a snapshot. This means you have access to your repo anywhere you go.

In contrast to Centralized Version Control Systems like Subversion or Perforce, where working with a branch involves a checkout into a new folder, and then tediously juggling multiple source code trees, Git makes it really easy to seamlessly integrate branches into your thought process.

I find it helps me be creative and productive without losing anything I create, in an unobstructive, intuitive manner.

I’ve drawn up an example workflow below to show you how a development process with Git would look like.

To get you started, I’ve created a Visual Studio project, with a single Awesome class that sorts anything you tell it. You can clone this project from my GitHUb repo:

git clone <a href="https://github.com/nikonian/Blog.git" rel="nofollow">https://github.com/nikonian/Blog.git</a>

If you’re interested, the bit that does the work is in AwesomeClass.cpp. It reads words typed at the console into a vector, sorts them, and iterates over the vector to print it back out. This can be achieved with just three lines of cross platform C++ code. I’ve created a Visual Studio project for this example.

copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(_vec));
sort(_vec.begin(), _vec.end());
for_each(_vec.begin(), _vec.end(), [](string& s){ cout << s << " "; });

After you clone the repo, check the status on the newly downloaded folder:

git status

git status

git status

By default, Git has started us off on the main Master branch.

Now let's try something that will make our class even more awesome.

Let's branch the repo and switch to the new branch using:

git branch <branch name>
git checkout <branch name>

git branch, git checkout

git branch, git checkout

You can also use a single command to create a branch & switch to it:

git checkout -b <branchname>

Let’s get a listing of the branches we have:

git branch -v

git branch -v

git branch -v

The * you see next to the branch name indicates the branch you’re currently working on.

And that’s it! Our working folder is now on our soawesome branch!

Let’s create our awesome new code that can change the world. After writing the code, let’s run a git status:

git status after making changes

git status after making changes

Git has detected our new files, and we can add them all into the staging area with:

git add .
git commit -m "Awesome new code"

Staging files on branch 'soawesome'

git add .
Staging files on branch ‘soawesome’

You can also combine the two into a single command:

git commit -am "Awesome new code"

Once we’re checked in, we can jump back to the Master branch with:

git checkout master

On the master branch

On the master branch

Then we switch to the experimental branch to complete work on the class.

git checkout soawesome

On branch soawesome

On branch soawesome

You can see how git completely replaces your working folder with the branch of your choice, and if the solution is open in your IDE, and if your IDE is worth its salt, it will detect the file system changes and prompt you to refresh the solution.

If you have modified a file, and switch branches, Git will not replace it and wipe out your work, it will mark those files as modified with an M symbol, and you can either check it in or discard the changes with git checkout -- <filename>

So, our awesome new changes are ready, and we can now pull them into the Master branch. We can ask Git to merge the two branches with:

git checkout master
git merge <branch name>

One way to visualize this workflow is to imagine yourself in a sushi restaurant, with Git as your waiter, the dinner table as your working folder, and the branches as different bowls of sushi. You ask Git for the sushi bowl you want and it will clear your table & place your desired sushi bowl before you, with a courteous bow.

You can sample as much sushi as you want by asking Git for a different bowl anytime (git checkout <sushi bowl>), add sushi from another sushi bowl into the one you’re using ( git merge <sushi bowl>), or ask it to get you a new sushi bowl (git branch <sushi bowl>).

I’ve used PowerShell in this article, but you can also use Git bash, that runs on MinGW on Windows, which has the added benefit of displaying the branch name right on the prompt.

Git bash

Git bash

Now go ahead and try out what you’ve just learned! I’ll cover more interesting productivity features of Git in the next part.

Arigato!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
Albert Holguin3-Mar-15 11:11
professionalAlbert Holguin3-Mar-15 11:11 
SuggestionGit branch listing Pin
Albert Holguin3-Mar-15 11:07
professionalAlbert Holguin3-Mar-15 11:07 
QuestionHave you looked at the git integration with Team Server? Pin
rpwt18-Feb-15 10:29
memberrpwt18-Feb-15 10:29 
AnswerRe: Have you looked at the git integration with Team Server? Pin
Feature Extraction19-Feb-15 13:09
memberFeature Extraction19-Feb-15 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 17 Feb 2015
Article Copyright 2015 by Feature Extraction
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid