Getting Started with Git and GitHub

What are Git and GitHub?

Git is a distributed version control system used to manage and track changes in source code during software development. It allows you to create, modify, and manage multiple versions of your code base.

GitHub is a platform that hosts Git repositories online, making it easier for teams to collaborate, review code, and manage version control.

With Git and GitHub, you can work on projects with others, revert to previous versions if necessary, and keep your code organized.

How to Install Git

Follow these instructions to install Git on your operating system.

Linux

Most Linux distributions include Git in their package managers. Use the following command to install:

sudo apt update
sudo apt install git

Windows

Download Git from https://git-scm.com/download/win and run the installer. Follow the on-screen instructions.

macOS

On macOS, Git can be installed via Homebrew:

brew install git

Configuring Git

After installation, configure Git with your username and email:

Step 1: Set Username

git config --global user.name "Your Name"

Step 2: Set Email

git config --global user.email "your.email@example.com"

These settings will identify you as the author of any commits you make in Git.

Basic Git Commands

Here are some essential Git commands to get you started:

Initialize a Repository

To start tracking changes in a directory, initialize a new Git repository:

git init

Check the Status of Your Repository

View the current status of your files, including any changes:

git status

Add Files to Staging

To add changes to the staging area, use:

git add filename

To add all changes, use:

git add .

Commit Changes

Record your changes with a commit message:

git commit -m "Your commit message"

View Commit History

See a log of past commits:

git log

Connecting Git with GitHub

To use GitHub, you’ll need a GitHub account. Sign up here if you haven’t already.

Step 1: Create a New Repository on GitHub

Go to GitHub, create a new repository, and name it as desired. Copy the repository URL, as you’ll need it in the next step.

Step 2: Link Your Local Repository to GitHub

In your terminal, link your local repository to GitHub by adding the remote URL:

git remote add origin https://github.com/username/repository-name.git

Step 3: Push Changes to GitHub

To upload your local commits to GitHub, use:

git push -u origin master

Basic Workflow with Git and GitHub

Once your repository is connected to GitHub, use the following workflow: