DEV Community

Cover image for Creating a New Git Repository for the Absolute Beginner
Alec DuBois
Alec DuBois

Posted on • Updated on

Creating a New Git Repository for the Absolute Beginner

Exactly what it says on the tin. Here's a walk-through for starting a new Git repository locally using Git and GitHub. 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

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 GitHub from here on out, but there are other services out there.

Before we do anything, we need to install Git! You can find it with links to downloads here. Go on, I'll wait!

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:

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.

Creating the Remote Repository

Here's the official GitHub documentation on creating a remote repository.

(It isn't hard - log in to GitHub 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:

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

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:

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 Markdown 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 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: Git Branches in a Nutshell

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.

Top comments (1)

Collapse
 
brad_beggs profile image
Brad Beggs

I forgot that git is just a way to track the version of the software. I equate git to just GitHub. Right. Thanks for the reminder. :)