DEV Community

Cover image for The Ultimate Git & GitHub Guide: From Beginner to Advanced
Parnab Bagchi
Parnab Bagchi

Posted on

1 1

The Ultimate Git & GitHub Guide: From Beginner to Advanced

Welcome to the ultimate beginner's guide to Git and GitHub! Whether you're just starting your journey in version control or looking to solidify your foundational knowledge, this blog has got you covered.

In this guide, you'll discover the key concepts of version control, learn what Git and GitHub are, and explore the essential commands and workflows that power modern software development. Step by step, you'll understand how to set up Git, connect it with GitHub, and manage repositories effectively.

From understanding the staging area, commits, branches, and merges to handling pull requests, merge conflicts, and resets, this guide breaks down every concept in simple terms with practical examples. By the end of this blog, you'll have the confidence to start using Git and GitHub for your own projects and contribute to open-source repositories.

Whether you're a student, developer, or tech enthusiast, this comprehensive guide will help you go from zero to hero in Git and GitHub!


Table of Contents

  1. What is Version Control?
  2. What is Git?
  3. What is GitHub?
  4. Installing and Setting Up Git
  5. Setting Up GitHub
  6. Connecting Git with GitHub
  7. Understanding the Staging Area
  8. What is git config?
  9. What is git status?
  10. What is git commit?
  11. What is git push?
  12. What is git pull?
  13. What is git clone?
  14. Understanding Git Branches
  15. What is Fork in Git?
  16. What is a Pull Request?
  17. What is Merge in Git?
  18. What is a Merge Conflict?
  19. What is git log?
  20. What is git reset?

What is Version Control?

Version control is a system that records changes to a file or set of files over time, allowing you to recall specific versions later. With version control, you can:

  • Keep track of code history
  • Compare different versions of the code
  • Collaborate with multiple people without interfering with each other's work
  • Resolve conflicts when multiple people edit the same part of the code

Version control systems are essential for modern software development as they ensure code integrity and team collaboration.


What is Git?

Git is the most widely used version control system (VCS) today. Created by Linus Torvalds in 2005, the same person who developed the Linux kernel, Git helps developers track changes in their codebase over time.

Why Use Git?

Git allows multiple developers to work on a project simultaneously without fear of overwriting each other’s work. It enables:

  • Tracking changes made to code
  • Reverting to previous versions of a project
  • Creating branches to experiment with new features or fix bugs
  • Collaborating effectively with other developers

Git is distributed, meaning every developer has their own local copy of the entire repository and history. This makes it faster and more resilient to failures compared to centralized version control systems.


What is GitHub?

While Git is a version control tool that runs on your local machine, GitHub is a cloud-based platform that hosts Git repositories. It allows developers to store, manage, and collaborate on code.

GitHub integrates with Git, allowing you to:

  • Share your code publicly or privately
  • Collaborate with other developers on open-source projects
  • Use issues, pull requests, and project boards to organize and track progress

GitHub also provides features like continuous integration and GitHub Actions, which automate processes like code testing and deployment.


Installing and Setting Up Git

To start using Git, you need to install it on your computer. Here’s how to do it:

Step 1: Download Git

Go to the official Git website and download the version suitable for your operating system (Windows, macOS, or Linux).

Step 2: Install Git

Follow the installation instructions for your operating system. For most systems, the default installation options will work fine.

Step 3: Set Up Git

Once Git is installed, you need to set up your user name and email. These details will be used to associate your commits with your identity. Open Git Bash (a terminal program that comes with Git) and run the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

This step ensures that every commit you make is tagged with your name and email address.


Setting Up GitHub

Step 1: Create a GitHub Account

Visit GitHub and sign up for a free account. After signing up, you can access your personal repositories, explore open-source projects, and start collaborating with others.

Step 2: Create a New Repository

  1. Once logged in, click on the + icon in the top-right corner of the page and select New repository.
  2. Enter a repository name (e.g., my-first-repo).
  3. Add a short description about your project.
  4. Choose whether the repository should be Public (visible to everyone) or Private (only you and collaborators can access it).
  5. Optionally, check the box to Initialize this repository with a README. This will create a README.md file that provides information about your project.
  6. Click Create repository to complete the process.

Connecting Git with GitHub

Once your GitHub repository is set up, you need to link your local Git repository to the remote repository on GitHub. Here’s how to do it:

Step 1: Initialize a Git Repository Locally

  • Open Git Bash on your computer.
  • Navigate to the folder where you want to store your project using the cd (change directory) command:
cd <path to your project folder>
Enter fullscreen mode Exit fullscreen mode
  • Initialize a new Git repository in this folder by running:
git init
Enter fullscreen mode Exit fullscreen mode

This command creates a .git directory in your project folder, which will track changes.

Step 2: Connect to GitHub

  • Go to your newly created GitHub repository and copy the HTTPS URL from the "Clone or download" button.
  • Back in Git Bash, link your local repository to the GitHub repository using the following command:
git remote add origin https://github.com/your_username/your_repository.git
Enter fullscreen mode Exit fullscreen mode

Step 3: Push Your Changes

Before pushing, let’s commit your first change to the repository. Run the following commands:

  • Stage the changes:
git add .
Enter fullscreen mode Exit fullscreen mode
  • Commit the changes:
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode
  • Push the changes to GitHub:
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

This uploads your local code to GitHub.


Understanding the Staging Area

The staging area (also called the index) is an intermediate space where you prepare changes before committing them. It allows you to selectively add modifications to the commit.

  • Use git add <file_name> to stage specific files.
  • Use git commit -m "message" to commit the staged changes.

Think of the staging area as a "buffer" where you decide what to include in your next snapshot of the project.


What is git config?

The git config command is a convenience function used to set Git configuration values. These configurations can be set at the global level (for all repositories on your system) or the local level (specific to a repository). The configuration values are saved in .gitconfig text files.

Common git config Commands:

  • Set global username:
git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode
  • Set global email address:
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • List all configuration settings:
git config --list
Enter fullscreen mode Exit fullscreen mode

What is git status?

The git status command shows the state of your working directory and staging area. It allows you to see:

  • Which files are staged (ready to be committed)
  • Which files have changes that are not yet staged
  • Which files are untracked (new files that Git is not yet tracking)

This command is essential for checking what has been modified and what's ready to be committed.


What is git commit?

The git commit command is used to save your staged changes to the local repository. Each commit represents a snapshot of your project at a specific point in time. Once committed, changes are permanent in your local repository, and Git will never modify them unless you explicitly ask it to.

Basic git commit Command:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

The commit message should describe the changes you made. A good commit message helps track the progress of the project and understand the reasons for changes.

Staging and Committing Changes

To stage and commit changes, follow these steps:

  1. Check status: Use git status to see which files have been modified.
   git status
Enter fullscreen mode Exit fullscreen mode
  1. Stage changes: Use git add <file_name> to stage a specific file or git add . to stage all modified files.
   git add file_name
   # or
   git add .  # Stage all changes
Enter fullscreen mode Exit fullscreen mode
  1. Commit changes: Use git commit to save your changes to the repository.
   git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

What is git push?

The git push command is used to upload your local repository content to a remote repository, such as GitHub. It transfers your local commits to the remote repository, making them visible to others.

Important Considerations:

  • Pushing is how you share your changes with others.
  • Be cautious when pushing, as it can overwrite changes on the remote repository if you’re not careful.

Common git push Commands:

  • Push your local changes to the main branch on GitHub:
git push origin main
Enter fullscreen mode Exit fullscreen mode
  • Push your changes and set the default upstream branch for future pushes:
git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • Push the current branch to its upstream repository:
git push
Enter fullscreen mode Exit fullscreen mode

What is git pull?

The git pull command is used to fetch and immediately integrate changes from a remote repository into your local repository. It combines two Git operations:

  1. Fetch: Downloads the latest changes from the remote repository.
  2. Merge: Integrates those changes into your current branch.

This command is commonly used in collaborative workflows to ensure your local repository is up to date with the latest changes from the team.

How to Use git pull:

  • To fetch and merge changes from the default branch of the remote repository (origin):
  git pull
Enter fullscreen mode Exit fullscreen mode
  • To pull changes from a specific branch of the remote repository:
  git pull origin branch_name
Enter fullscreen mode Exit fullscreen mode

What is git clone?

The git clone command is used to create an identical copy of an existing remote repository on your local system. It downloads the repository’s content, including all branches, commit history, and configuration files, into a new directory.

This is the first command developers use when they start working on a new project hosted on a remote repository like GitHub.

How to Use git clone:

  • To clone a remote repository to your local system:
  git clone repository_url
Enter fullscreen mode Exit fullscreen mode
  • To clone a repository into a specific directory:
  git clone repository_url custom_directory_name
Enter fullscreen mode Exit fullscreen mode

Understanding Git Branches

A Git branch is essentially a pointer to a snapshot of your changes. Git allows you to work on different features or bug fixes in isolation without affecting the main codebase (usually main or master).

Why Use Branches?

  • Branches help you experiment or add features without modifying the stable main codebase.
  • Once you're done, you can merge the branch back into the main branch after reviewing the changes.

Branch Commands

Git branches are an essential part of version control. Here's how you can manage branches in Git:

  • List all local branches: This command lists all the branches in your local repository.
  git branch
Enter fullscreen mode Exit fullscreen mode
  • Rename the current branch to main: If you want to rename the current branch, for example, to main, use this command.
  git branch -m main
Enter fullscreen mode Exit fullscreen mode
  • Create a new branch and switch to it: To create a new branch and immediately switch to it, use this command. Replace branch_name with the desired branch name.
  git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode
  • Delete a branch: If you no longer need a branch, you can delete it with this command. Replace branch_name with the name of the branch you want to delete.
  git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode

What is Fork in Git?

In Git, a fork is essentially a copy of an existing repository where the new owner can independently work on the codebase. The forked repository is disconnected from the original repository's commit history, allowing the new owner to make changes without affecting the original repository.

On GitHub, a fork creates a new repository that retains the code and visibility settings of the original repository (upstream). It enables developers to experiment or contribute to a project without altering the original repository.


What is a Pull Request?

A pull request (PR) is a collaborative GitHub feature that allows developers to propose merging their changes from one branch into another. It provides a platform for reviewing, discussing, and improving the proposed changes before they are integrated into the main project.

In a pull request, you can:

  • View the changes (diff) proposed by the contributor.
  • Add comments or suggestions for improvement.
  • Approve or request further modifications.

A pull request workflow typically involves:

  1. Forking a repository.
  2. Making changes in a branch.
  3. Pushing the changes to the forked repository.
  4. Opening a pull request on the original repository.
  5. Collaborators reviewing and merging the pull request.

Pull requests ensure a structured and transparent code review process, helping teams maintain code quality and consistency.


What is Merge in Git?

Merging is Git's method of combining different lines of development created by branches into a single unified branch. It integrates changes from one branch into another to maintain a cohesive project history.

  • Show differences between the current branch and another branch: This command highlights the changes between the current branch and the specified branch.
  git diff branch_name
Enter fullscreen mode Exit fullscreen mode
  • Merge changes from another branch into the current branch: To integrate changes from a branch into your current branch, use this command. Replace branch_name with the name of the branch you want to merge.
  git merge branch_name
Enter fullscreen mode Exit fullscreen mode

What is a Merge Conflict?

Merge conflicts occur when multiple people edit the same line of code or when one person edits a file and another deletes the same file. Git cannot automatically resolve such conflicts and requires manual intervention.

To resolve merge conflicts:

  1. Open the conflicting files in your code editor.
  2. Look for conflict markers (<<<<<<<, =======, and >>>>>>>).
  3. Edit the file to retain the desired changes and remove the conflict markers.
  4. Stage the resolved file using git add.
  5. Commit the resolved changes with git commit.

What is git log?

The git log command displays the history of committed snapshots in your repository. It provides details such as:

  • Commit hashes
  • Author names
  • Commit dates
  • Commit messages

This is useful for tracking changes, understanding project history, and identifying specific commits.

  • View commit history: Running git log without any options shows the complete commit history.
  git log
Enter fullscreen mode Exit fullscreen mode
  • Filter commits: You can filter commits based on criteria like author, date, or keywords in commit messages. For example, to view commits by a specific author:
  git log --author="Author Name"
Enter fullscreen mode Exit fullscreen mode

What is git reset?

The git reset command is a powerful tool in Git used to undo changes by modifying the commit history, the staging area, and the working directory. It has three main modes:

  • --soft: Moves the HEAD pointer to a previous commit, keeping changes staged for recommitting.
  • --mixed: Resets the HEAD pointer to a previous commit and unstages changes, allowing them to be reviewed before recommitting.
  • --hard: Resets the HEAD pointer to a previous commit, discarding all changes made since that commit. This action is irreversible.

Common git reset Commands:

  • Reset all files in the staging area: Use this command to unstage all files that are currently staged.
  git reset
Enter fullscreen mode Exit fullscreen mode
  • Reset a specific file: If you want to unstage a particular file, use:
  git reset file_name
Enter fullscreen mode Exit fullscreen mode
  • Move the HEAD pointer back by n commits: This command reverts the HEAD pointer to a previous state while preserving changes in the working directory for further use. Replace n with the desired number of commits.
  git reset HEAD~n
Enter fullscreen mode Exit fullscreen mode
  • Reset to a specific commit using its hash: You can reset the HEAD pointer to a specific commit using its unique hash value. Use git log to find the commit hash.
  git reset commit_hash
Enter fullscreen mode Exit fullscreen mode

Modes of git reset Explained with Commands:

  • git reset --soft: Moves the HEAD pointer to a prior commit, keeping all changes staged for recommitting. Use this when you want to amend or restructure your commits.
  git reset --soft commit_hash
Enter fullscreen mode Exit fullscreen mode
  • git reset --mixed: Resets the HEAD pointer to a previous commit and unstages changes, allowing you to review them before recommitting.
  git reset --mixed commit_hash
Enter fullscreen mode Exit fullscreen mode
  • git reset --hard: Completely resets the HEAD pointer to a prior commit, discarding all changes made since that commit. This action cannot be undone, so use it cautiously.
  git reset --hard commit_hash
Enter fullscreen mode Exit fullscreen mode

“A guide can illuminate the path, but it's your consistent practice and curiosity that will lead you to mastery.”

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more