Being able to code is a wonderful skill that can open up a world of possibilities for employment, artistic expression, and thoughtful analysis. But an often-overlooked part of coding is the ability to effectively work with other people’s code, and one of the most powerful tools to do that is Git.
You’re probably familiar at least in passing with Version Control Systems (VCSs), and if you’ve worked professionally as a developer, its quite likely you’ve used a platform like GitHub or Bitbucket to collaborate with your coworkers. Both of the services listed above use the same underlying VCS - Git - which was originally developed by Linus Torvalds to meet the needs of managing distributed development of the Linux kernel. It’d be an understatement to say that Git has been a success! The Eclipse Foundation’s 2014 community survey estimates that over 42% of professional developers use Git, and projects ranging from the Linux Kernel, jQuery, Windows, Ruby on Rails, D3, and even git itself.
Learning Git means learning about a collaborative tool that programmers find invaluable across disciplines and languages and projects, and it will absolutely enable you to more easily and safely contribute code to whatever you’re building, whether its for yourself, your employer, or even for one of the largest open source projects out there.
So let’s get started!
— Sorry to interrupt this program! 📺
Given the current situation with COVID-19, times are hard for most of us, but if you're stuck at home you can perhaps use that extra time to work on your React skills. Wes put all of his courses at 50% off to help out with the current challenge we're facing.
Plus, these are affiliate links, so if you purchase a course you help Alligator.io continue to exist at the same time! 🙏
Installing & Configuring Git
For the purposes of brevity, this tutorial will assume you’re running a *nix-like system or emulator for your Command Line Interface. On Windows that could be Windows Subsystem for Linux, on MacOS and Linux it would be opening up a terminal session. To check if you have Git installed, please open up your CLI and enter the following:
$ git --version
If you see
-bash: git: command not found as a response, you either do not have Git installed, or your CLI is unaware of the installation (IE, it is not in your PATH). To install Git, please follow the official documentation for your operating system.
If you definitely have installed Git and your CLI remains unaware of it, its possible you need to add Git to your PATH. To do that, you’ll need to locate where exactly Git was installed. On *nix systems like Linux or MacOs, this usually is at
/usr/local/git/, however the installation can vary depending on how Git was installed and your operating system. If you can get access to the
git command, you can determine the location of the install by the following command:
$ which git
Once you can confirm the location of the installed Git, put the location into the following command:
$ echo 'export PATH=/PATH/TO/GIT/bin:$PATH' >> ~/.profile
so if your git was at
/usr/local/git/, you would run the following:
$ echo 'export PATH=/usr/local/git/bin:$PATH' >> ~/.profile
What the above command does is add the
export PATH=/usr/local/git/bin:$PATH line to your bash profile, which gives your CLI a window into wherever your git installation is, so it can recognize and run the
Now that Git is installed, we need to tell Git who you are so it can appropriately credit you with your work. For now, let’s set up your git installation to credit all work under one name. We can do that by running the below command:
$ git config --global user.name "Betty Bites"
The other piece of information Git needs from you is an email address. Git itself doesn’t see this, but wherever you store your version-controlled code will. Think of this like the email you’d use to sign up with a service like GitHub, Atlassian, Trello, or Google.
$ git config --global user.email firstname.lastname@example.org
Wonderful! Now you’re all set up to work with Git!
Working with Repos
Next, lets go over what Git manages for you: Repositories. The repository, or ‘repo’ is a folder on a computer that has its changes over time recorded and managed by a version control system (VCS)
To create a new repo, we’ll start by making a folder using the
mkdir (make directory) command, then use the
cd (change directory) command to navigate into our new empty folder.
$ mkdir studious-gator && cd studious-gator
So how do we turn this charming and vacant directory into a bustling repo with a robust history? Why, with the
git init command!
$ git init
On many computers, you won’t see any change whatsoever. Even if you run a
ls (the list command, which shows you the contents of your current directory), you might not see anything. So what just happened? Try running
ls -a and you should see that you have a
.git directory hiding in your newly init’d repository. You can
.git and look around if you’d like - this subfolder contains what Git needs to manage your repo’s version history.
It is highly recommended you do not attempt to change or manipulate the contents of your
.git folder unless you absolutely know what you’re doing.
Okay, so you’ve turned your folder into a git repo. But that’s not the only way to create a git repo on your computer, and in fact, when working with others on code, usually only the initial creator of the project runs
git init. Everyone else clones the repository onto their computers.
Cloning is the process of copying an existing git repository onto your computer with its history and version control systems already in place. For this example, let’s say you’d like to contribute to Bootstrap, a popular front-end library. Bootstrap is hosted on GitHub, and if you click the ‘Clone or Download’ button, you’ll see a long text string under the heading ‘Clone with HTTPS’. Select the text and copy it, or hit the clipboard button next to the text string.
The text string should look like a URL, but instead of
.net, it will end it
.git. This address will tell Git where to find the desired repository’s code so it can be copied onto your computer using
git clone, like below:
$ git clone https://github.com/twbs/bootstrap.git
What the above command will do is create a new folder called ‘bootstrap’ in your current directory, with all the current code, already initialized with Git and ready to work.
Congratulations! You made it through, and you’re well on your way to learning how to sling code like the pros. Today we went over:
- What Git is and why its worth learning
- How to install Git on your computer
- How to configure Git for basic use
- What a repository is and how to initialize one from scratch
- How to clone an existing repository
Next time, we’ll make our first contribution to a repo and learn how to push code up to a remote git server so we can collaborate with others!