DEV Community

Emmanuel Akpadia
Emmanuel Akpadia

Posted on

Git & Github: Quick Setup Guide

What is Git

Git is a version control system that allows multiple developers in a team collaborate on a project in real time, by tracking changes to source codes.

Key Git Concepts

  • Repository - A folder where Git stores your project's code and its history of changes. It can be stored Locally or Remotely.
  • Clones - A copy of a remote repository on your computer.
  • Pull - Getting the lates changes from a remote repository.
  • Push - Saving your changes to a remote repository.
  • Commit - A snapshot of changes made to the repository.
  • Branch - A parallel version of the repository.
  • Merge - Combining changes from different branches.

What is Github

GitHub is a cloud-based platform based on Git, which hosts code repositories and helps developers collaborate.


Setting up Git

Git can be downloaded from git-scm.com

  • For windows: Simply download and run the installer to install Git and Gitbash
  • For MacOS: Open terminal and run $ brew install wget to install brew, then run $ brew install git.

Configuring Git

Git requires your Username and email address to label your commits.
To add Username, run

$ git config --global user.name "Username"
Enter fullscreen mode Exit fullscreen mode

To add Email Address

$ git config --global user.email "[email protected]" 
Enter fullscreen mode Exit fullscreen mode

Creating a Repository

using terminal on MacOS, or Git bash on Windows, create a directory for your project.

$ mkdir git-lab
Enter fullscreen mode Exit fullscreen mode

Change the working directory to the one we just created.

$ cd git-lab
Enter fullscreen mode Exit fullscreen mode

Initialize Git

To create our Repository in this directory, run

$ git init
Enter fullscreen mode Exit fullscreen mode

A simple Html file can be created via the notepad or using the terminal.

$ touch index.html
Enter fullscreen mode Exit fullscreen mode

To quickly add text to the html file

$ echo "this is an index file for my first repo" > index.html
Enter fullscreen mode Exit fullscreen mode

Then we check the status of our file

$ git status
Enter fullscreen mode Exit fullscreen mode

At this point, it is untracked. This means it is present in our folder, but it's absent from the staging area.

So we need to tell Git to track our file.

$ git add index.html
Enter fullscreen mode Exit fullscreen mode

or git add -all , git add -A or git add . to add all the files in the directory.

To unstage a file

git restore --staged index.html
Enter fullscreen mode Exit fullscreen mode

Then check the status.

Commit

To save the staged file(s)

$ git commit -m "My first commit"
Enter fullscreen mode Exit fullscreen mode

To see previous commit history,

$ git log
Enter fullscreen mode Exit fullscreen mode

Branches

If we wanted to add changes to our project without affecting the main project, we create a branch.

$ git branch "parallel-repo"
Enter fullscreen mode Exit fullscreen mode

To see all branches

$ git branch
Enter fullscreen mode Exit fullscreen mode

To switch between branches

$ git checkout parallel-repo
Enter fullscreen mode Exit fullscreen mode

To delete a branch

$ git branch -d parallel-repo
Enter fullscreen mode Exit fullscreen mode

Merging

To combine changes from one branch into another, first switch to the branch you want to merge into(main or master)

$ git checkout main
Enter fullscreen mode Exit fullscreen mode

Now we merge

$ git merge parallel-repo
Enter fullscreen mode Exit fullscreen mode

Creating a Remote Repo

Go to github.com and create a new repository.

Create a new Github repository
Copy git remote add origin https://github.com/username/repository.git, then paste in terminal and run.

Finally run,

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

Git would ask to authorize Github, then the local commit gets pushed to the Github repository.

Top comments (4)

Collapse
 
mehajho3ein profile image
Ho3ein

You wrote a good post, but i have a suggestion for you.

When switching branches, it's better to use the switch command because it's more modern, improve code readability, and is used in most projects.

Collapse
 
emzeeviolino_ profile image
Emmanuel Akpadia

Thank you for this, I'll look to improve on this.

Collapse
 
victorarchie profile image
Victor Archie

Good read

Collapse
 
emzeeviolino_ profile image
Emmanuel Akpadia

Thank you Victor