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.
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.
Open the Vs code text editor and go to the terminal command interface. Type the following commands
1. git --version 2. mkdir demo-repo (<strong>make a new directory</strong>) 3. cd demo-repo/ (<strong>cd stands for current directory</strong>) 4. git init (<b>creates a new Git repository</b>) 5. touch index.html (<strong>touch creates a file</strong>) 6. code . (<strong>opens a new window in Vs code with index.html file</strong>) 7. ls (<strong>list items within a directory</strong>) 8. git add index.html (<strong>add index.html to the staging area</strong>) 9. git status (<strong>displays the state of the working directory and the staging area</strong>) 10. git commit -m "first commit" (<strong>git commits are snapshots of your entire repository at specific times</strong>)
That was the local environment. Now the following commands are used on the remote environment on GitHub. Type them as follows…
1. create a repo (<strong>create a repository on GitHub, give it a name</strong>) 2. copy the ssh link (<strong>two URLs are created https & ssh, copy the ssh link if you want an extra layer of security</strong>) 3. git remote add origin (<strong>add </strong><strong>ssh link and click enter</strong>) 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
1. Open Git Bash.
2. Paste the text below, substituting it for your GitHub email address.
$ ssh-keygen -t ed25519 -C "firstname.lastname@example.org"
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
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.
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.