DEV Community 👩‍💻👨‍💻

Cover image for Git 101 - How to Create Your First GitHub Repository
Saravanan Gnanaguru
Saravanan Gnanaguru

Posted on • Updated on

Git 101 - How to Create Your First GitHub Repository

Table of Contents

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 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

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

Step 3 Create a directory with similar to repo name

mkdir test_repo
Enter fullscreen mode Exit fullscreen mode

Step 4 Go inside the directory

cd test_repo
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 6 Create branch main

  • After the initiatives 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 8 Create a file

  • Create a file named README.md
echo "## test_repo readme" > README.md
Enter fullscreen mode Exit fullscreen mode

Step 9 Check repo changes using git status

git status
Enter fullscreen mode Exit fullscreen mode

Step 10 Add the files to local staging

  • Stages the file in preparation for versioning
git add .
Enter fullscreen mode Exit fullscreen mode

Step 11 Commit the staged files

  • Records staged files permanently in version history
git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • We can now see the repo updated with checked-in files Repo Updated

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
Enter fullscreen mode Exit fullscreen mode

Add the files to git staging

git add .
Enter fullscreen mode Exit fullscreen mode

Commit the staged files

git commit -m "new commit message"
Enter fullscreen mode Exit fullscreen mode

Push the repo changes to Git

git push origin main
Enter fullscreen mode Exit fullscreen mode

Conclusion

Here is the link to the 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,

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.