DEV Community

Cover image for My First Project: From a Local Folder to GitHub Using Git and SSH
Makori Nyachaki
Makori Nyachaki

Posted on

My First Project: From a Local Folder to GitHub Using Git and SSH

My First Project: From a Local Folder to Git hub Using Git and SSH

GIT

Introduction

Git is an open source version control system used to manage projects. It is fast and efficient to manage projects using git. You can easily track the project version, history, modifications, and many more project activities. It allows for ease of collaboration while working on projects as teams.

Installing Git

Download and install git from Git. Ensure you download for your respective operating system.

Configuring Git

Before using Git effectively, you have to configure your identity by setting up your username and email.

This allows to easily track any changes to your project and the associate that is who made the changes.

To set up your identity, follow the following steps, using Git Bash a git terminal:

Steps

  1. To set your username

    git config --global user.name "YOUR NAME"

In place of YOUR NAME type in your correct user name that will be associated with your github account activity.

  1. To set your email

    git config --global user.email "youemail@example.com"

Note: It is recommended to use your correct email that is associated with your GitHub account.

  1. Rename you GitHub branch to main

    git config --global init.Branch main

  • After setting up your identity you can verify your configurations to confirm your identity for each field set.

To check your:

User name

`git config --global user.name`
Enter fullscreen mode Exit fullscreen mode

Email

`git config --global user.email`
Enter fullscreen mode Exit fullscreen mode

Branch

git config --global init.Branch`
Enter fullscreen mode Exit fullscreen mode

OR

You can use the following command to check all you account details instead:

`git config --global --lists`
Enter fullscreen mode Exit fullscreen mode

SSH

SSH key is a secure credential used to authentication to accessing network resources confidentially without need of a password. The ssh-key contains two keys. A Public and Private key.

Public key is used publicly, that is it is shared between peers. Used to identify you remote resources.

Private key it is a pair key that is kept as a secret for identifying your local machine.

Generating an SSH Key

Using Git Bash

Before generating a new SSH Key, it is recommended to check and confirm if you have an existing key which you can use.

To check if you have an existing key use the following command:

`ls -al ~/.ssh`
Enter fullscreen mode Exit fullscreen mode

The command will list all existing ssh keys. Here sample output:

If there is no existing key, you can generate a new key that you will use to link your computer with your github account.

To generate a new ssh key, follow the following steps:

  1. Genereate a new ssh-key

    ssh-keygen -t ed25529 -C "youremail@example.com"

The email should be the one associated with with your github account.

Follow the prompt by adding the name of your key, and also you can add the passphrase which is optional.

The passhrase should be memorable, you will need to use it each time you want to access you github account.

  1. Add your key to the SSH-Agent.

Open your Powershell Terminal and follow the steps below:

- Run the following command to start your SSH Agent

    `Get-Service -Name ssh-agent | Set-Service StartupType Manual`

- Add your key to the  path using the  command

    `ssh-add <PATH/TO/YOUR/PRIVATE_KEY>`
Enter fullscreen mode Exit fullscreen mode

After the command, you should specifify the path to your private key and add it to your host machine's ssh-agent. i.e. ssh-add "C:/Users/<YOUR_USER_NAME/.ssh/<YOUR_PRIVATE_KEY>

If you don't know your computer's user name, you can use the whoami command to see your username.

Your PRIVATE_KEY_NAME is the name that you gave to your key.

- Once you're done, close your Powershell terminal and return to Git Bash.

- Type the following command

    `git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe`

- Then Clip and copy your key using the following commands:

    `clip < ~/.ssh/<PUBLIC_KEY.pub>`
Enter fullscreen mode Exit fullscreen mode

i.e clip < /.ssh/my_key.pub

    `cat ~/.ssh/<PUBLIC_KEY.pub>`
Enter fullscreen mode Exit fullscreen mode

i.e. 'cat ~/.ssh/my_key.pub` to view your public ssh key

- After displaying your key, select and copy the key to add to your GitHub account.
Enter fullscreen mode Exit fullscreen mode

Adding Your Key to GitHub

To connect your computer and git-hub is necessary to facilitate access to your remote resources from your local computer.

You need to add your SSH Keys to GitHub to facilitate the connection. To achieve this, follow the steps:

If you have an existing GitHub account:

a. Login to your account using your email/username and password.

b. From the profile section, go to Settings

c. Scroll to SSH and GPG Key. Navigate and click on New SSH Key on the SSH Key Section to your right.

e. Add the Title/Name of the key i.e. Ubuntu Laptop, the key will be used for Authentication

f. Paste your key to the available textbox.

g. Finally, click on Add Key to save your key. You'll be prompted to re-enter your git-hub account password (login password), to confirm your authenticity before saving.

Now you are ready to go. Before you start, it is good you test your connection to check if it was successful.

Run Powershell as an Administrator and use the following command to test connection:

`ssh -T git@github.com`
Enter fullscreen mode Exit fullscreen mode

If the connection is successful, your username will be displayed along with a success message.

If successful, you are good to go and work on your project.

Working With Folders

  • To keep you project structural and more organized; you've to keep its files and scripts in folders.

This will make it easier to navigate and access the project. Follow the steps below to work with folders in Windows.

  • In Git Bash Terminal(Opened normally)
  1. To navigate and change location use the following command

    `cd <path_location>`
    

To select the current location

    `cd .`
Enter fullscreen mode Exit fullscreen mode

To get to the previous location one step back use

    `cd ..`
Enter fullscreen mode Exit fullscreen mode

Getting back to the root directory use

    `cd ~`
Enter fullscreen mode Exit fullscreen mode

You can also move to a specific folder by specifying the path to that folder

    `cd <path/to/folder>
Enter fullscreen mode Exit fullscreen mode

example: cd "C:/Users/Admin/Desktop/My-Project"

My-Project/ is a folder located in Desktop, which is for the user with the username Admin.

  1. To create a directory or folder use

    `mkdir <directory_name>`
    

i.e. Create a directory named My-First-Project in Desktop mkdir My-First-Project

  1. Navigate to your created folder. Assuming you are on Desktop, use the following command:

    `cd My-First-Project/`
    

So your bash terminal path will look like ~/Desktop/My-First-Project

  1. To list and view items use the following command

    `ls`
    

It will list all files and folders excluding the hidden files and folders. As shown below:

To list and view all files and folders including the hidden ones you add -a attribute to the ls command as below

    `ls -a`
Enter fullscreen mode Exit fullscreen mode

To list files and display their attributes we add the -l attribute to the ls command.

    `ls -l`
Enter fullscreen mode Exit fullscreen mode

Note: You can combine the attributes to list all files and their attributes i.e. as ls -al

  1. You can delete an empty folder using

    `rm -r <folder_name>`
    

To delete a folder together with its contents we use the rm -r <non-empty_folder_name> command.

Example: Create a folder in you my project folder using the mkdir command, inspect the folder created and its contents, then delete the folder using the rm command:

Steps

  • Create scripts: mkdir scripts

  • Navigate to scripts and back to your project folder: cd scripts/ and cd ..

  • Inspect scripts: ls scripts/

  • Delete scripts: rm -r scripts/

Note: rm without any attribute will no delete a directory/folder even if empty, the attribute -r tells the command rm that what you're deleting is a directory.

  1. You can copy and paste contents between different folders.

To copy contents moves a duplicate of the files to another location leaving the original intact.

While using the cp command you have to specify the file/contents path and the destination address. Example:

To copy some file named hello.txt to the scripts folder; you need to recreate the folder and create the file in the root directory then copy it to scripts folder in your projects folder

Steps

  • In your project folder create scripts mkdir scripts

  • Navigate to your project root folder and create the file hello.txt touch hello.txt

  • Copy the file to scripts cp hello.txt ~/scripts

  • Inspect both locations to see that the file exists in both location. ls and ls scripts

Note: You can specify the file path in the cp command if the file is in a different location and the destination path address

i.e. cp <~/path/to/file_to_be_copied.txt> <~/path/to_destination/>

  1. You can move a file or any content completely from the source to an new destination using the mv command.

Example: Move hello.txt file from the project's folder to a new folder named docs

`mv hello.txt docs/
Enter fullscreen mode Exit fullscreen mode

Note: You can also specify the file path in case the files and destination address are in different locations i.e. mv <~/path/from_source/hello.txt> <~/path/to_destination_folder/>

Working With Files

While working in your project you will need to create different files for your project.

The different files will be saved in different directories depending on their type and use.

The organization will help us to achieve a proper project structure, easier to navigate and access our project files.

We will create our files using Git Bash terminal.

  • To create a file we'll use the touch command.

Create a file named hello-world.txt in your project root directory.

    `touch hello-world.txt`
Enter fullscreen mode Exit fullscreen mode
  • Use the ls command to confirm your file has been created.

  • To view the contents of the file we use the cat command, since our file is empty it will display 'nothing'.

    `cat hello-world.txt`
    
  • To add contents to our file we use the echo command.

Add the following text to the file hello-world.txt; "Hello, World"

    `echo "Hello, World!" > hello-world.txt`
Enter fullscreen mode Exit fullscreen mode

We use the > to redirect the output to our file. > Rewrites any content in the file.

If you want to add more content to the file use >>, more text will be added to our file.

Example; add the following text to our file "I am happy to learn SSH and Git".

    `echo "I am happy to learn Git and SSH" >> hello-world.txt`
Enter fullscreen mode Exit fullscreen mode
  • cat hello-world.txt to view the contents of the file.

* Editing you file using nano *

Nano is an inbuilt text editor in Git Bash.

It helps us to navigate through our files; scripts, text file, .... once we make mistakes.

To open our script or text file we use the nano command in git bash then specify the file we want to open, for example open hello-world.txt using nano:

    `nano hello-world.txt'
Enter fullscreen mode Exit fullscreen mode

There are many more commands for nano that will help you edit your text file. For example the most common commands are:

1. Ctr + O - To save your file

2. Ctrl + X - To exit your nano text editor

3. Ctrl + K - To Cut text in the editor

4. Ctrl + U - To Paste your text

5. Ctrl + G - To find help for you nano editor.
Enter fullscreen mode Exit fullscreen mode

There are many more commands of help that you can use to edit you files in nano editor.

Your First Project

Initialize your first project with git

Before running the command, check to confirm that you are in your project folder using the pwd command.

The pwd command means print working directory it shows your current location with regard to the root directory.

If in your project directory, the path will end with the name of the project folder. I.e. My-First-Project

Use the git init command.

The command is used to initialize our project directory as a git repository.

Git repository structure.

After running the above command, you may notice notification of git initializing an empty git repo...

If you list to view the structure using the ls command you may notice a .git/ folder.

The folder contains Git's internal repository information, which git uses to manage activities such as: push, commits, pull, Branches, project history, repository configurations,....

Checking the project status

    `git status`
Enter fullscreen mode Exit fullscreen mode

Used to show what's happening with your project currently.

It can show things such as untracked files, staged files to be committed yet, if the working tree is clean meaning everything has been committed.

The above show that there is nothing that has been staged for commit, there are three untracked files in the initialized repository.

Staging your project files

    `git add .`
Enter fullscreen mode Exit fullscreen mode

It adds your project files to git for tracking.

Checking on the status of the staged file, you should get such an output with git status command

It prepares all you changes for the next commit.

You can stage a single file by specifying the file/folder you want to stage i.e. git add <file_name.txt>.

You should see almost a similar output when using git status command after staging a single file

Using the . means you stage everything in your project folder.

Commiting you staged files

    `git commit -m "<commit message>"`
Enter fullscreen mode Exit fullscreen mode

Creates a saved checkpoint.

The message should be precise and clear, related to the activity carried out. i.e. "My First Project Using Git Commit"

It does not upload anything to git-hub yet.

Uploading to git-hub

    `git push -u origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

Sends all the committed changes to GitHub. However, in our case we get the above error because we have not linked our local project repo to a git-hub remote repository.

We will be linking our repositories in the next section.

After running this command, you can easily check on the changes whether they reflect on GitHub via your browser.

However, for us we are yet to see these changes. This may not work since we are yet to create a remote repository which we will then connect to our local project in our laptop.

Creating a remote repository

To create a remote repository, follow the following steps:

  • Login to your git-hub account

  • In your account click on "Create New Repository"

  • Enter the name of your Repository as "My-First-Project"

  • Do not create a README, uncheck the checkbox

  • Provide a brief descriptions for your project. The description is: "My First Project: From a Local Folder to Git hub Using Git and SSH"

  • Then click on Create Repository

A new empty repository is created with the name "My-First-Project"

  • Navigate to your repository

  • Click on the code section, then click on ssh tab and copy the ssh code

We'll use the ssh code to connect our remote and local repositories before pushing our project.

Connecting our local and remote repositories

Navigate to your git initialized project folder

Type in the following command and thereafter paste the ssh code for your remote repository and hit enter.

    `git remote add origin <your_remote_ssh_code>`
Enter fullscreen mode Exit fullscreen mode

i.e. git remote add origin git@github.com:MakoriNyachaki/My-First-Github-Project.git

Rename your branch

If your remote repository's branch is not main rename it to main

    `git branch -m main`
Enter fullscreen mode Exit fullscreen mode

Check the remote

    `git remote -v`
Enter fullscreen mode Exit fullscreen mode

Push your project to git-hub

    `git push -u origin <branch_name>`
Enter fullscreen mode Exit fullscreen mode

i.e. git push -u origin main this works to upload our committed changes to our git-hub repository.

Summary

An introductory article for learning GIT and SSH. It is thrilling practical exercise for generating ssh keys, working with folders and files, connecting local project folders with remote repositories, and working with git. The introductory article requires one to do further research on so many commands that are available for the different tools we have used here in order to be an expert. Let's learn forward.

Top comments (0)