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
- Before You Start
- Creating the Local Repository
- Creating the Remote Repository
- Commit and Push
- A Note on Branches
- Sum Up
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.
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.
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.
(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.
git remote add origin <your-repository-link>
Wait, did it work?
There's no response if this worked.
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
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:
Now for Git:
git add .
Before you can send anything you need to
commit your changes.
git commit -m 'Added README.md'
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 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
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.
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
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...
If you made it to the end here, thanks for reading. Leave a comment if you've found this useful, it's good karma.