DEV Community

Cover image for A Quick Start with Git and GitHub
Celestina Odili
Celestina Odili

Posted on • Updated on

A Quick Start with Git and GitHub

Check out My Repository

Table of Contents

Introduction
Setting Up Git

  • Installation
  • Configuration

Creating a Repository

  • Initializing a Repository

Basic Git Commands

  • Checking Status
  • Adding Files
  • Making Commits
  • Pushing Changes
  • Pulling Changes

Introduction

Git and GitHub have become essential tools for developers worldwide. Git is a version control system that tracks changes in source code, while GitHub is a web-based platform for hosting Git repositories. Mastering Git and GitHub is crucial for modern software development and devops. This guide will walk you through setting up Git, creating a repository and using the basic commands like commits, pushing and pulling.

Setting Up Git and GitHub

Installation

To get started with Git,

  • Click download to download and install.

Image description

  • Choose your local computer OS. For windows, choose the appropriate for your configuration and install.

Image description

Configuration

After installing Git, initialize your git in git bash and configure your username and email. These details will be associated with your commits.

  • On your local computer task bar search for git bash and run as administrator.

Image description

  • Configure Username. Run the command below personalizing the username and press enter.
    git config --global user.name "Your GitHub username"

  • Configure email. Run the command below personalizing the email and press enter.
    git config --global user.email "your github email address"

Image description

Creating a Repository

A Git repository is where your project files and their revision history are stored.

Create a Directory

  • Run this command to create a new directory called gitlab3.
mkdir gitlab3
Enter fullscreen mode Exit fullscreen mode
  • Run the command below to change to the new directory created.
cd gitlab3
Enter fullscreen mode Exit fullscreen mode

Initializing a Repository

To initialize a new repository, run the following command

git init
Enter fullscreen mode Exit fullscreen mode

This will initialize empty git repository in the local directory.

Image description

Move to Visual studio code (optional)

You can complete all task with git bash but if you do enjoy the GUI feel of visual studio code while working on the terminal, you can navigate to visual studio code by running the following command.

code .
Enter fullscreen mode Exit fullscreen mode

This will launch a new visual studio code interface.

Image description
On the Visual studio code,

  • Create a simple html file called Index.html
  • go to the menu, click on the 3 dot select terminal and then new terminal.

Image description

Basic Git Commands

Now that you have your code ready, you can carry out any other command.

  • On the terminal, run git commands like check git status, git add, git commit, git push and git pull.

Checking Status

git status
Enter fullscreen mode Exit fullscreen mode

Adding Files

Add your file to the staging area.

git add filename
Enter fullscreen mode Exit fullscreen mode

replace _filename _ with the name of the file you want to add. For example

git add index.html
Enter fullscreen mode Exit fullscreen mode
  • To add all changes:
git add .
Enter fullscreen mode Exit fullscreen mode

Image description

Making Commits

Commits are snapshots of your project at a given point in time. Once your changes are staged with the add command, commit them with a message:

git commit -m "Your commit message"

example:
git commit -m "initial commit"
Enter fullscreen mode Exit fullscreen mode

Image description

Pushing Changes

Pushing command sends your commits to a remote repository, like GitHub. Hence, the remote repository has to exist first before a push command can take effect.

Creating Remote Repository in GitHub

  • go github.com and sign in
  • click on + to add new repository

Image description

  • Give it a name.
  • choose public or private
  • Add README file for your documentation
  • Click create repository

Image description

  • go to code and copy the repository url.

Image description

  • connect the repository to the local machine. use the following command. git remote add origin repo url replace repo url with your git hub repository url

For Example,

git remote add origin https://github.com/nnenne/mywebapp.git
Enter fullscreen mode Exit fullscreen mode

Image description

  • Push changes
git push origin master
Enter fullscreen mode Exit fullscreen mode

Image description

  • sign in to GitHub when prompted and authorize git ecosystem. This is only applicable when you are pushing for the first time.
  • Go back to GitHub and notice the file is now in GitHub.

  • add a readme file on visual studio code. Afterwards do git add, git commit and git push.

Image description

Image description

Pulling Changes

Pulling fetches and merges changes from the remote repository to your local repository.

  • Edit any of the file on GitHub, commit changes

Image description

  • On your terminal, type the pull command below and notice the changes now reflect.

git pull origin master

Image description

Check out My Repository

Top comments (0)