Upon completing Linux Academy’s “Source Control with Git” course, I have been tasked with learning how to utilize the world’s most popular open source version control system, Git.
So what is Git?
Git is a type of a version control system (VCS) records the changes made to developers’ code in a special database called repository. You can see who has made what changes, when, and why. Upon writing code for a project, should someone come across any errors or malfunctions, Git has the capability to revert back to an earlier state. Without the presence of a VCS, constant copy versions of an entire project would have to be stored in various file folders, which does not exemplify any sort of scalability. If you have several developers collaborating on various projects, the exchange of code would need to be transmitted with some sort of other method, like via file transfer with portable drive or e-mail (security of the code has some risk exposure to potential corruption or loss) and takes on a manual process having to merge pieces of a code project together from various sources, lacking performance/operational efficiency.
Git is a ‘distributed’ version control system that allows developers to retain copies of a shared project with its history on their own computers so they can save snapshots of the project(s) locally on their machine. Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. If the centralized server that houses the main version of a project were to go offline, team members can synchronize their work amongst each other, track project history and effectively collaborate on projects of all sizes.
Benefits of Git?
It’s fast, open source, scalable, and FREE. More than 90% of software projects in the world use Git, which is why most software developer job descriptions mentions Git. Experience using Git is a highly-desired skill sought out by software companies. If you intend on transitioning or applying for a software development position, it’s best you know how Git works, and how it tracks project history and promotes effective team collaboration.
Let’s get started on using Git
Disclaimer: There are a variety of options for installation of Git onto your computer, but for the purpose of this tutorial, my directions are specifically suited for MacOS users since I perform all my work on a MacBook. If you need Windows instructions or require further explanation of Git, I recommend reading this well-written, in-depth blog article by Anne Bonner. You’ll need to also sign up for a free GitHub account here. So without ado, let’s jump into it.
1. Download & install Git
First, go to the following website found here, and click on either the link for the MacOS X instructions or the ‘Latest Source Release’ (they will bring you to the same page). There are several ways to install Git on a Mac. The easiest is probably to install the Xcode Command Line Tools. On Mavericks (10.9) or above, you can do this simply by trying to run git
from the Terminal window for the very first time (I chose to utilize this option; if you own a Mac, you already have a terminal! You can search for it by clicking on the magnifying glass icon in the upper right-hand corner of your screen and search for the word “terminal.” )
Option #1: In a new Terminal window, install Homebrew by copying & pasting the following command, then hit Enter:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
You will be offered to install the Command Line Developer Tools from Apple. Confirm by clicking Install. After the installation finished, continue installing Homebrew by hitting Enter
again.
Copy & paste the following into the terminal window and hit Return
.
brew install git
Congratulations, you’re now ready to use Git!
Option #2: Open a new Terminal window, and type git, then press Enter. A pop-up window should appear, and you should proceed to click Install.
I received a second pop-up window indicating Git could not be properly installed onto my computer. In order to utilize the Xcode Command Line Tools, I realized that I need to download Xcode via the App Store. You should do so as well in case you run into this same problem.
The Xcode is a rather large application, over 11GB in size. Make sure you have enough disk storage space in case you’re close to capacity. It may take at least 10 minutes for the download to complete (depending on your CPU speed), but once it does, open the application & click Agree when prompted to accept the License Agreement. You can close now Xcode and restart your Terminal window.
Once you see Git details in the above image, scroll to bottom or click the ESC button to enter the following command to determine which version of Git you have on your local computer. I was able to successfully install Git, and then I entered the command which indicates my version of Git: git --version
Note: Of course taking the more complex route using Xcode, I will need to update to the 2.28.0 Git version, released as of late July 2020. This may require either updating the old version within Xcode, OR uninstalling Xcode and reinstalling using Brewhouse. But for now, let’s continue with the setup tutorial with the current version.
2. Configuration of your Git environment
There are two main settings that you will need to have in place for your git environment — your name and your email address. These are just used to keep track of who made changes to files, aka setting up your identity.
Copy & paste the git config command below to set up your username for every repository on your computer. Replace “<your name here>” with your own name in quotations. You can use any name or handle you want. If you want to set your name for just one repository, leave out the word “global.”
git config --global user.name "<your_name_here>"
You can then proceed to tell Git your email address you used when you signed up for GitHub account.
git config --global user.email "<your_email@email.com>"
It’s simple to keep your email private by following the instructions in this article. You only need to check two boxes in your GitHub account.
3. Create a new repository
Your repository is where you’ll organize your project. You can keep folders, files, images, videos, spreadsheets, and anything else your project needs. Before you can work with Git, you have to initialize a repository for your project and set it up so that Git will manage it. You can do this right on the GitHub website.
At this point, you should have opened a new GitHub account. If you haven’t done so yet, Go to https://github.com/ and create an account. Once you’ve gone through all the account verification steps, navigate to the top-right corner and select the ‘New repository’ option under the [+] drop-down menu.
- Name the repository, and add a quick description.
- Decide whether you want this to be a public or a private repository.
- Click Initialize this repository with a README if you want to include the README file. (It’s recommend you do this because it’s the first thing people view when they check out your repository. It’s also a great place to put information that you need to have in order to understand or run the project.)
4. Edit, make changes to your repository
At this point, you’re ready to start working on a new project simply by uploading or editing files from your repository through your GitHub account. However, if you’re not satisfied with only this option, you can make changes to your project one of two ways:
a) make changes to your files/notebooks located on your computer; or b) make the changes directly on GitHub.
Let’s proceed as if you intend to edit your README file right on GitHub:
- First, go to your repository.
- Click the name of the file to bring up that file (for example, click README.md to go to the readme file).
- Click the pencil icon in the upper right corner of the file and make some changes.
- Write a short message in the box that describes the changes you made (and an extended description if you want).
- Click the green Commit changes button.
I successfully edited the README file through my Git Hub account, but I could very well have made the same changes on my local computer via the Terminal window with the proper git commands. Working through the Terminal application will be essential for working on projects with Git and GitHub.
5. Cloning your repository
In the event you want to work from you local machine, you would need to either a) clone your new repo(sitory), or b) clone an existing repo. So here’s how we can do so in your GitHub account:
- First, go to the repository in your Github account you intend to clone.
- Click the green Code button on the right side of the screen, which will open a smaller box with Clone details. We want to make sure the details highlighted is HTTPS as an option. You’ll then proceed to click the small Clipboard icon in the top-right corner of this small drop-down box, which will copy the URL from that box menu onto your clipboard.
- Now you’ll open up your terminal and get yourself to the place where you want that repository to land. For this tutorial, I’d like to initiate the clone be placed on my desktop using the following bash command:
cd Desktop
- Clone your repository using the following command:
git clone <your_GitHub_HTTPS_clone_URL>
- You’ll be prompted to enter your GitHub account credentials, and the clone will have been successfully completed as illustrated below. I now have the option or capability to work on GitHub projects directly from my laptop with my cloned GitHub repository in the Desktop folder/directory.
Note: If you haven’t moved around in your terminal before, you can move around slowly with the cd
command until you get where you want to go. For example, open up your terminal and type ls
to list the choices of where you might go next. You might see “Desktop” listed, and you could just type cd Desktop
to get to your desktop. Then you can run the git clone
command above to clone your repository right onto your desktop.
You might see some user names instead of choices like “Desktop.” In that case, you need to choose a user before you see “Desktop,” so choose the user with cd <user>
(replacing <user>
with the user name) and then type ls
again to see your choices. There’s a very good chance you’ll see “Desktop” now. You’ll type cd Desktop
if you see the Desktop listed. Now go ahead with that git clone!
If you ever want to move back a step in your terminal, just type cd ..
If you ever want to just play with a project on your own, you can fork it on the GitHub website instead of cloning it. A fork is a copy of a repository that allows you to freely experiment with changes without affecting the original project. In this way, your fork acts as a bridge between the original repository and your personal copy where you can contribute back to the original project using Pull Requests.
6. Add files to your repository
There should now be a new folder located on your desktop with named as the same repository on your GitHub account. We’re now capable of adding files into this folder either by a) dragging files into it manually from your computer, or b) by using the git add command (which is what we’ll be using for this tutorial).
- Go to your terminal window and navigate to the newly-cloned repository running the command:
cd <folder name>
- To add one of your files to the repository, you would run repeatedly to add a few files to your above-mentioned repo:
git add <filename>
Note: I initially kept receiving an error, so I resorted to copying/pasting 3 JPEG files to my repo, and then I re-ran git add including those 3 file names. I’m now able to view the 3 files in my repo.
- The 3 new files indicated in green are your proposed changes to the repo where we’re bringing new files/changes to Git’s attention. We now need to finalize & save the changes by using the git commit command. To commit these changes, you’ll run:
git commit -m “<commit message>”
- The purpose of the commit message is to include a message explaining the reasoning for the changes, in case you’re collaborating on a team project.
- At this point, these files have been applied to your cloned repository, but not your GitHub account’s repository. To send the changes to your remote repository, run:
git push
to push your changes right into your repository. If you’re working on your local computer and want your commits to be visible online too, you would push the changes up to GitHub with the git push command.
To confirm if everything is up to date, simply run the git status
command. You now have a GitHub repository that includes the added files from the changes you made through your terminal window.
If you’d like further information related to using GitHub in a collaborative environment, I recommend further reading the article I mentioned at the beginning of this story. This concludes this beginner’s guide, good luck on your future project endeavors, thank you for your time.