GitHub for Beginners

"Kickstart your GitHub journey with this beginner's guide! Learn essential features, setup, and boost your collaboration skills."

By Samir Niroula

28 October 2024
GitHub for Beginners

Getting Started with GitHub: A Beginner's Guide

GitHub is a powerful platform for version control and collaboration, widely used by developers to manage code and projects. Built on top of Git, GitHub enables teams to work together effectively, track changes, and share their work with the world. In this guide, we’ll cover the basics of GitHub, how to set it up, and explore its key features.


What is GitHub?

GitHub is a web-based platform that allows developers to store, manage, and share their code repositories. It provides a user-friendly interface for Git, making it easier to use version control in your projects. GitHub also facilitates collaboration through features like pull requests, issues, and project boards.

Key Features of GitHub:

  • Repositories: A place to store your project files and track their history.
  • Branches: Allow you to work on features or fixes in isolation before merging changes back to the main project.
  • Pull Requests: A method for submitting changes to a repository, enabling review and discussion before merging.
  • Issues: Track bugs, tasks, or feature requests associated with your project.

Setting Up GitHub

Step 1: Create a GitHub Account

  1. Go to GitHub.com and sign up for a free account.
  2. Choose a username, enter your email address, and create a password.

Step 2: Install Git

To interact with GitHub from your local machine, you need to have Git installed. Download it from Git's official website and follow the installation instructions.

Step 3: Configure Git

After installing Git, you need to configure your username and email:

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

Creating a New Repository

To create a new repository on GitHub:

  1. Click the “+” icon in the top right corner and select “New repository.”
  2. Enter a name for your repository and choose visibility (public or private).
  3. Click “Create repository.”

Example: Initialize a Repository Locally

You can also create a repository directly on your local machine:

mkdir my-project
cd my-project
git init

Cloning a Repository

To work on an existing project, you can clone a repository:

git clone https://github.com/username/repository-name.git

This creates a local copy of the repository on your machine.

Making Changes and Committing

After making changes to your project, you need to stage and commit them:

git add .
git commit -m "Describe your changes here"
  • git add . stages all modified files.
  • git commit -m saves your changes with a descriptive message.

Pushing Changes to GitHub

To upload your local changes to GitHub, use:

git push origin main

This command pushes your commits to the main branch on GitHub. If you’re using a different branch, replace main with your branch name.

Working with Branches

Branches allow you to work on features or fixes without affecting the main codebase. To create a new branch:

git checkout -b new-feature

After making changes, push the branch to GitHub:

git push origin new-feature

Creating a Pull Request

Once your feature is ready, you can create a pull request to merge it into the main branch:

  1. Go to your repository on GitHub.
  2. Click on the “Pull requests” tab.
  3. Click “New pull request.”
  4. Select your branch and click “Create pull request.”

Using Issues for Project Management

GitHub Issues allow you to track bugs, tasks, and feature requests. To create an issue:

  1. Go to the “Issues” tab in your repository.
  2. Click “New issue.”
  3. Provide a title and description, then click “Submit new issue.”

Using Git in VS Code

Visual Studio Code (VS Code) is a popular code editor that comes with built-in support for Git. This guide will walk you through creating a Git repository in VS Code, making changes to your project, and pushing those changes to GitHub.


Prerequisites

  1. Install Git: Make sure you have Git installed on your machine. Download it from Git's official website.
  2. Create a GitHub Account: If you don’t already have one, sign up at GitHub.com.

Step 1: Open VS Code

Launch Visual Studio Code on your computer.


Step 2: Open the Integrated Terminal

You can open the integrated terminal in VS Code by selecting View > Terminal from the menu, or by using the shortcut:

  • Windows/Linux: Ctrl + (backtick)
  • Mac: Cmd + (backtick)

Step 3: Create a New Project Folder

Use the terminal to create a new directory for your project:

mkdir my-new-project
cd my-new-project

Step 4: Initialize a Git Repository

In the terminal, run the following command to initialize a new Git repository:

git init

You should see a message indicating that an empty Git repository has been created.


Step 5: Create a New File

You can create a new file in your project. For example, create an index.html file:

touch index.html

Example HTML Content

Open index.html in the editor and add some basic HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My New Project</title>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

Step 6: Stage Your Changes

In the terminal, stage the file you just created:

git add index.html

You can also stage all changes at once using:

git add .

Step 7: Commit Your Changes

After staging, commit your changes with a descriptive message:

git commit -m "Add index.html with basic structure"

Step 8: Create a GitHub Repository

  1. Go to GitHub and log in to your account.
  2. Click on the “+” icon in the top right corner and select “New repository.”
  3. Name your repository (e.g., my-new-project).
  4. Choose visibility (public or private) and click “Create repository.”

Back in the VS Code terminal, link your local repository to the newly created GitHub repository:

git remote add origin https://github.com/your-username/my-new-project.git

Make sure to replace your-username with your actual GitHub username.


Step 10: Push Your Changes to GitHub

Finally, push your local commits to the GitHub repository:

git push -u origin master

Note: If you’re using a different branch (like main), replace master with main.


Conclusion

GitHub is an essential tool for developers, enhancing collaboration and version control in projects. By understanding its basic features and workflows, you can effectively manage your code and work with others. As you become more familiar with GitHub, you’ll discover advanced features that can further streamline your development process. Happy coding!