Git 101 - How to Create Your First GitHub Repository
Tags: github, git, tutorial, beginners
Published: 2022-03-10 18:09:04

## Table of Contents
- [Introduction](#introduction)
- [What are we trying to do](#what-are-we-trying-to-do)
- [Step 1 Create Repo in Github Website](#step-1-create-repo-in-github-website)
- [Step 2 Copy the Repo Url from Address Bar](#step-2-copy-the-repo-url-from-address-bar)
- [Step 3 Create a directory with similar to repo name](#step-3-create-a-directory-with-similar-to-repo-name)
- [Step 4 Go inside the directory](#step-4-go-inside-the-directory)
- [Step 5 Run Git init command to initialize directory](#step-5-run-git-init-command-to-initialize-directory)
- [Step 6 Create branch main](#step-6-create-branch-main)
- [Step 7 Now add the repo to remote Git URL](#step-7-now-add-the-repo-to-remote-git-url)
- [Step 8 Create a file](#step-8-create-a-file)
- [Step 9 Check repo changes using git status](#step-9-check-repo-changes-using-git-status)
- [Step 10 Add the files to local staging](#step-10-add-the-files-to-local-staging)
- [Step 11 Commit the staged files](#step-11-commit-the-staged-files)
- [Step 12 Push the repo changes to Git](#step-12-push-the-repo-changes-to-git)
- [Next steps](#next-steps)
- [Check repo changes using git status](#check-repo-changes-using-git-status)
- [Add the files to git staging](#add-the-files-to-git-staging)
- [Commit the staged files](#commit-the-staged-files)
- [Push the repo changes to Git](#push-the-repo-changes-to-git)
- [Conclusion](#conclusion)


## Introduction
- This article is a Git-101 starter tutorial. I'm trying to provide the steps to create a repo locally and Git commands to initialize and push the repo changes.
- I've used GitHub for demo purpose, and it is similar to all Git based source code management tools
- Also this article inspired by the [Git Cheat Sheet](https://training.github.com/downloads/github-git-cheat-sheet/) section `Create Repositories`

## What are we trying to do
- A new repository can either be created locally, or an existing repository can be cloned. In case a repository was created locally, user has to initialize and have to push it to GitHub afterwards
- Use Case for this article is,
  - A GitHub user having a set of files in a directory (`test_repo`) and needs to be check-in into Git 
  - User wants to know, how to create repo in github website and Git commands associated with initializing the local repo and pushing the changes to github

## Step 1 Create Repo in Github Website
- Login into Github
- Choose create repo 
- Fill-in the details in required field `Repository name` and optional field `Description`
- Choose your repo is `public` or `private`
- Then click `Create Repository` 
![Create Repo](https://github.com/chefgs/test_repo/blob/main/images/git-repo-create.png?raw=true)

## Step 2 Copy the Repo Url from Address Bar
- Copy the Git repo url from browser address bar. It will be used later in Step 8
![Repo created](https://github.com/chefgs/test_repo/blob/main/images/git_repo.png?raw=true)

## Step 3 Create a directory with similar to repo name
```
mkdir test_repo
```

## Step 4 Go inside the directory
```
cd test_repo
```
## Step 5 Run Git init command to initialize directory
- We need to download and install Git CLI tool by following Git Installation](https://git-scm.com/downloads) instruction of specific operating system 
- The `git init` command turns an existing directory into a new Git repository inside the folder you are running this command.
```
git init
```

## Step 6 Create branch main
- After the [initiatives](https://inclusivenaming.org/word-lists/tier-1/) of `Inclusive naming convention`, it is recommended to use branch name as `main`, instead of `master`
- So the below command renames the branch master to main
```
git branch -m main
```

## Step 7 Now add the repo to remote Git URL
- After using the git init command, link the local repository to an empty GitHub repository using the following command:
```
git remote add origin https://github.com/chefgs/test_repo.git
```

## Step 8 Create a file
- Create a file named `README.md`
```
echo "## test_repo readme" > README.md
```

## Step 9 Check repo changes using git status
```
git status
```
## Step 10 Add the files to local staging
- Stages the file in preparation for versioning
```
git add .
```

## Step 11 Commit the staged files
- Records staged files permanently in version history
```
git commit -m "first commit"
```

## Step 12 Push the repo changes to Git
- `git push` Uploads all local branch commits to GitHub
- The current branch `main` has no upstream branch in GitHub, since we initialized a local directory as repo, so use below command for the first time to push the current branch changes and set the remote branch as upstream, 

```
git push --set-upstream origin main
```

- We can now see the repo updated with checked-in files
![Repo Updated](https://github.com/chefgs/test_repo/blob/main/images/repo-updated.png?raw=true)

## Next steps
- Once changes are pushed for the first time, follow-up changes can be pushed by running below commands

### Check repo changes using git status
```
git status
```

### Add the files to git staging
```
git add .
```

### Commit the staged files
```
git commit -m "new commit message"
```

### Push the repo changes to Git
```
git push origin main
```

## Conclusion
Here is the link to the [repo](https://github.com/chefgs/test_repo), created as part this article.
In this article we have discussed about,
1. Creating a repo in Github
2. Initializing, adding files and pushing changes into GitHub

Looking forward  to write more such tutorials regarding Git commands as part Git tutorial series

## Follow me and share your thoughts,
- [GitHub](https://github.com/chefgs)
- [LinkedIn](https://www.linkedin.com/in/saravanan-gnanaguru-1941a919/)
- [Twitter](https://twitter.com/saransid)
- [gsaravanan.dev](https://gsaravanan.dev)