Soapy water bubbles forming a net
Photo by Lanju Fotografie on Unsplash

Getting Started with Git

Shital Giri
3 min readAug 23, 2020

--

Ever worked on a project and wanted to try something but dreaded messing up what you already had? Ever wish you had a history of all your changes so you can see what you were thinking the time you added that cryptic hack? Well worry no more. That’s what Git was made for and that’s what this guide was made for as well!

I’m using a Mac for this guide. If you’re using Windows, you’ll need to set yourself up with a terminal like Commander or Hyper to follow along.

First, let’s make sure you have git installed. Macs should have Git installed by default. You can check if you have it by opening up your terminal and entering the command git --version. This will print out the version of Git you have installed. If it doesn’t, then you probably don’t have it installed, and will need to install it from https://git-scm.com/. Now that we know we have git, we can get started!

Let’s create our first repository! But wait, what is a repository you ask? It’s a way for Git to track what you have worked on. Think of it as a database for all the changes that you will be making in your project. Now, let’s get started! Create a directory that you will be using for your project. I’ll make mine in ~/code/git-starter. In your terminal, do the following

~ > cd code
~/code > mkdir git-starter
~/code/git-starter > cd git-starter

Now that you have your project directory, let’s create the repository.

~/code/git-starter ❯ git init
Initialized empty Git repository in /Users/shital/code/git-starter/.git/

And, that’s it! You now have a Git repository! If you don’t believe me, you can use git status to see. You’ll see that your directory now has a Git repository.

~/code/git-starter ❯ git status
On branch master
No commits yetnothing to commit (create/copy files and use "git add" to track)

Before we proceed with the next steps, you should probably add a .gitignore file at this point. When you’re working on projects, you will have files that you don’t want checked into the repository. For example, you may have design documents or secret files that you don’t really want other people to see. That is where .gitignore will help us out. It’s a file that you place in your repository to tell Git to not bother with the specified files. Here’s a sample that I’ve placed in my project.

.DS_Store
node_modules
logs
*.zip

Now, let’s see how Git sees this change.

~/code/git-starter ❯ git status
On branch master
No commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)

As you can see, Git sees that you’ve added .gitignore to your project folder. Now, lets do as Git recommends add this file to staging, which lets Git know that we want it around. You can do this by adding the file.

~/code/git-starter ❯ git add .gitignore

We’re not done yet though. Once you’ve staged a file (or files), you have to commit it so Git knows you’re serious about saving it. You do this by, you guessed it, git commit

~/code/git-starter ❯ git commit -m "Adding .gitignore"
[master (root-commit) 3fa3613] Adding .gitignore
1 file changed, 4 insertions(+)
create mode 100644 .gitignore

Note how I added the -m "Adding .gitignore" parameter. This is a shortcut for specifying the commit message so that you aren’t prompted by Git to enter one.

Now, Git will remember what you’ve changed! Let’s make more changes and see how to connect Git to GitHub in the next part of this series!

--

--