DEV Community

Cover image for A Simple beginners Guide to Git & GitHub
lawrence Murithi
lawrence Murithi

Posted on

A Simple beginners Guide to Git & GitHub

What is Git, GitHub and why version control is important

1. What is Git & GitHub
Git is a distributed version control system (VCS)/tool that helps you save, track, and manage changes in your code. It keeps a history of your work so you can see what changed, when it changed, and who changed it.
Think of Git like a save system for your code, but much better than “Save As”.
GitHub is an online platform where you store Git projects.
GitHub allows you to:

  • Back up your code
  • Share code with others
  • Work on the same project from different computers.

What is version control?
Version control is a system that tracks and manages changes to software code and files over time.

Why is version control important?
Version control helps:

  • Track changes in your work
  • Go back to an older version if something breaks
  • Work safely without fear of losing files
  • Collaborate with other people on the same project

2. Set Up the Git & GitHub Environments
Step 1: Create a GitHub Account
Sign up on GitHub using you credentials.

Step 2: Install Git
Download Git.
Install the application using the downloaded file.
Open Git Bash (Windows) or Terminal (Mac/Linux).
Configure your identity(Name and Email) to help Git Identify who is making the changes any time the changes are made.
NB: Use the email address used to sign up on GitHub.

git config -global user.name "your name"
Enter fullscreen mode Exit fullscreen mode
git config -global user.email "youremail@example.com
Enter fullscreen mode Exit fullscreen mode

Check to ensure your configuration has been set up.

git config -global --list
Enter fullscreen mode Exit fullscreen mode

Step 3: Connect Git to Your GitHub Account
One of the easiest ways to connect your Git to your GitHub Account is using an SSH key which provides(digital identity) a secure password-less way to connect both to avoid the need for inputing a password every time.
To generate the SSH key on GitBash, run the command:

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

Now, you need to generate an agent; a helper program that holds your key in memory.

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Add the key generated to the agent.

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Lastly, print the public key and use it to connect your GitHub account.

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

Copy the key, navigate to the SSH and GHG keys on settings in you GitHub account and add a new SSH key.


Finally, authenticate your GitHub account in Git by running this command:

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

3. Set up a Repository in GitHub and Project folder in Git

  • Create a repository on GitHub
  • Create a project folder in Git
mkdir Myproject
Enter fullscreen mode Exit fullscreen mode
  • Initialize Git in your project. Tells Git to start tracking this project.
git init
Enter fullscreen mode Exit fullscreen mode
  • Add files in your folder. Tells Git which files to track.
git add 
Enter fullscreen mode Exit fullscreen mode
  • Save changes (commit)
git commit -m "your message"
Enter fullscreen mode Exit fullscreen mode

4. Connect your project to GitHub)

git remote add origin https://github.com/yourusername/repositoryname.git
Enter fullscreen mode Exit fullscreen mode
  • Push code to GitHub (send code from your computer to GitHub) Pushing refers to uploading local commits to a remote server to make your code accessible to others.
git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • Pull code from GitHub (send code from GitHub to your computer).
git clone https://github.com/username/repositoryname.git

git pull origin main
Enter fullscreen mode Exit fullscreen mode

clone creates/downloads a new copy on your local computer while pull updates your local files with any changes made.

  • Check the status of your file.
git status
Enter fullscreen mode Exit fullscreen mode

Checking status shows:

  1. Modified files
  2. New files
  3. Files ready to be committed

Summary Cheatsheet
Start Git - git init
Check status - git status
Stage files - git add
Save snapshot - git commit -m "message"
Download repo - git clone
Upload changes - git push
Update local - git pull

Top comments (0)