August 21st, 2021  |  Victor Paredes

Git 101: A Practical Primer That Won't Confuse You

Let’s face it: starting out with git can be confusing. There are a lot of little nuances about git that can seem outdated and some commands can feel misnamed to a newcomer, but there are also a lot of ways to make git way more difficult than it needs to be. This article is a simplification of the basic git workflows that we have all come to know and love.

The Basic Git Workflow

This is the workflow that I use every day: clone, checkout, write code, and publish. Here's how to do it!

Step 1: Clone a Repository... a new directory (folder name will reflect repository name):

git clone{github_user}/{repository_name} the current directory:

git clone{github_user}/{repository_name} .

Step 2: Create and Checkout a New Branch

git checkout -b {brand_name}

Note: -b will create a new branch prior to checking out. If you intend to checkout an existing branch, leave this flag out of your command.

Step 3: Create a Destination for Your Pushes

git push --set-upstream origin {branch_name}

Note: This command will create a new branch (e.g. child repository, usually under main) with your chosen branch name. Branch names cannot contain spaces.

Step 4: Work on Your Code!

Step 5: Add Untracked Files to Git Index

If you’ve added any new folders or files to your project, then you will have to ask git to track them, which is accomplished with this command. If you’ve only updated existing tracked files, then you won’t need to worry about this.

git add .

Step 6: Commit Changes

git commit -am "{Describe commit here in plain English}"

Note: -am will commit all and attach a message.

Step 7: Always Pull Before Pushing 🧐

git pull

Step 8: Push Changes

git push

That’s it! I hope that it helps you on your dev journey! 🥳

Adding a Repository with the GitHub CLI

Using the command line is generally faster and more convenient than having to visit your GitHub account in your browser to manage your repositories. Luckily, GitHub has a great command line tool called the GitHub CLI. Let's see how it works!

Note: In order to use the GitHub CLI, you will need to already have your ssh keys set up and linked in your GitHub account. If you do not, you will need to follow Adding a new SSH key to your GitHub account - GitHub Docs before proceeding with this section.

Step 1: Install the GitHub CLI

For this task, you will need the GitHub CLI. If you are using something other than GitHub, there is a good chance that there is a CLI available for your chosen service. Because I am using MacOS, I will use Homebrew to install via the shell. This can take a sec if Homebrew is not updated, so be patient while everything works:

brew install gh

Note: the first time you run any gh command, you will be prompted to authenticate your access.

Step 2: Authenticate Your Access

Before you can use the GitHub CLI, you will need to authenticate your access.

gh auth login

Then follow the setup instructions when prompted. Again, if you do not have your ssh keys set up yet, you can visit Adding a new SSH key to your GitHub account - GitHub Docs before proceeding.

Step 3: Create a New Repository on GitHub

Now that you have the cli installed, we can create a repository via the command line. This adds a repository on for you to push into. We need this to complete Step 5.

gh repo create {repo_name} --public

Tip: you can also change the trailing flag to --private or --internal to create various repository types.

Step 4: Initialize Git Config Files in a New Directory

mkdir NewFolder && cd NewFolder
git init

Step 5: Add Your Remote Origin

Next we will need to tell Git where the repository is on GitHub which we created in Step 3.

git remote add origin{UserName}/{repo_name}.git

Step 6: Add Some Files

Usually a Git repository will have a .gitignore, so let's just create an empty one so that we have something to push to this new repository. You can add as many as you need or create all of your project files now.

touch .gitignore
git add .
git commit -am "first commit"

Note: If you do not add and commit something to the repository prior to moving to Step 7, you will get this error: error: src refspec main does not match any.

Step 7: Tell Git Into Which Branch To Push

Git already knows which repository it's supposed to use, but there aren't any branches in the repository yet. This is where we create the branch and update the repo with our new files. This is also where everything finally gets connected!

git push --set-upstream origin main

Note: I'm using main as the first branch because this is generally the name of primary branches. You can use another name if you wish.

And that's it! You should have your repo ready to go!

Meet the Author

Victor Paredes is a web and print designer. When not hard at work, you can find him tinkering around with code, music, artwork, and archaic poetic structures. He is based in Portland, Oregon.