Git is not a new technology; it was created in 2005 making this technology one of the oldest standing software to maintain its value and user base over time. It is a fundamental technology in IT that stands beside technologies like Linux, Apache servers, MySQL, Java, and Microsoft Office. Like any technology that becomes fundamental in modern tech practices, most people know enough to get through their day jobs but that’s where it ends. No shame. I was “most people ” until recently, so let’s learn together.
What is Git best at?
Git is primarily a collaborative source code system. Its distributive nature allows each member of a software team to maintain their copy of the code base locally, which increases redundancy and offline work. Each local copy is efficient because Git’s storage compression technology decreases the size of the data while it maintains its speed.
What makes Git so collaborative?
One of the most collaborative feature sets of Git is its branching capability. While branching can be complex to understand, it gives users some of the most flexible workflows available while maintaining the integrity of the fundamental code base. By isolating changes multiple people can work on one code base in a non-linear way. Each change can also be code-reviewed in a simplified manner by only looking at the specific changes made.
How does Git differ from Source code repository platforms?
Many people confuse Git the tool with a source code repository platform – especially GitHub. However, Git is the technology that manages the code while a source code repository like GitHub, Azure DevOps, Gitlab, or BitBucket hosts the code repositories. The Source code repository platform will provide the interface for managing the repositories with a Graphic User Interface (GUI), as well as provide extra features such as code reviews, issue tracking, and Continuous integration/continuous development tools.
While every source code platform has a GUI nowadays, the most versatile way to understand Git and how it works is to get into the command line. Each system and developer environment will have a GUI for interacting with Git, even Git has a GUI, however, to keep consistency and deep dive into the technology all the examples in this article will walk you through each example using the command line.
Getting Started:
Prerequisites:
- Git installed locally
- A Git-managed Repository
Definitions:
Repository: A collection of files and history managed by Git.
Remote Repository: Refers to a repository not hosted on the local computer. Remote Repositories were originally hosted on a server but are now hosted in a Source code repository platform. Examples: GitHub, Azure DevOps, GitLab, Bitbucket.
Local Repository: The repository state as it exists on your local host.
Clone: A command (git clone) to create a copy of an existing repository.
Initialization: The process of starting a new Git repository with the git init command. This creates a new .git directory in your project, establishing it as a repository that Git can manage.
Install Git by going to https://git-scm.com/downloads and installing the correct version for your local environment.
Get the “Clone” link from your Git-managed repository.
The example below is from the source code repository platform GitHub.
Open File Explorer and create a folder for all your Git repositories in a directory that is not backed up or synced to cloud services such as OneDrive, Google Drive, or Dropbox. Move into your newly created directory and right-click> Git bash here.
Clone down your repository with the HTTPS link shown in the GitHub screenshot above.
Git clone <Insert Clone link>
If you do not have an existing repository you can create one by Initializing a new repository.
Git init
Creating your first commit:
Stage: A command (git add) to stage changes in the working directory for the next commit.
Commit: A snapshot of changes in the repository. It includes a message describing the changes.
Pull: A command (git pull) to fetch and integrate changes from a remote repository branch.
Push: A command (git push) to upload local branch commits to a remote repository.
Sync: a slang term often used to reference a Pull and Push to ensure the local repository is in the same state as the remote repository.
You officially have a local Git repository connected to a remote repository! Traditionally Source code software was hosted on a Server in a company’s personal data center. Nowadays most companies opt for a cloud-hosted application to minimize management headaches. In this article, I will refer to either of these solutions as remote repositories since that is usually how the Git documentation refers to the software maintaining the Git repositories.
Anything you save locally will not affect the code on the server until the code is Staged and Committed. These steps are two of the most fundamental steps to managing Git because they are what enforce “Atomic” commits -essentially packaging each change into its most functional changes.
**Make sure to use a repository you created for personal use. Many collaborative repositories utilize Branching to organize the code which will be explained in another section.**
To create your first commit, create or edit a file within the repository directory and save it. In the Git bash console run the below commands.
Stage your changes by running “git add” for all the changes you want to add to your commit. When only updating one file this can seem unnecessary, however, most changes in large applications require changes to multiple files. The staging process creates control over what changes get selected for the commit.
git add <file you created or changed>
Commit the files by bundling them together with a short descriptive message describing the changes.
git commit -m "short description of change"
Repeat the Stage and Commit steps as many times as makes sense and then sync your changes using the commands below with the remote repository for collaboration and redundancy.
Pull gets any changes to the remote repository since you cloned the repository and any other changes that other people might have changed since your last pull.
**It is important to pull before you push in case someone else has pushed code that causes a Merge Conflict which will be covered in another section.**
git pull
Finally, Push your changes up to the remote repository.
git push
Congrats! You have made your first commit and synced your changes from your local workstation to the remote repository!
Common Errors:
If you clone an existing repository, it will usually set up your profile for you. If you get an error like “fatal: unable to auto-detect email address” then set user information on the repository using the below commands. This can happen under many circumstances but is most common if you use multiple accounts for different repositories. For example, I have a few personal repositories with my personal GitHub account and repositories for work with a different account.
git config user.name USERNAME
git config user.email EMAIL@EMAIL.COM
To confirm the account information
git config --list
In Part II of my “Unlocking the Potential of Git” series, you will learn about Branching.