General

Git Fundamentals for Newbies: All You Need to Work with Git and GitHub

Damian Igbe, Phd
Sept. 10, 2024, 12:08 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Introduction to Git and GitHub

Git is a distributed version control system used to track changes in source code during software development.

GitHub is a platform that provides Git repository hosting and additional collaborative features. This guide will cover the basics of Git and GitHub and include lab exercises to help you practice.

Key Concepts

1. Version Control

Version control systems track changes to files and enable multiple people to collaborate on projects. Git is a popular version control system that helps manage and track changes to your codebase.

2. Repository

A repository (repo) is a directory where your project files and history are stored. Git repositories can be local (on your machine) or remote (on platforms like GitHub).

3. Commits

Commits are snapshots of your project at a specific point in time.

Each commit includes:

  • a unique ID,
  • a message describing the changes,
  • and metadata about the changes.

4. Branches

Branches allow you to work on different features or fixes simultaneously.

The main branch is usually named:

  • main or
  • master

But you can create additional branches for development purposes.

Getting Started with Git

1. Installing Git

  • Windows: Download the Git installer from git-scm.com and follow the setup instructions.
  • macOS: Install Git using Homebrew with brew install git or download from git-scm.com.
  • Linux: Install Git using your package manager, e.g., sudo apt-get install git for Debian-based distributions.

2. Configuring Git

Set up your Git identity by configuring your name and email:

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

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

3. Creating a Local Repository

To create a new Git repository, navigate to your project directory and run:

git init

4. Adding Files

Add files to the staging area (preparing them for a commit):

git add <file>

To add all files, use:

git add .

5. Committing Changes

Commit your staged changes with a descriptive message:

git commit -m "Your commit message"

6. Viewing History

View your commit history with:

git log

 

Working with Branches

1. Creating a New Branch

Create and switch to a new branch:

git checkout -b <branch-name>

2. Merging Branches

Merge changes from one branch into another:

git checkout main

git merge <branch-name>

3. Deleting a Branch

Delete a branch that is no longer needed:

git branch -d <branch-name>

 

Getting Started with GitHub

1. Creating a GitHub Account

Sign up for a GitHub account at github.com.

2. Creating a Repository on GitHub

  • Go to GitHub and log in.
  • Click the "New" button to create a new repository.
  • Fill in the repository name and description, then click "Create repository."

3. Pushing Local Changes to GitHub

Link your local repository to the GitHub repository:

git remote add origin <repository-URL>

Push your local commits to GitHub:

git push -u origin main

4. Cloning a Repository

To clone an existing GitHub repository to your local machine:

git clone <repository-URL>

5. Pulling Changes

Fetch and merge changes from the remote repository:

git pull

 

Lab Exercises

Exercise 1: Initialize a Git Repository

  1. Create a new directory on your computer.
  2. Initialize a Git repository in that directory.
  3. Create a file named README.md.
  4. Add and commit the README.md file.

Exercise 2: Branching and Merging

  1. Create a new branch named feature-branch.
  2. Switch to feature-branch and create a new file named feature.txt.
  3. Add and commit feature.txt.
  4. Switch back to the main branch and merge feature-branch into main.
  5. Delete feature-branch.

Exercise 3: Using GitHub

  1. Create a new repository on GitHub.
  2. Initialize a local repository and link it to the GitHub repository.
  3. Create a file named index.html, add it, commit it, and push the changes to GitHub.
  4. Clone the GitHub repository to a new directory on your machine.

Conclusion

By understanding these Git and GitHub fundamentals, you’ll be equipped to manage your projects effectively and collaborate with others. Practice these commands and concepts regularly to become proficient in version control and repository management.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role