Git. Some people love it, others hate it. I have to admit that I was in the camp of the haters. Not that I didn’t like Git, I just found it too difficult and didn’t understand why Git was so popular. Team Foundation Version Control (TFVC) was so much easier to use and I was already familiar with it so why look at Git?
This is the first post in a series where I discuss some of the things I learned about Git and that made me start liking Git more and more.
Local History in Git
Git is fundamentally different from TFVC. Git is a distributed version control system (DVCS) The whole idea behind a distributed version control system is that you not only have the current snapshot of your code but also the complete history in your local repository. In TFVC (a centralized version control system), when you do a Get Latest, the server is contacted and a snapshot of the latest state of your code is downloaded to your machine. With Git, when you download the latest changes to your machine, you don’t download a snapshot. Instead, you download all the changes that where made to your code and apply these to your local repository.
A direct consequence of this is that you can append changes to your local history without uploading them to a central server. This is called a Commit in Git. A Commit is like a changeset in TFVC that’s stored in your local repository. You can commit multiple times and then decide to upload these changes to a central location where other team members can download them. This is called a Push.
Why helped this feature change my mind? The advantage of local history is that you can easily experiment with changes. Instead of having to wait with uploading your changes until your code is ready to be shared with others, you can locally commit your changes and create a history where you can easily go back to a previous version without having to share your code with others.
Do it yourself
To get a feeling of how this works, run the following commands in a PowerShell console on your local machine. Make sure you have installed Git.
First, create a new repository in a folder on your machine:
md mylocalrepository
cd mylocalrepository
git init
Then create two files, add them to your local repository and commit the changes
New-Item index.html -type File
New-Item readme.md -type File
git add .
git status
git commit -a -m "Initial commit"
git status
Now create a third file and commit it.
New-Item scripts.js -type File
git add .
git commit -a -m "Added scripts"
If you check your log, you’ll see that you now have 2 commits. By using the checkout command you can view the state of your files for a specific commit. By using reset —hard, you move the complete state of your local repository back to that moment in time
Make sure to replace the two commit ids with values of your own as shown in the log command
git log
git checkout 0d1d7fc32
git reset --hard 0a2681d8fc
git log
What’s next
This was only one of the features that made me reconsider Git.Understanding the idea behind distributed version control and Git in particular and temporarily forgetting your TFVC experience helps when moving to Git. Of course this doesn’t mean that TFVC is dead and that I’ll never use it. It’s still popular and is actively developed by Microsoft. Git is just another tool for your toolbox.
In the following part, I take a look at Git branching and how that differs from TFVC.
So what do you think? Git: love it or hate it?