How to Create GitHub Repository from Local Directory?

Author: Kamile Yagci

Objective

In this post, I will explain how to create a GitHub Repository from an existing Local directory/folder in your computer. Then, I will procide few more tips which might be helpful to you in the process.

Content

  • Step 1: Create a local git repository
  • Step 2: Create a new blank repository on GitHub
  • Step 3: Push local git repository to GitHub
  • More Tips
    • Create .gitignore
    • SSH or HTTPS?
    • Delete Local Git Repository
    • Delete GitHub Repository
    • Update GitHub Repository

Step 1: Initialize a local git repository

  1. On terminal, go into your local project directory which you plan to create a git repository from.

    cd project_test

    Note: Use your own the directory path.

  2. Initialize the local git repository.

    git init

  3. (Optional) Create ‘.gitignore’ in your project directory. This file contains the list of files and directories to be excluded, not tracked and not uploaded, in the git repository. Even though it is not a required component, I highly encourage you to create one. See ‘More Tips’ section below to learn how to create .gitignore file.

  4. Add files to the repository.

    git add *

  5. Commit changes.

    git commit -am ‘Initial commit’

Step 2: Create a new blank repository on GitHub

  1. Go to your GitHub Respository Page.

  2. Click on the ‘NEW’ button on the upper left of the page.

  3. On the ‘Create a new repository page’:
    • Enter a ‘Repository name’. I usually use the same name as the local directory, but it is not required.
    • Choose ‘Public’.
    • There is option ‘add a README file’ and ‘gitignore’ in the initial setup. Keep them unclicked. Since I will create the repository from local directory, I will not use auto created ones.
  4. The new blank repository is created and ‘Quick Setup’ page opens.
    • Clik on ‘HTTPS’ button on the top of the screen. The repository adress starting with ‘https://github.com/…..’ will be displayed on the right of ‘HTTPS’ button.
    • In Step 3, you will use the second set of instructions under ‘…or push an existing repository from the command line’.

Step 3: Push local git repository to GitHub

  1. In terminal window, go into or stay in your local project directory. You created local git repository for this directory in Step 1.

  2. Connect your local repository to GitHub repository,

    git remote add origin https://github.com/kamileyagci/project_test.git

    Note: You need to use the address ‘https://github.com/…..’ displayed on your own GitHub repository setup page. The whole command line is actually provided on the setup page.

  3. Create the main branch

    git branch -M main

  4. Push the local repository to GitHub

    git push -u origin main

  5. Process is complete. Now your GitHub repository page displays the files in your local project directory.

More Tips

Create .gitignore

The ‘.gitignore’ is a text file that contains the list of files and directories to be excluded, not tracked and not uploaded, in the git repository. You can use it to stop tracking some hidden files or exclude uploading the large files. In GitHub Repository, the maximum size limit per file is 100 MB.

Example .gitnore file content:


#Ignore the following files

.ipynb_checkpoints

.DS_Store

pycache

*.db

*.pytest_cache

#*.log

#*.jpeg

#ignore ALL files in ANY directory named

#chest_xray_tmp/


Note: You may add/remove and comment/uncomment in this file depending on your need.

HTTPS or SSH?

HTTPS and SSH are secure communication / file transfer protocols.

GitHub gives you an option to choose which one to use in your repositories. The GitHub setup page, displayed just after repository created, has two button ‘HTTPS’ and ‘SSH’.

The main difference between them is the GitHub repository address. You are using this address to connect the local repo to GitHub repo.

HTTPS: https://github.com/kamileyagci/project_test.git

SSH: git@github.com:kamileyagci/project_test.git

In my instructions, I preferred the HTTPS because it is easy to use in setup.

SSH is more secure protocol and requires a SSH key. The supported SSH public keys for GitHub Enterprise Server are ‘id_rsa.pub’, ‘id_ecdsa.pub’, ‘id_ed25519.pub’. You can check your SSH keys as follows:

ls -al ~/.ssh

Delete Local Git Repository

It is very easy to delete the local Git Repository. There are two ways.

  1. Delete local git repository while keeping the local project directory. You just need remove the ‘.git’ in the local repository. In terminal window, go into project directory and type:

    rm -r .git

  2. Delete local git repository together with the local project directory. You just need to remove whole project directory.

    rm -r project_test

Delete GitHub Repository

  1. Got to GitHub Repository, the one you plan to delete.

  2. Click on the ‘Settings’ on top menu.

  3. Click on the ‘Delete’ repository button at the bottom of the Settings page.

Update GitHub Repository

As you modify your local project folder, you need update the GitHub Repsoitory.

  1. Add new files and updates the existing files

    git add -A

  2. Commit the changes to git repository

    git commit -m ‘update’

    (Use it always after ‘git add’)

    OR

    git commit -am ‘update’

    (Updates existing files and do commit. You can use this command without ‘git add’, only if there is no new file added.)

  3. Push to GitHub.

    git push

Written on April 12, 2022