Crash Course Git

I recently walked a colleague through the process of uploading files to a remote Git repo using Git and seeing as the semester is just starting, I anticipate other colleagues asking about this in the near future. Instead of giving the same tutorial multiple times, I think it’d just be easier to explain about it here once on my blog. This post will be walking you through the process of your first Git commit and push. This post will not be covering the intricacies of Git.

Overview

Git is a version control system that allows programmers to track changes to a project stored in a repository. Git streamlines non-linear development and is useful for enabling a large number of programmers to work together on the same project.

This guide will be walking you through five main steps:

  1. Setting up Git.
  2. Cloning a repository.
  3. Adding files to the repository.
  4. Creating a commit.
  5. Pushing your commits.

1. Setting up Git.

Linux/MacOS

If you’re running a Linux or MacOS system, you can install Git from your package manager. It’s as simple as looking it up and then installing it!

Windows

If you’re running a Windows system, this can get a bit more complicated.

Head over to Git for Windows and download the latest release for your system. Most systems nowadays are 64-bit, so you’ll want to download the 64-bit executable. If your system is a bit older then you may want to check your system’s architecture to decide whether you want to download the 32-bit or 64-bit executable.

Run the installer. Unfortunately, I can’t walk you through this as I don’t run Windows, so I don’t know what dialogues the installer features. For dialogues, when in doubt, let it default.

You’ll want to be using Git Bash for this tutorial, as we’ll be using the command line. I know that many Windows users may be scared of the command line, but I assure you that it’s not as scary as it sounds and is actually a more pleasant experience than GUIs!

2. Cloning a repository.

Cloning a repository is akin to downloading a project. To clone a repository, you will use the clone option of the git command, followed by the URL of the repository you’d like to clone.

$ git clone $URL

For example:

$ git clone https://github.com/shawnduong/PXEnum

Afterwards, you can move into the repository by changing directories using cd.

$ cd $REPOSITORY

For example:

$ cd PXEnum/

3. Adding files to the repository.

You can add files to the repository by moving them to the directory (or as Windows users would call it: folder) using any means you’d like. If you’re on Linux/MacOS, you can just use the mv command like so:

$ mv $SOURCE $DESTINATION

For example:

# NOTE: "./" means "here," in case you're out of the loop.
$ mv ~/work/code.py ./

If you’d like, or if you’re on Windows, you can open up two file explorers and navigate to the repository and the files you’d like to move, and then just drag and drop.

4. Creating a commit.

Now, it’s time to create a commit. A commit is a single set of changes made to a project. We essentially want to tell Git that we’re making a change to the project: we’re adding the files we’ve just moved to the repository.

If you’ve never used Git before, you’ll need to run the following commands to get set up so that your commits can be created under your name:

$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@yourdomain.com"

Of course, remember to replace "Your Name" and "youremail@yourdomain.com" with your actual name and email.

To add your files to the commit, run the following command:

$ git add .

. means “all files.” Afterwards, you need to create a commit with a message. You can do this using the following command:

$ git commit -m "Message"

Of course, remember to replace "Message" with your actual commit message.

5. Pushing your commits.

After creating a commit locally, it’s time to push your commit online so that others can see your work. This is fairly simple. You just need to run one command and authenticate when prompted:

$ git push

Conclusion

If you’ve followed all these steps correctly, then you’ve just pushed your first commit! Congratulations. Refresh the online repository to verify that your changes appear and then pat yourself on the back.

Happy hacking!