The 8 Most Used Git Commands and How To Add An SSH Key

Git Create ssh Keys

Hello there folks! in this tutorial, I will show you how to work with git and GitHub. This is very important when you want to keep track of your source code for that software you are building.

Git and GitHub are very popular and most software companies use them every day. Let’s get started

Difference between git and Github

Git is a version control system that lets you manage and keep track of your source code history.

GitHub is a cloud-based hosting service that lets you manage Git repositories.

Basic Git Commands

Let’s say you want to create a project and you want to keep track of your source code till the time you push the code to a remote repository like GitHub. The following are commands you can use within the git local environment.

This is how you would begin. So am going to illustrate this using Vs Code text editor. However, you can use PowerShell for windows terminal CLI  for mac and Linux.

Let’s begin.

Step 1

Open the Vs code text editor and go to the terminal command interface. Type the following commands

Local:

1. git --version

2. mkdir demo-repo (make a new directory)

3. cd demo-repo/ (cd stands for current directory)

4. git init (creates a new Git repository)

5. touch index.html (touch creates a file)

6. code . (opens a new window in Vs code with index.html file)

7. ls (list items within a directory)

8. git add index.html (add index.html to the staging area)

9. git status (displays the state of the working directory and the staging area)

10. git commit -m "first commit" (git commits are snapshots of your entire repository at specific times)

That was the local environment. Now the following commands are used on the remote environment on GitHub. Type them as follows…

Remote:

1. create a repo (create a repository on GitHub, give it a name)

2. copy the ssh link (two URLs are created https & ssh, copy the ssh link if you want an extra layer of security)

3. git remote add origin (add ssh link and click enter)

4. git pull origin master

5. ls

6. git push origin master

git pull origin/master will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch.

git push origin master will push changes from the local master branch to the remote master branch.

You may encounter an issue when using the ssh link. The error that comes tells you to add an ssh key. So let’s see how we can generate an ssh key in GitHub.

Generating a new SSH key and adding it to the ssh-agent

Steps

1. Open Git Bash.

2. Paste the text below, substituting it for your GitHub email address.

$ ssh-keygen -t ed25519 -C "your_email@example.com"

3. When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

> Enter a file in which to save the key (/c/Users/you/.ssh/id_algorithm):[Press enter]

4. At the prompt, type a secure passphrase. For more information, see “Working with SSH key passphrases.”

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter the same passphrase again: [Type passphrase again]

Adding your SSH key to the ssh-agent

Steps

1. # start the ssh-agent in the background

$ eval "$(ssh-agent -s)"

> Agent pid 59566

2. Add your SSH private key to the ssh-agent.

$ ssh-add ~/.ssh/id_ed25519

Add the SSH key to your account on GitHub.

Steps

1. Copy the SSH public key to your clipboard.

$ clip < ~/.ssh/id_ed25519.pub

# Copies the contents of the id_ed25519.pub file to your clipboard

2. In the upper-right corner of any page, click your profile photo, then click Settings.

3. In the “Access” section of the sidebar, click SSH and GPG keys.

4. In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Windows, you might call this key “Brian’s Personal Window’s Computer”.

5. Paste your key into the “Key” field.

6. Click Add SSH key.

7. If prompted, confirm your GitHub password.

If you haven’t got this tutorial well I have made a video for you that talks about all this.

 

close

Sign up for free tutorials in your inbox.

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published.