Kirk Evans Blog

.NET From a Markup Perspective

Git for Team Foundation Developers

Git for Team Foundation Developers

Rate This
  • Comments 16

This post will introduce you to using Git using Visual Studio Online.  This is the first post in a series.

This series is targeted towards easing the Git learning curve for developers familiar with Team Foundation Version Control.

Background

I joined the Azure Modern Apps Center of Excellence in Microsoft around 6 months ago and have been working on a project that uses Git for source control with Visual Studio Online.  It has been frustrating to say the least.  I have been working with TFS source control since 2004, and SourceSafe prior to that, Git was like a foreign language to me. 

To work with the source code, my boss tells me to go clone the repo and create a published branch.  Finally, 6 months later, I can tell you what that means.  No, it did not take me 6 solid months to learn this, but it did take me 6 months to find the time to read, watch videos, and keep trying things for myself until the lightbulb went off. 

This post is to help reduce that time to a single blog post.

You can still use TFS source control.  When you create a project, you have a choice between TFS or Git.  I am going to challenge you to create a project using Git and become familiar with it.

TFS Source Control

I am used to using TFS for source control.  In that model, every developer works with the source control all at once.

image

TFS source control is a centralized version control system.  When TFS first came out, I remember work with customers with geo-dispersed development teams who were concerned about performance because it required a connection to the central repository, so we added the TFS proxy to address those issues.  If I didn’t have access to TFS (I was at a conference with crappy wireless or on a plane), I was kind of out of luck for checking in changes. 

Using TFS, I would create a new team project, which creates a repository for my source code.  TFS manages the deltas between files as part of a change set.  Those deltas are managed on TFS itself.  Each time I modify the source code and check that into TFS, the changeset is stored within TFS, allowing me to roll back to a previous version.

If I were going to work on a new version release using TFS source control, I would create a branch.  The new branch would be mapped to a completely new directory on my hard drive, and all of the source code in that branch would be downloaded to that new directory.  I would have an entire new copy of the source code on my hard drive. 

At the same time that I was working on a new feature, bug fixes occur in the main trunk for the source code, so I have to eventually merge those bug fixes into my branch.  I check in code very frequently, often with comments as simple as “added UI for feature”, resulting in many individual changesets for the feature I am working on. 

Introducing Git

Git is a distributed version control system, which provides several layers of abstraction.  The first layer of abstraction is that every developer has both the central repository as well as a local repository. 

image

This allows you to make lots and lots of changes locally, then finally take those changes as a feature and add them to the central repository.  It took me a LONG time to come around to this conclusion: it’s a better model.  I can make changes to the local repository and, when the feature is complete, I can push the changes from the local repository to the central repository.  This allows me to work offline and with distributed teams. 

Taking that a step further, you only work with a single repository at a time, and you can push your changes from the local repository to a remote repository somewhere else.  I loved how Jessica Kerr (@jessitron) explains this in her video (at the end of this post), think of this as putting a bunch of stuff on a palette.  Once you are done putting things on a pallet (the wooden structure you’d pile stuff on to be lifted by a forklift), you then put them in a truck to be driven somewhere else.

Another abstraction layer is the separation of the working directory, staging area (also called the index), and the repository. 

image

Checking in code is a two-stage process: you first stage the changes that you want and then you commit the changes.  There is a tremendous amount of power in this abstraction. 

These two concepts (the local repository and staged commits) were the things that took me so long to grok.  The rest of this post will walk through a few simple examples.

The .git Folder

Let’s start with a very simple example.  I am going to open Git Bash, which opens a command window. 

image

The very first thing you should do is know where to find help.  Type “git help”.

image

The next thing you will want to know is how to clear the screen.  Type “clear”.

image

To interact with Git using Git Bash, you will use Unix syntax commands.  You can use “cd c/” to change directory to the C: drive, and then use “ls” to list all of the files and folders.

image

Let’s make a directory, “GitDemo”.  Use the command “mkdir GitDemo”, then “cd GitDemo” to navigate to that directory.

image

Now we have some basic directory navigation out of the way, let’s use Git.  Let’s see if our new folder is a Git repository using the command “git status”.

image

This is not a Git repository.  We can tell that just by looking at the directory structure, there is no .git folder.

image

Let’s turn this folder into a Git repository.  We use the command “git init”. 

image

That creates an empty Git repository, which is the .git folder. 

image

Open that guy up, and you can now see what just happened.  A few folders are created for us, namely objects and refs, as well as a config file.

image

Now we check to see if this is a repository again using “git status”.

image

We initialized the repository, which creates the .git folder and its contents.  Let’s delete the .git folder.

image

Now try “git status” again and see what happens.

image

We see that this is not a repository because we deleted the .git folder and its contents.  We can create a new repository using “git init” again.

image

Lesson 1: the .git folder is the “database” for your repository, everything is contained within that folder.

Cloning a Repository

You would typically start by creating a new team project in Visual Studio Online, choosing Git as the source control provider. 

image

This creates that central “origin” repository, but you don’t yet have a local repository.  Once the project is created, click the link to open the project in Visual Studio 2013, and you’ll see a message in the Team Explorer node telling you to clone the repository.

image

Click that link, and you’ll see something like this:

image

That link tells you to create a local copy of the central repository.  I prefer to change the destination path to something other than the default so I can find it easier.  Click “clone”.  Go to that directory, and notice that you have a .git subdirectory.

image

Go back to Git Bash and navigate to the directory using “cd /c/gitlearning”.  Now try “git status” again.

image

Lesson 2: Cloning the repository does nothing more than downloading the files and the .git subdirectory.

Checking in Source Code

I’m going to do something a little bizarre here, so bear with me.  Let’s go to that folder that Visual Studio just created and add a file.  I am going to use “echo” to echo some text to output, and redirect the output to a file called “hello.txt”.

image

That added a file to the directory.  We talked before about the concept of staging changes first.  Let me demonstrate what I mean, we’ll use “git status” to see the status of the repository.

image

This is an “untracked file”, meaning it exists in the working directory but has not been added to the index yet.  To do this, let’s use “git add hello.txt” to add the file to the index, then use “git status” to check the status again.

image

We can see now that there is a change to be committed.  Let’s go inspect the folder structure in the .git subdirectory to see what happened there.

image

This is a SHA-1 hash of the change information.  Let’s commit the change using “git commit hello.txt” –m “My initial check-in”.

image

When we used “git commit hello.txt”, we provided the check-in comment “My initial check-in”.  Now let’s inspect the folder structure again.

image

A few new folders and files.  This is how Git is tracking the changes, using the SHA-1 hash of the change.

Let’s go edit our hello.txt file and change the contents.

image

Save, then use “git status” again.

image

Git knows that the SHA-1 hash of the file is different than what it is tracking, so it reports the file has been changed.  If we want to commit that change, remember we first have to stage it using “git add”, and then commit using “git commit”.

image

Now let’s see the status so far.  Use “git log”.

image

We see our two check-ins that have been committed, and each one is identified by a SHA-1 hash.

Lesson 3: Checking in source code stages changes to the local repository, and those changes must be committed.

Pushing to Visual Studio Online

So far, we cloned the repository and showed how to stage and commit changes to the local repository.  To understand this, go to your Visual Studio Online page and notice that there’s nothing in there.

image

There are instructions to enabling basic authentication for your profile.  After enabling alternate credentials, we can now go back to the command line and push our changes from the local repository to Visual Studio Online using “git push –u origin –all".

image

Now that we’ve pushed changes, let’s look at Visual Studio Online again.

image

We now see that our code has been pushed to the remote repository.  Let’s look at the history.

image

OK, that is seriously cool.  Not only did the source code get copied from my local repository, but it also copied the change history.  This is the most powerful part of Git, the fact that I can push the entire change history, I can cherry pick a few changes, or I can push all of the changes but squash them all into the latest commit.  This allows you to tell a story about what’s in your feature without having a ton of chatty check-in comments in the central repository.

Lesson 4 – Changes are committed to the local repository and then pushed to the remote repository.

Using Visual Studio Git Integration

Now that we’ve seen how to do everything from command line, let’s use Visual Studio instead.  Create a new project and use the same folder.  Check the option to add to source control.

image

The result is a new project in Visual Studio. 

image

We can go to the Changes tab to see the changes.

image

Notice that it looks like the changes are staged.  However, that’s really not the case, Visual Studio is trying to make things easier for us by hiding the low-level details.  Let’s go look at the repository real quick to see the status.

image

Everything Visual Studio just created is untracked as far as Git is concerned.  Visual Studio is going to make things easy for us, instead of having to know anything about staging and committing, we just commit.  Go back to the Changes tab and enter a check-in comment, the Commit button is enabled.

image

You can see there are three different options for committing changes: Commit, Commit and Push, and Commit and Sync. 

image

We will just use Commit to commit the changes to the local repository, just like we did with the command line before.

image

Click that button, “Unsynced Commits” to see the commits that exist in the local repository but have not yet been pushed to the remote repository.

image

We see the commit that we just created in our local repository.  We click Push to push the changes to Visual Studio Online.  Once that is complete, go back to the browser for your project, we can see the changes.

image

Now let’s go see if the source is actually there.

image

Our source code is there, just as we expect. 

Lesson 5 – Visual Studio tries to simplify the interactions with Git, but it will help you tremendously if you familiarize yourself with how Git works.

            

For More Information

Set up Git on your dev machine (configure, create, clone, add)

Develop your app in a Git repository (track, commit)

Work from the Git command prompt

Jessica Kerr did a great presentation that helped me tremendously to understand the benefits of Git.

One of the best videos I have seen for understanding Git concepts
  • Really nice post, would be even better if the scrrenshots were slightly bigger

  • Thanks, ErikEJ, agreed.  I've edited the post to increase the image sizes.  Hopefully better?

  • great article, thanks Evans

  • Thanks Kirk. This came exactly when I started looking into git. I spend last 2 night with VS2013 and Git and was finally going to decide on TFS. This is exactly what I wanted.

    Thank you very much.

  • Great post. How does GIT compare with source controls systems such as SVN. SVN appears to have a very similar model to GIT are they actually that similar? Would be great to learn from anyone who has used SVN and GIT.

  • Thanks MikeR.  I personally have no experience with SVN (I've always just used TFS with CodePlex), but there is a good discussion on StackOverflow:

    stackoverflow.com/.../why-is-git-better-than-subversion

  • Kamlesh - that's why I posted it!  I had the same exact feeling, I even argued with the rest of my team that we should move back to Team Foundation Version Control because I felt Git was more of a hindrance than a benefit.  Now, I feel the exact opposite.  Glad the post is helping others.

  • The major question I have about GIT is how do you ensure that your local repository is backed-up properly? With GIT is there not a tendency for devs to "avoid" pushing their changes up (nobody wants to break the build) and so they can have days worth of changes sitting locally - one hard drive failure later and that dev has lost all of their work since the previous commit. Whilst I can understand the benefit of GIT for remote devs who don't want to rely on network connectivity, for me TFS shelvesets gave the benefit of isolated dev checkins with the reassurance that the code changes were backed-up.

    But...maybe I'm getting it all wrong!

  • finarne - you are not alone, in fact that was one of my key arguments against using Git initially.  Turns out that Git makes this really easy to solve through branching.

    Some developers might choose to keep their changes local for long periods of time, but they would do that with TFVC as well.  However, the branching model in Git makes it ridiculously easy to create a branch for you to sync with where automatic builds are not configured for that branch.  Your changes are still on the server, but not committed to the main branch until you explicitly merge and push changes, at which time the automated build would occur.

    See the second post in the series for more information on branching and addressing the exact concern that we both initially shared.  

    blogs.msdn.com/.../git-for-team-foundation-developers-branches.aspx.  

  • Looks great now, Kirk

  • This is a great post and have increased my understanding on git and differences with VS online.

    Is it possible to connect to GitHub using this approach to save the source code?

  • @shahed

    Pushing source to github (or any other remote location) is super easy.  Its built into got using:

    git remote add ...

    help.github.com/.../adding-a-remote

    In that command you create a named remote that you then push to.  It's that simple.

    Tfs adds a layer of crud to git usage that is unfortunate, and taking someone 6 months to learn how to do the simplest operations in git bears testimony to that fact. Everything is better without tfs.

  • I did a larger exploration of Git in Visual Studio 2013 in this presentation here:

    www.youtube.com/watch

    Use the TOC, as it's a long presentation aimed at beginners.

  • Does GIT allow you to shelve changes to the central repository like TFS? What happens if I have a change I've staged but I lose the local workspace/repository? How do you share uncommitted changes with someone else working remotely?

  • @mongo,

    Thanks. It worked :)

    Cheers

Page 1 of 2 (16 items) 12
Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
Translate This Page
Search
Archive
Archives