Git Introduction

Git can be complicated for a lot of people. Today, I’m going to break down Git and the commands to dispel the confusion. Furthermore, this is just a simple sheet to refer to how the commands work and when to use them. Finally, Git is not Github. If you’d like to download git, do so here: To create a Github account head here:

This tutorial is for understanding the commands, and for referencing them easily. This won’t help you get GitHub or git set up on your computer.

How Git Works

Git is like a time machine for source code. Now, like any time machine, it allows you to go to any point in time. In Git, these points in time are called commits. Commits exist on a timeline (a branch) within a repository which holds all the information for branches and commits. The next important concept to understand is a branch. A branch is a different version of the timeline. Each timeline is a different path the code has gone down. These are the important concepts, now let’s learn the controls (commands) to operate the machine.

Git Commands

Git commands are used to perform specific tasks on the timeline. The most common commands are: init, add, rm, commit, push, pull, clone, checkout, remote, and merge. Let’s go over each one.

Git Init

“git init” creates a local repository for your current directory (folder) to track all your files. This repository will monitor all the commits and timelines you create within that project locally. This means it’s only available on your machine. Here’s an example:

This created a new local repository in my “GitTut” folder.

Git Status

“git status” this checks the status of your branch on local repository compared to the remote. Here’s an example from GitTut folder:

As you can see in my folder, there are three files that are untracked; this means they’re not a part of my timeline. Use git status often to check the status of your branch. Now, to add files is where “git add” comes in handy.

Git Add

“git add” add files and folders to your commit. This means they are being “staged” for creating a new point on the timeline. Here’s an example:

In this example, I added “File1.txt” to be staged for commit. This means that File1.txt will be added to my commit when I create one. Also, notice you can unstage files with the “git rm” command if you make a mistake.

Git Rm

“git rm” is used to remove files from the tracking process. This means that they won’t be added to your commit or your new point on the timeline. Here’s an example removing one of the files I staged using the add command:

Now, once you add your files, you’re ready to commit.

Git Commit

“git commit” creates that point on the timeline with a message describing the commit. The best version of git commit to use is: ‘git commit -m “Your Description Here”‘. This makes writing your message easy. Here’s an example of creating a commit and the subsequent “git status”:

Once you’re done committing, you want to push your changes/ commit. This is where you use “git push”

Git Push

“git push” takes your local (on your machine/computer) changes and sends them to a remote repository’s (A repository on Github for example) branch to modify changes. Here’s an example:

As you can see, it didn’t work; we need to add a remote to store our files remotely. This is where we should use the “git remote” command.

Git Remote

“git remote” is often used to set the remote repository. Means you’re setting the remote location to push and pull your changes from. Note, for this you’ll need to have a remote repository setup on Github, Bitbucket, Gitlab, etc. Here’s an example of setting up the remote, and pushing correctly:

Git Pull

“git pull” takes the changes/timeline from the remote and pulls it onto your machine’s local timeline(branch). Always make it a habit of pulling before pushing, or else you’ll deal with the dreaded merge conflicts (if two people work on the same file). Here’s an example of pulling:

In this example, my remote master branch and my local master branch are up-to-date. This means there are no commits ahead of mine, but if there were, this would add those changes to my local repository branch.

Now, what if you wanted to download from a repository? Clone it!

Git Clone

“git clone” clones or downloads a remote repository for you to play with on your local machine. It copies everything from the remote including the branches (timelines). You can traverse each timeline from start to finish here and make changes like you would with your own repository.

Here’s an example of cloning a repository of my own:

Here we clone the repository, and you can see it created the folder for the repository in my “TutorialProjects” directory. Very useful command, especially if you want to transfer your projects to another computer, or look at someone else’s project.

Git Checkout

“git checkout” has two key functions. Switching to another branch (timeline) and creating a new branch based on a timeline. For example, you started a new project, but want to try out ideas? Create a new timeline based on your current progress with git checkout. Here’s an example of creating a new branch and switching to a branch respectively.

In this example, we created a new branch dev and switched to it using the checkout command. Now, let’s switch to another branch.

Now, we switched back to the master branch we started on. “git checkout” is for switching between branches or creating new ones.

Git Merge

“git merge” combines two branches together; you merge two timelines into one. For example, you’ve made changes on your “dev” branch and think they’re good enough to move to master? You’d do “git merge dev” on the master branch to add those changes to master. Here’s an example of making changes to dev and merging them to master:

In this example, we switched to our dev branch, used the add command to add File2, made a new commit ( a new point on the timeline), switched to master, and finally merged our changes. This combines a lot of the commands we used before into one process. Now, by doing this, we have merged the master and dev branch timeline.

Git Best Practices To Avoid HeadAches

  • Pull before pushing.
  • Don’t make changes directly on master, make a new branch to code on.
  • Make a new branch if you want to try something new with your project.
  • Always commit and push your work before you’re done.
  • Make your commit messages informative and match the work you’ve done.
  • Merge your changes from the branch you’re working on to another if you like them.
  • Use git status often to check where you are and what your branch looks like.

Now, I hope this post helped, and if not, tell me how I could improve it below!

%d bloggers like this: