DEV Community

Billy Witherspoon
Billy Witherspoon

Posted on

Making Git, Lit (A Fun Intro to Version Control)

Note: All referenced commands are using Bash on a Mac OS X Terminal. Github is used as an example version control platform.

Project. Project-final. Project-final-FINAL. Project-please-let-this-be-the-last version. Project-spaghetti. Project-done-complete-over-submit.

Look familiar? Maybe you’ve been down this path of fear. That you’ve worked tireless hours and dread the thought of losing your hard earned work. Maybe you’re going out on a limb and don’t want to lose your current version for posterity.

You need version control in your life

“Hey Max? Can you send me the updated file?”
“I don’t have it, Stephanie is currently working on it”
“Hey Megan, I just sent you my changes”
“You did? Max just told me, Stephanie is supposed to be working on the file”

Look familiar? The decrease in productivity, the continual cycle of communication and manual changes.

You need version control in your life.

Enter Github

Github is a version control platform that enables multiple users to seamlessly and simultaneously work on the same set of files and directories. Storing a breadcrumb of versions along the way.

Quick Definitions

Let’s run through a set of definitions. Don’t skip these!

Directory: A folder

Project: For this post, refers to a set of files nested in directories.

Repository: A place where a project are stored (hint: it can be stored in multiple places). Often referred to as a repo.

Local: Your machine, your computer, and only your computer.

Remote: A place that is not your machine.

Branch: A repository linked to the master repository. Can be local or remote.

Working directory: Your workspace. Where you access and edit your project directories and files once you have created a local repository.

Where Da Repos At?

Ok so you’ve got a project. Do you have a project? No? Well get on it!

Build Your Project Directory

This is your project directory, add your files, your code, your pictures, your memes, your last will and testament, and whatever else seems prudent.

Alright, you’ve got your project ready to go. Now what? Start a new repository on Github!

Create Github Remote Repository

Go to Github.com and create a new repository. This will be the central point for your version control. This won’t be the only repository, but it is the most important. This repository is considered remote as it is hosted by Github.

Next you have two options to create your working directory:

  • Clone your repository
  • Build your directory, initialize a local repository, link to your remote repository

Option 1: Clone your repository

Clone

Command: git clone ssh/https_link_to_github_repository

The act of cloning takes three major actions:

  1. Cloning creates a working directory on your local machine linked to your local repository. Migrate your project directory to this new directory.
  2. Cloning creates a local repository on your machine that is a copy (clone) of the one you just made on Github. You generally won’t be viewing the content of the individual files while they are on your local repository.
  3. Cloning creates a link to your local repository and the remote repository created earlier. You’ll be able to update the remote repository later on with certain commands.


Option 2: Initialize a local repository, link to your remote repository

You currently have a project directory. Let’s use it to create a local repository, then link it to the remote repository. You won’t need to move your project directory with this option, it will simply become your working directory.

Initialize

Command: git init

First, make sure you are in your project directory. Then run git init. This creates a local repository on your local machine linked to your working directory. Your project directory has now become your working directory and is linked to this new repository.

Add Remote

Command: git remote add origin ssh/https_link_to_github_repository

This links your local repository to your Github remote repository.

Getting Samantha Involved

KABOOM, two repositories have now been created. A remote repository on your Github profile and a local repository on your machine.

For fun, lets create one more. Let’s say your coworker Samantha wants to do a sick collab. She will simply use option 1 to clone your Github repository. Don’t forget to add Samantha as a collaborator on Github. That is, if you trust Samantha. The employee eating other people’s food from the fridge still hasn’t been caught…

Now we have three repositories:

  1. A remote repository on your Github profile.
  2. A local repository on your machine.
  3. A local repository on Samantha’s machine.

Add it, Commit it, Push it,

Psst. Pssssst. PSSSSST. HEY WAKE UP. This is the important bit. Save your files and get ready to throw them up to the remote.

Let’s get right into it, these are the commands you are going to use to sync your local repository to the remote repository. Note: all git commands are prepended with git.

  1. Add

Command: git add file_name or git add .

git add prepares your files by staging them. In this case staging them simply means collecting them into a staging area. You can add files one by one by file name or all files in the working directory with git add .

The staging area is full of some files you want to send on to bigger and better things.

2. Commit

Command: git commit -m "summarize your changes"

You’ve set the stage, literally. You’ve got your updated files all hyped and ready. Now commit them. Committing moves those bad boys to your local repository.

Congrats, you’re in a committed relationship with Github. You have now updated your local repository.

3. Push

Command: git push

Send em on up! You can specify the specific remote repository you want to push by appending it’s name to git push, otherwise it will use your current remote repository.

Pushing files updates the remote repository with your changed files. Congrats! You just updated your repository and the remote repository! These three steps are Github at it’s core and you will be using them often.

Pull it, Fetch it, Branch it

4. Fetch

Command: git fetch

CRSHKKBRRTCKK ERROR ERROR

You may get an error while attempting to push. This error may say something along the lines of MERGE CONFLICT.

Samantha sent up some files before you and Github can’t accept your files just yet. No worries, let’s fetch them and fix this up.

git fetch takes down files from the remote repository to your local repository. However, you still can’t see them in your working directory. Time to merge them into your working directory.

5. Merge

Command: git merge

Merges files from your local repository to your working directory.

When you run git merge, more than likely your text editor will prompt you to fix the conflicts that exist between your files and the ones you just pulled down.

Once you fixed these up, repeat steps 1 through 3. For this second commit, you can typically just use the command git commit -m “merged”.

4. Pull (Fetch + Merge)

Command: git pull

Pulling is the act of bringing down those files from the remote repository and merging them to your working directory. In this case these are the updated files Samantha already pushed to the remote repository.

Pulling is the combination of two git commands git fetch and git merge.

5. Branch

Command: git branch branch_name and git checkout branch_name

Let’s say you want to try out some funky stuff on your project, but while retaining your current version. The best way to do this is to create a new directory. The directory you already have is pre-designated the master. You can see this with the command git branch.

A branch is a new directory linked to your master that you have already created. Creating a new branch allows you to edit your project without yet making changes to your master.

git branch branch_name will create a new branch. git checkout branch_name will switch to working in that directory. Now when you do git add and git commit in steps 1 and 2 they will be only affecting this new directory.

If you want to merge those changes in your new directory/branch with your master, run the following sequence:

git checkout master switch back to your master branch

git merge branch_name merge that branch into your master

IT’S A PARTY IT’S A PARTY

You’ve done it! Now you and Samantha can knock this out as a team and have extra time to celebrate!

Top comments (0)