Creating a New Git Repository for the Absolute Beginner
Tags: beginners, git
Published: 2020-08-12 04:57:47

Exactly what it says on the tin. Here's a walk-through for starting a new Git repository locally using <a href='https://git-scm.com/'>Git</a> and <a href='https://www.github.com/'>GitHub</a>. This is intended for beginners, and I've tried to include some explanations to make this less of a monkey-see, monkey-do kind of tutorial.

*Hey: notes for absolute beginners are sometimes included like this*

#### Jump To:
* [Before You Start](#before)
* [Creating the Local Repository](#local)
* [Creating the Remote Repository](#remote)
* [Commit and Push](#commit)
* [A Note on Branches](#branches)
* [Sum Up](#summary) <a name='before'></a>

#### Before You Start

Here, I'm assuming you're familiar with the basics of moving around your terminal. If you aren't, go brush up on that first!

In order to start a local *(on your computer)* Git repository, you'll need an account with a git hosting service. I use <a href='https://www.github.com/'>GitHub</a> from here on out, but there are other services out there.

Before we do anything, we need to install <a href='https://git-scm.com/'>Git</a>! You can find it with links to downloads <a href=https://git-scm.com/downloads/'>here</a>. Go on, I'll wait! <a name='local'></a>

#### Creating the Local Repository

Okay! First, open your terminal and find (or create) a directory *(folder)* on your computer that you'd like to turn into a Git repository. *Use `cd <your/file/path>` to change your directory and `mkdir <name>` to make a directory.*

Do `git init`

You should see something like:

```shell
Initialized empty Git repository in /home/khaleesi/plans/conquer_westeros/.git/
```

That's it! This directory is now a Git repository.

To finish setup, you'll need to create the repository on whatever hosting platform you're using online. <a name='remote'></a>

#### Creating the Remote Repository

<a href='https://docs.github.com/en/github/getting-started-with-github/create-a-repo'>Here's the official GitHub documentation on creating a remote repository.</a>

(It isn't hard - log in to <a href='https://github.com/'>GitHub</a> and click 'New'. Follow the prompts, and you're done!)

But wait! How is the local repository going to know about the remote *(hosted online)* repository? **Great question.**

First, get the HTTPS link for your remote repository. On GitHub, you do this by clicking the green **Code** button on the right of your repository page, making sure it says **Https** and copy that link.

Next, point the local repository at the remote repository.

Do `git remote add origin <your-repository-link>`

*Wait, did it work?*

There's no response if this worked.

Do `git remote -v`

You should see something like:

```shell
origin  https://github.com/khaleesi/conquer_westeros.git (fetch)
origin  https://github.com/khaleesi/conquer_westeros.git (push)
```

And that's it! Your local repository is now pointed at your remote repository.

*`-v` is what's called an option flag - it tells the command `git remote` to print the `verbose` response, which includes the remote link*

*Notice `origin` is the same as what you put in the `git remote add` command - you could name this something else, though most places you go you see it called origin* <a name='commit'></a>

#### Commit and Push

Ready for the next bit? Awesome. Now you're going to **send stuff** from your local repository to the remote repository.

First, you need to add something to push *(send)*. How about a Readme file? Everyone knows writing good documentation makes you more attractive.

You can be fancy and use the terminal for this:

Do `touch README.md`

Now for Git:

Do `git add .`

Before you can send anything you need to `commit` your changes.

Do `git commit -m 'Added README.md'`

Now, `git push -u origin master`

Because the HTTPS link is being used for the remote repository, you'll be asked for your login credentials.

After a moment, you should see something like:

```shell
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/khaleesi/conquer_westeros.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
```

There you have it! You can add files, remove files, change files, and as you go follow this same `git add`, `git commit`, `git push` flow.

*`touch` creates a file and the `.md` extension means it's a <a href='https://www.markdownguide.org/'>Markdown</a> file, like this blog post!*

*`git add .` tells Git to add everything in `.`, the alias for the current directory*

*`-m` is an option flag on `git commit` for 'add a message'*

*`-u` is an option flag on `git push` for `set upstream`, which tells the local repository's `master` branch to track the remote repository's `master` branch - meaning that later you can just do `git push` on its own and it will know which remote and branch to push to*

*Notice `origin` again? This is what we're calling the remote repository. `master` is the main branch of that repository* <a name='branches'></a>

#### A Note on Branches

As a happy side-effect of creating your first repository, you've created your first *branch*. I'm not going to go into depth here, but I'll just touch on what that means.

Remember `master` from `git remote add` and `git push`? Seemed kind of arrogant, right? Well, the master branch is the main branch of a repository. When you're pushing changes to the master branch, it's a pretty big deal!

Most projects start out with just a master branch, and other branches get added and removed later as you and maybe others develop sections of code on its own before merging it with the master *(main)* branch.

Here's a resource if you want to learn more: <a href='https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell'>Git Branches in a Nutshell</a> <a name='summary'></a>

#### Sum Up

In summary, Git is easy:

`git init` to start your directory;

`git remote add <your-remote-repository-link>` to point it at a remote repository;

`git add .` to add *all* files in the local repository to a commit;

`git commit -m '<your-message>'` to stage a commit;

`git push` (using `-u origin master` the first time) to push up to the remote repository, and...

Done!

If you made it to the end here, thanks for reading. Leave a comment if you've found this useful, it's good karma.