PUSHING A LOCAL FOLDER TO GITHUB USING GIT AND SSH.

PUSHING A LOCAL FOLDER TO GITHUB USING GIT AND SSH.

# firstgithubproject# pushinglocaldirectory
PUSHING A LOCAL FOLDER TO GITHUB USING GIT AND SSH.Gee Gloria

Introduction As a beginner in the data industry it is paramount to understand that...

Introduction

As a beginner in the data industry it is paramount to understand that projects are often created and stored locally on a computer before being shared or backed up online. Therefore, the first step into the journey of a data specialist becomes to learn the simplest yet most important thing: How to transfer these folders or files in your local PC into GitHub. This article will show you how to transfer locally created directory to GitHub using Git and a SSH key.
Git Bash commonly known as Git is a tool installed on your computer that tracks and manages changes made to your projects' files and folders. GitHub on the other hand, is an online platform where you can store your Git projects. The platform enables you to access them, share them, and collaborate on them from anywhere. Meanwhile, an SSH key is a secure digital key that allows your computer to connect to your GitHub account without repeatedly entering a password.
In this article I will show you how to create or access your local folders and files using git and transfer them to a GitHub platform with the use of a SSH key.

Overview

The article will contain:

  • How to create a local Directory

  • Initializing Git

  • Adding Local Directories to the Staging Area

  • Committing the Project

  • Connecting the Local Repository to a Remote Repository

  • Understanding the SSH KEY

  • Pushing the Project to GitHub Using a SSH key

A. Creating a Local Directory

Steps to Creating a Local Directory.

NB: We will use Sales Project as an example of our project directory.

The first step in creating your local project directory is to check your current working directory. This is done by opening the Git terminal and writing the command:

pwd

This command simply asks, where am I right now?

After knowing where you are, you can now create a project directory in the location you are currently in or open a new location you would like the directory to appear.

Lets say we want to create a directory called Sales Project, and we would like it to be located in the Desktop directory. What we require is to open the desktop directory and create our sales project directory there.
To open the Desktop Directory we use a command cd which allows us to enter into a directory of our choice as long as the directory exists in our local PC. So our code would be:

cd Desktop

Now, having opened the desktop folder, we want to create our sales project directory. To create a folder we are required to use the command mkdir.The code appears as:

mkdir "sales Project"

Now when you check your Desktop, you should be able to see a folder called Sales Project.

You can also check for the presence of the folder by using ls under the directory Desktop. ls is simply a command we use to show the files and folders in your current location.

After creating our sales project directory we are required to create a README.md file. A README.md file is a document that is created using markdown language and is used to explain what the project is about, the tools used, how it works, and other important information concerning the project. It helps users understand a project without having to read the code.

To create it we use the command touch , the code appears as:

touch README.md

Afterwards we can create a folder That will contain all the data used in the project in question. For example our data could entail excel files, screenshots and pictures. Lets say our folder is called DATA, then we will have to create it by coding:

mkdir DATA

With these steps we would have successfully created our folder called Sales Project, which contains a README.md file and a folder where we can place the data we are working on called DATA. So in summary the codes of creating this directory in our PC would be:

pwd
cd Desktop
mkdir "Sales Project"
cd Sales Project
touch README.md
mkdir DATA

Enter fullscreen mode Exit fullscreen mode

B. Initializing Git

When we say Git initialization we refer to the process of enabling Git to start tracking a particular folder as a Git repository.
In our example, when we initialize git we are enabling the ordinary directory we called Sales Project to be turned into a git repository where changes made to it can be tracked.
To enable this we use the command:

git init

c. Adding Files to the Staging Area

After initializing, we are required to tell Git which changes we want to include in our next commit. For this to be possible we use the command:

git add

Now depending on the changes in the directory you want to stage you will use different ways to initialize. For instance, if you want to add everything in the directory and its subdirectories to the staging area we use the command:

git add .

if you want to select specific changes then you select the specific areas you want to add to the staging area. For example, we want to add the changes in the README.md file alone we will initiate a:

git add README.md

if we need changes committed in the DATA folder only we will have:

git add DATA/

D. Committing the Project

A commit refers to a saved checkpoint of the project. It records the changes that you had prepared in the staging area.
The command used to commit is:

git commit -m " what you are committing"

The message inside the quotation marks describes what the commit contains and helps in understanding the project later.
taking the example of our project we could write

git commit -m "Sales project"

E. Connecting the Local Repository

After committing we are now required to create a repository on GitHub. GitHub, acts as a remote location where we can store and access the project online.

The remote repository can be given a name , and a description, and it will generate a SSH key URL which will be used in connecting the local repository to GitHub. The command used will be:

git remote add origin

Here origin means the name given to GitHub repository connected to the local project. If we use the example of our project the command would appear as:

git remote add origin git@github.com:username/repository.git

with the URL written after origin being an SSH remote URL that connects the local Git repository to the GitHub repository.

Understanding the SSH KEY

To connect your local Git repository to GitHub securely, you require a key called the secure shell key or commonly called the SSH key.
It is generated and obtained after you have configured the Git Bash, the key is generated using the command:

ssh-keygen -t ed25519 -C "Your email@example.com"

Consequently, Git bash will ask you to enter a passphrase twice for the purpose of confirmation. Then it will generate a private key and a public key, which helps us to connect the local Git repository to GitHub securely.
For safety purposes we will use the public key written with .pub which stands for public.

The command
cat ~/.ssh/id_ed25519.pub|clip

shows us how to copy and clip the key.

After acquiring this public key we are required to go to the GitHub account, and open settings then click SSH key, then New SSH Key and add the SSH key.
Afterwards return to Git Bash and test the connection using the command

ssh -T git@github.com

Once the connection is successful, we now know we will be able to connect the local repository with the remote repository using the command;

git remote add origin git@github.com:username/repository.git

Allowing GitHub to verify that the computer is authorized to access the repository.

F. Pushing the Project to GitHub

After connecting the project to GitHub, check what your branch is called using the command:

git branch

A branch in this context refers to a separate version of Git project that allows you to make changes without directly affecting the main branch. Meanwhile, the main branch is the primary branch of a Git repository which contains the main, stable version of your project.

Its paramount that the local branch and remote branch you are pushing to match. In the past the branch was commonly referred to as masterbut with time the modern GitHub default branch evolved and begun to be called main for new GitHub repositories.

So for instance if your default branch is master and your remote branch was created as main you will be required to change it to main and hence, use the command

git branch -M main

This changes your branch to main.

Now after dealing with the branch, the next step is to push the project and it is done using a command

git push -u origin main
or

git push -u origin master

depending with what your remote repository is called. After pushing your project successfully, the repository will appear in your GitHub account marking the end of the cycle of creating and pushing a directory to become a repository from the local PC to a remote GitHub repository.

This image shows the steps of pushing a local directory to GitHub using git.

Now lets have a code example showing the flow of events in the process of pushing a locally created directory to github usiing the SSH key:

example
Lets consider that we already set up our SSH key and we want to push a locally generated project from the git bash to GitHub using an SSH key.
While using our example of Sales Project as our directory, this is how the flow of creating a directory and pushing it would look like:


pwd 
cd Desktop
mkdir "Sales Project"
cd "Sales Project"
touch README.md
mkdir DATA
cd .. ##this allows you to go a step back from current directory.
     ## this one for instance will take you back to README.md
echo "sales" >README.md # enables writing text into a file
cat README.md # displays contents of a file in the terminal
nano README.md # enables editing of README.md file
git init
git status ##Helps check what is happening in your git project
git add .  
git commit -m "Sales Project"
git remote add origin git@github.com:username/repository.git                                                   
git branch main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion, moving a project from a local computer to GitHub involves a clear sequence of steps which involve creating and initializing the repository, staging and committing changes, establishing an SSH connection, linking the local repository to a remote repository, and pushing the committed work to GitHub. Each stage serving a specific purpose in maintaining a reliable version-controlled project. Understanding this workflow provides a practical foundation for managing projects, tracking changes, and collaborating through Git and GitHub.