Monday, June 8, 2015

Visual Studio Git

Visual Studio provides Git tools and will automatically detect the presence of a local Git repository even if you have set it up using command line tools.  I suggest continuing to use the command line tools if you are comfortable with that.  It can be confusing when you start editing a file that you are tracking with Git.  All of a sudden there is a check mark next to the file in Solution Explorer.  What just happened?  The answer is nothing.  Visual Studio can compare the file with the most recent commit in the repository and it is just letting you know that the file has changed.  If you were to click the Commit button in Team Explorer (remember, I prefer the command line tools), Visual Studio would first stage the files (git add) and immediately commit (git commit) them.

Git Overview

Git is an open-source software version control system.

It is a decentralized system which means that the source code repository resides on your local machine.  You make your changes on your local PC, you commit those changes to a repository on your local PC.

You can create a remote repository on GitHub or Assembla and "push" your changes to those sites in order for your teammates to download and "clone" their own local repository.

You can download Git here:  http://www.git-scm.com  This download includes a couple of GUI tools and Git Bash.

Git is a Unix program.  Although many GUI's exist for using Git, it is recommended that you learn to interact with the original program using a command line.  If you are using Windows, Git Bash is the program that you will use to give these commands to Git.

First configure Git:

git config --global user.name "Steve Emrick"
git config --global user.email steve@steve.com

You need to create a local repository.  Navigate to the directory that contains the folder to store your repository files. Then run an init command.

cd c:\users\semrick
git init <nameofsubdirectory>

To do anything with a repository you have to change the current directory to the directory containing the repository files.

If you change a file in your repository, and you want to commit the change, you have to "stage" the file by adding the file to the "index":

git add <Name of File>

Or, if you have a bunch of files in your repository, and you change a handful, you don't have to add each file individually.  Using the following command will add and only add the files you have changed from what is in the last commit:

git add .

Add on the dry-run option if you just want to see which files will be added:

git add . --dry-run

Perhaps, you want to see which files you have changed, but not staged:

git status

To commit a file in the index:

git commit --message 'A comment'

At this point, you will probably want to create what is called a "remote repository" hosted on the internet. After signing up on Assembla.com and creating a repository there, follow the instructions that Assembla provides.  If you use the https method, you just use the command below. Otherwise if you choose (ssh), you'll probably have to generate a certificate and register it with Assembla and Git.

git remote add origin https://git.assembla.com/{repository name goes here}.git

This creates a remote repository named origin on Assembla.  To "push" a commit to the remote repository just:

git push origin master

To "pull" commits down from the remote repository, just:

git pull origin master

If you make a change to a file, and before you add it to the index and commit it, you decide to undo it, you would:

git checkout <FilenameWithRelativePath>

I believe this replaces the specified file with the version from the most recent commit.  Checkout has a different meaning in Git than in TFS.  An easy way to find the path and all files that have been changed is to do a git status.