DEV Community

Emmanuel Koech
Emmanuel Koech

Posted on

Understanding the Git Workflow: Working Directory, Staging, Commit and Push.

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

Introduction

When I started learning Git, I found it a bit confusing at first. I knew GitHub was used to store code, but I did not fully understand how a project on my computer gets connected to GitHub.

In this article, I will explain the steps I followed to create a simple project, connect it to GitHub using SSH, and push my code to the repository.


What is Git?

Git is a tool that helps developers track changes in their code.

For example, if I am working on a website and make changes to my files, Git keeps a record of those changes. This makes it easier to go back to an earlier version if something goes wrong.

Git works on your computer.

What is GitHub?

GitHub is a website where Git repositories can be stored online.

The easiest way to understand the difference is:

  • Git helps me track my code.
  • GitHub stores my code online.

GitHub also makes it easier for developers to share projects and work together.


Step 1: Install Git

The first thing I need is Git installed on my computer.

I can check if Git is installed by opening the terminal and running:

git --version
Enter fullscreen mode Exit fullscreen mode

If Git is installed, I should get something similar to:

git version 2.x.x
Enter fullscreen mode Exit fullscreen mode

The version number may be different depending on the computer.


Step 2: Create a Project Folder

I can create a folder for my project.

For example:

mkdir my-first-project
Enter fullscreen mode Exit fullscreen mode

Then I move into the folder:

cd my-first-project
Enter fullscreen mode Exit fullscreen mode

I can then create a simple file such as:

README.md
Enter fullscreen mode Exit fullscreen mode

Inside the file, I can write:

# My First GitHub Project

This is my first project using Git and GitHub.
Enter fullscreen mode Exit fullscreen mode

At this point, the project is only on my computer.


Step 3: Initialize Git

Now I need to tell Git that this folder is a Git project.

I run:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a hidden .git folder inside my project.

I can check the status of the project with:

git status
Enter fullscreen mode Exit fullscreen mode

Git will show me which files are currently being tracked or are not yet tracked.


Step 4: Set Up SSH

SSH allows my computer to connect securely to GitHub.

First, I create an SSH key:

ssh-keygen -t ed25519 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

I can press Enter to use the default location.

This creates two keys:

id_ed25519
id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

The important thing to remember is that the .pub file is the public key.

I should never share my private key.


Step 5: Add the SSH Key to GitHub

I need to copy my public key and add it to my GitHub account.

On Linux or macOS, I can use:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

On Windows PowerShell:

Get-Content ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

I copy the key that is displayed.

Then on GitHub, I go to:

Settings → SSH and GPG keys → New SSH key

I give the key a name, paste my public key, and save it.


Step 6: Test the SSH Connection

Before pushing my project, I can test whether SSH is working:

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

If everything is set up correctly, GitHub will confirm that authentication was successful.

This means my computer can communicate with GitHub using SSH.


Step 7: Create a Repository on GitHub

Next, I create a new repository on GitHub.

For example, I can name it:

my-first-project
Enter fullscreen mode Exit fullscreen mode

I create the repository without adding a README because I already have one in my local project.

GitHub will then give me a repository address that looks like:

git@github.com:username/my-first-project.git
Enter fullscreen mode Exit fullscreen mode

Step 8: Connect My Local Project to GitHub

I go back to my terminal and make sure I am inside my project folder.

Then I connect it to GitHub:

git remote add origin git@github.com:username/my-first-project.git
Enter fullscreen mode Exit fullscreen mode

Here, origin is the name Git gives to the remote repository.

I can check that the connection was added by running:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Step 9: Add My Files

Before committing my files, I need to add them to the staging area.

I can add a specific file:

git add README.md
Enter fullscreen mode Exit fullscreen mode

Or I can add all the files:

git add .
Enter fullscreen mode Exit fullscreen mode

I can check the status again:

git status
Enter fullscreen mode Exit fullscreen mode

Step 10: Commit the Changes

Now I create my first commit.

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

A commit is basically a saved version of my changes.

The message should explain what I changed.

For example:

git commit -m "Add README"
Enter fullscreen mode Exit fullscreen mode

Step 11: Push the Project to GitHub

Before pushing, I can make sure my branch is called main:

git branch -M main
Enter fullscreen mode Exit fullscreen mode

Then I push the project:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Git will send my committed files to the GitHub repository.

I can then refresh my GitHub repository page and see my project online.


My Git Workflow

After setting everything up, the normal process becomes much simpler.

Whenever I make changes to my project, I can use:

git add .
git commit -m "Describe my changes"
git push
Enter fullscreen mode Exit fullscreen mode

So the basic workflow is:

Make changes
     ↓
git add
     ↓
git commit
     ↓
git push
     ↓
GitHub
Enter fullscreen mode Exit fullscreen mode

Common Problems

Permission denied (publickey)

If I get:

Permission denied (publickey)
Enter fullscreen mode Exit fullscreen mode

it usually means GitHub could not authenticate my SSH key.

I should check that I added the correct public key to GitHub.

Nothing to commit

If Git says:

nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

it means there are no new changes to commit.

Wrong repository

If I connected my project to the wrong GitHub repository, I can check the remote using:

git remote -v
Enter fullscreen mode Exit fullscreen mode

I can also change it using:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using Git and GitHub becomes much easier once I understand the basic process.

I create my project locally, initialize Git, connect my GitHub account using SSH, create a repository, commit my changes, and push them to GitHub.

The main commands I need to remember are:

git init
git status
git add .
git commit -m "message"
git push
Enter fullscreen mode Exit fullscreen mode

I learned that Git is used to manage changes in my project, while GitHub allows me to store and share that project online. SSH makes the connection between my computer and GitHub secure.

This is the basic workflow I can now use for future projects.

Top comments (0)