Git, from a Developer's Perspective What is Git? Git is a content-addressable file system wrapped in a version control system.That may sound complicated, but code snippets sometimes speak louder than words: $ mkdir proj $ cd proj $ git init Initialized empty Git repository in proj/.git/
That folder, proj, is now a Git repository. It is that simple. A Scenario Lucas is working on ticket 1034 when Bishop, a systems analyst, comes by and lets him know about a pretty nasty bug in production. Lucas goes to the production system website and is able to recreate the bug. Management has decided fixing this bug is an absolute priority. Since Lucas has the most current knowledge about it, management assigns Lucas to develop a solution. Lucas was working on a topic branch for ticket 1034 and had unfinished work. He stashes those changes, fetches the latest changes from the production branch, and then creates another topic branch from the production branch so he can apply the needed hot-fix. This is where Lucas would open his .NET solution and write failing tests, make them pass, and commit, fixing the production bug. He fetches from the remote production branch again to get any changes anyone else might have pushed up in the meantime, commits his changes locally, and then finally pushes his changes up to the remote production branch in preparation for a production hot-fix deployment. He moves the task to “committed” and goes back to working on ticket 1034. I just described a possible scenario for a developer using Git, which is getting a lot of buzz in the .NET development arena. With the use of GitHub.com and social coding lately, Git’s popularity has increased. Git What is Git? Git is a content-addressable file system wrapped in a version control system. Huh? The simple answer is that it’s a distributed version control system. If you aren’t familiar with version control, it is the management of changes to files or sets of files, as related to software development. Git is the version control system for the Linux kernel and other popular projects (see the Works Cited sidebar). If you look on the Internet you can find many good resources already available on how to install Git so I won’t explain that here. So what is “distributed” source control? In layman’s terms it means you can still put your local code under version control without being connected to a remote server. This will become clearer by reading the remainder of this article. Workflows Learning the syntax is just memorization and muscle memory. The hardest part about Git, and most source control choices for that matter, is the workflow. Git users work with three popular remote workflows: Central Repository (Subversion style), Integration Manager, and Director/Lieutenants. In this article, I’ll focus on the first option as it is the most common workflow. Central Repository (Subversion Style) In this workflow, there is one “blessed” repository, usually remote on a centralized server. A developer will clone this central repository locally, work, pull any new changes, and then finally push their changes back to the central repository. This is how a developer works with other source control systems like Subversion. And as in Subversion, if two developers are working on tickets, the first one to push their changes back to the central repository will not have any issues. However, the second developer will be forced to pull down the first developer’s changes, handle any conflicts, and then push their changes up. Using Git Locally on the Command Line For the following sections I’ll use msysgit/Git Bash on a Windows 7 virtual machine. The dollar sign ($) is my command-line prompt, not something you will include in the commands issued. You can also perform the tasks through the Git Gui interface that’s available. | " | The first thing you should always do when working with Git is set up your name and email settings.
| " |
Git Init As mentioned above, to create a Git repository, use the git init command: $ mkdir proj $ cd proj $ git init Initialized empty Git repository in proj/.git/
The git init command creates a .git folder. In Subversion there is a .svn folder in every directory. Not so with Git, which has only the one .git folder. Git Config The first thing you should always do when working with Git is set up your name and email settings so that your information is attached to your commits: $ git config --global user.name "John Doe" $ git config --global user.email jdoe@site.com
The --global argument is to tell Git that you want this user name and email assigned to any future repositories when logged in as this user to this machine. This will update the ~/.gitconfig file. For the non-*nix readers, the tilde sign (~) means your “home” directory or the C:\Users\<username> folder. If you used --system instead, you would have this setting as the default for all users. If they use the above git config command, it would supersede that system-wide setting. Omitting the --global or --system option would set this for a particular repository only. This way you could use a different email address and/or name on another repository. Git Status Running the git status command shows that you are on the local master branch and that there is nothing to commit: $ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
The following snippet will create two files, test1.txt and test2.txt, by writing (echoing) some text into them. Then you’ll run git status again: $ echo "test1" > test1.txt $ echo "test2" > test2.txt $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test1.txt # test2.txt nothing added to commit but untracked files present (use "git add" to track)
| & | | 
By: Adam Dymitruk
As a software professional, Adam Dymitruk specializes in .NET development, architecture, and Agile practices. Having consulted for numerous clients over the years, he has also acted as a coach, off-shore development coordinator, strategy consultant, technical lead, and development manager for various companies. He has incorporated his own software consulting company as of 1999. Adam has focused his efforts in helping the development community through many avenues such as the board of directors of Agile Vancouver, conference organization for ALT.NET Canada and the foundation of ALT.NET Vancouver. He has also taught I.T. courses at Langara College. Adam holds a couple of Microsoft certifications and an Associate of Science degree. In his spare time, Adam enjoys soccer and the Vancouver lifestyle.
adam@dymitruk.com 
By: Jason Meridth Jason Meridth is a continuously learning software developer. He has experience working for start-ups and Fortune 100 companies.. He has been running his own software company as a consultant since 2001. He believes in automation, test/behavior-driven design (TDD/BDD), open source, and empowering teams to succeed. He holds a bachelors degree in Computer Science. He is also the co-founder and maintainer of LosTechies.com, a very passionate blogging community. He is also the founder of AlamoCoders, a local software development user group in San Antonio, TX. In his free time he loves spending time with his family.
---
Jason Meridth
http://jason.lostechies.com
http://twitter.com/armmer
jmeridth@gmail.com |