Version Control using Git

Purpose & Terminology

Before getting into any specifics related to Git itself, it's beneficial to understand at a high level what version control is used for and why it exists to begin with.

With a VCS (version control system), you can:

  • See all the changes made to your project, when the changes were made, and who made them.
  • Include a message with each change to explain the reasoning behind it.
  • Retrieve past versions of the entire project or of individual files.
  • Create branches, where changes can be made experimentally. This feature allows several different sets of changes (for example, features or bug fixes) to be worked on at the same time, possibly by different people, without affecting the main branch. Later, you can merge the changes you want to keep back into the main branch.
  • Attach a tag to a version—for example, to mark a new release.

Git is a fast, versatile, highly scalable, free, open-source VCS. Its primary author is Linus Torvalds, the creator of Linux. Additionally, there is some key terminology that you should be aware of because they are terms that get tossed around quite a bit.

Key Git Terms:

  • Working tree: the directory, subdirectories, and files being worked on and tracked.
  • Repository (repo): the root directory of the working tree. A 'bare' repo usually ends in .git and is used for sharing or backup purposes.
  • Branch: a project contains at least one main branch and may have multiple offshoot branches where work can be done independently without effecting the production code. Each branch is a series of commits with the most recent commit called the head.
  • Remote: a version of your local repo that is hosted elsewhere (usually in the cloud like on GitHub). For example, if you were to clone (copy) a repo from GitHub to your local machine it automatically adds a remote pointer back to the specific GitHub url. The default name of "origin" is used for the remote. If you create the repo locally first then you must manually add the remote from the terminal, in which case you may call it whatever you wish.
  • Commit: when you make changes to your code or add files or directories and wish to save them to the project for others to see. Commits are usually accompanied with a message indicating the purpose or nature of the commit and a brief summary of what is being changed/added. Commits can be followed by a "push" to another repo, such as one in the cloud. You can also "pull" commits that others have contributed which both fetches them, and then merges them into your local branch.

Commands such as "push" and "pull" are typed after "git" from the terminal and are sometimes followed by other required or optional parameters and switches (flags).

Getting Started

After installing Git on your system you should have access to run git commands from control prompt, PowerShell, and Git Bash terminals so long as the appropriate folders have been added to your path variables.

At this point you must configure your username and email in order for Git to know who is making changes to the files. I also take the opportunity to rename the default branch to "main" instead of using the old default name "master." This is done using the following commands ran one at a time, replacing the quoted placeholders with your actual values.

git config --global user.name "<username>"
git config --global user.email "<email>"
git config --global init.defaultBranch main

Next move to the folder you want to track changes in (put under version control). You may need to create it first. Run the following to initialize it with a main branch. Note: you may still hear the main branch called the "master" branch. Then run the next command to confirm there is nothing to commit yet.

git init
git status

When you initialize a directory a hidden (dot) directory gets created called .git within the folder. You should confirm this.

Basic Commands

This page lists basic Git commands that you should know how to use.

GitHub

Github is a cloud based service that integrates with Git and can be used as a remote for your local projects. It also is a great way to host a portfolio (website) where you can showcase your work and learning using Markdown and GitHub Pages. The best part is it's free and fairly easy to get going. This Microsoft learning path will take you through everything you need to know.