Making Git, Lit (A Fun Intro to Version Control)
Tags: git, github, versioncontrol, bash
Published: 2019-04-08 16:56:27

<p><em>Note: All referenced commands are using Bash on a Mac OS X Terminal. Github is used as an example version control platform.</em></p><p>Project. Project-final. Project-final-FINAL. Project-please-let-this-be-the-last version. Project-spaghetti. Project-done-complete-over-submit.</p><p>Look familiar?<strong> </strong>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.</p><p><em>You need version control in your life</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n4VBS54zK4MxTbgs6sAJbA.jpeg" /></figure><blockquote><em>“Hey Max? Can you send me the updated file?”</em></blockquote><blockquote><em>“I don’t have it, Stephanie is currently working on it”</em></blockquote><blockquote><em>“Hey Megan, I just sent you my changes”</em></blockquote><blockquote><em>“You did? Max just told me, Stephanie is supposed to be working on the file”</em></blockquote><p>Look familiar? The decrease in productivity, the continual cycle of communication and manual changes.</p><p><em>You need version control in your life.</em></p><h4><strong>Enter Github</strong></h4><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*XSkuIVMupYV1Mc24sBgGNw.jpeg" /></figure><h3>Quick Definitions</h3><p>Let’s run through a set of definitions. Don’t skip these!</p><p><strong>Directory: </strong>A folder</p><p><strong>Project</strong>: For this post, refers to a set of files nested in directories.</p><p><strong>Repository</strong>: A place where a project are stored (hint: it can be stored in multiple places). Often referred to as a repo.</p><p><strong>Local</strong>: Your machine, your computer, and only your computer.</p><p><strong>Remote</strong>: A place that is not your machine.</p><p><strong>Branch: </strong>A repository linked to the master repository. Can be local or remote.</p><p><strong>Working directory: </strong>Your workspace. Where you access and edit your project directories and files once you have created a local repository.</p><h3>Where Da Repos At?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OP6RC92-DjB3IZhpvzII2w.jpeg" /></figure><p>Ok so you’ve got a project. Do you have a project? No? Well get on it!</p><h4>Build Your Project Directory</h4><p>This is your project directory, add your files, your code, your pictures, your memes, your last will and testament, and whatever else seems prudent.</p><p>Alright, you’ve got your project ready to go. Now what? Start a new repository on Github!</p><h4>Create Github Remote Repository</h4><p>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.</p><p>Next you have two options to create your working directory:</p><ul><li>Clone your repository</li><li>Build your directory, initialize a local repository, link to your remote repository</li></ul><h4><strong>Option 1: Clone your repository</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FJAqpNXfAN_aW0BbVt6lYw.jpeg" /></figure><p><strong>Clone</strong></p><p>Command: `git clone ssh/https_link_to_github_repository`</p><p><strong>The act of cloning takes three major actions:</strong></p><ol><li>Cloning creates a working directory on your local machine linked to your local repository. Migrate your project directory to this new directory.</li><li>Cloning creates a local repository on your machine that is a copy (clone) of the one you just made on Github. <em>You generally won’t be viewing the content of the individual files while they are on your local repository.</em></li><li>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.</li></ol><h4><strong>Option 2: </strong>Initialize a local repository, link to your remote repository</h4><p>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.</p><p><strong>Initialize</strong></p><p>Command: `git init`</p><p>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.</p><p><strong>Add Remote</strong></p><p>Command: `git remote add origin ssh/https_link_to_github_repository`</p><p>This links your local repository to your Github remote repository.</p><h4><strong>Getting Samantha Involved</strong></h4><p>KABOOM, two repositories have now been created. A remote repository on your Github profile and a local repository on your machine.</p><p>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. <em>That is, if you trust Samantha. The employee eating other people’s food from the fridge still hasn’t been caught…</em></p><p><strong>Now we have three repositories:</strong></p><ol><li>A remote repository on your Github profile.</li><li>A local repository on your machine.</li><li>A local repository on Samantha’s machine.</li></ol><h3>Add it, Commit it, Push it,</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*eqQjdyCde34Nv47MohUKAw.jpeg" /></figure><p>Psst. Pssssst. PSSSSST. HEY WAKE UP. This is the important bit. Save your files and get ready to throw them up to the remote.</p><p>Let’s get right into it, these are the commands you are going to use to sync your local repository to the remote repository. <em>Note: all git commands are prepended with </em><em>`git`.</em></p><ol><li><strong>Add</strong></li></ol><p>Command: `git add file_name or git add .`</p><p>`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 .`</p><p>The staging area is full of some files you want to send on to bigger and better things.</p><p>2. <strong>Commit</strong></p><p>Command: `git commit -m &quot;summarize your changes&quot`;</p><p>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.</p><p>Congrats, you’re in a committed relationship with Github. You have now updated your local repository.</p><p>3. <strong>Push</strong></p><p>Command: `git push`</p><p>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.</p><p>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.</p><h3>Pull it, Fetch it, Branch it</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sefktrimnSHSWaOg" /></figure><p><strong>4. Fetch</strong></p><p>Command: `git fetch`</p><p>CRSHKKBRRTCKK ERROR ERROR</p><p>You may get an error while attempting to push. This error may say something along the lines of MERGE CONFLICT.</p><p>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.</p><p>`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.</p><p><strong>5. Merge</strong></p><p>Command: `git merge`</p><p>Merges files from your local repository to your working directory.</p><p>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.</p><p>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”`.</p><p><strong>4. Pull (Fetch + Merge)</strong></p><p>Command: `git pull`</p><p>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.</p><p>Pulling is the combination of two git commands `git fetch` and `git merge`.</p><p><strong>5. Branch</strong></p><p>Command: git branch branch_name and `git checkout branch_name`</p><p>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`.</p><p>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.</p><p>`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.</p><p>If you want to merge those changes in your new directory/branch with your master, run the following sequence:</p><p>git checkout master switch back to your master branch</p><p>`git merge branch_name` merge that branch into your master</p><h3><strong>IT’S A PARTY IT’S A PARTY</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JYtSZXJ-S3zdvmxn" /></figure><p>You’ve done it! Now you and Samantha can knock this out as a team and have extra time to celebrate!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f657d0c9911" width="1" height="1">