Complete Git and GitHub Tutorial for Beginners
NOTES MADE BY YOUSIF SANI
Hi Raman! I'm your Shraddha didi, and welcome to today's tutorial. We're going to dive
deep into Git and GitHub, exploring their functionalities from scratch. Even if you have no
prior knowledge of what Git or GitHub are, don't worry. We'll cover everything from the very
basics to a good level of proficiency.
Overview
Git is a crucial tool every developer should be familiar with. It helps in version control,
tracking changes to your code, and managing collaborative projects effectively.
What is Git?
Git is a version control system (VCS). Think of it as a record keeper for your code, tracking
every change and storing a history of what happened when. This is similar to a bank
statement that records every transaction. Git helps you manage and track changes in your
projects.
Why Git?
• Popularity: Git is one of the most popular version control systems.
• Industry Standard: Widely used in companies for project management.
• Free and Open-Source: Available for everyone to use without cost.
• Scalability: Works well for both small and large projects.
GitHub
GitHub is a platform that uses Git for managing and storing code online, allowing
collaboration with others.
Basic Git Commands
We've prepared a cheat sheet with all the essential Git commands you need to get started.
Download it to keep handy as you learn.
Key Concepts
• Branches: Used to create separate lines of development.
• Commits: Saving your changes in Git.
• Repositories: Storage space for your project files.
Practical Application
We'll start with practical exercises to familiarize you with Git commands. For example, let's
say you're working on a project, adding features like a signup page, buttons, or a help form.
With Git, you can track all these changes, revert back if needed, and manage your project
efficiently.
Getting Started
1. Install Git: Visit the Git website to download and install Git.
2. Create a GitHub Account: Sign up on GitHub to start using their platform.
3. Set Up Git:
Absolutely, Git and GitHub are powerful tools for version control and collaboration, and
they are essential for any developer. Let's break down what you've mentioned and clarify
how these tools work and why they are beneficial.
Why Use Git?
1. Version Control:
o Track Changes: Git keeps a detailed history of every change made to your
code. This allows you to revert back to any previous state without losing your
work.
o Automatic History: Instead of manually tracking changes, Git automatically
records what changes were made, who made them, and when.
2. Collaboration:
o Teamwork: When multiple developers work on the same project, Git helps
manage and merge changes from different team members without conflicts.
o Branching: Developers can create branches to work on features
independently, then merge them back into the main project.
3. Efficiency:
o Fast and Reliable: Git is designed to handle projects of all sizes, from small
personal projects to large-scale enterprise applications.
o Free and Open Source: Git is free to use, making it accessible to everyone.
Why Use GitHub?
GitHub enhances the capabilities of Git by providing an online platform to store and
manage your code:
1. Centralized Repository:
o Store Code Online: GitHub provides a central place to store your code,
making it accessible from anywhere.
o Collaborate Remotely: Teams can collaborate on projects regardless of
their physical location.
2. Project Showcase:
o Portfolio: Uploading projects to GitHub allows you to showcase your work to
potential employers or collaborators.
o Verification: Provides a way for others to verify your work and contributions.
3. Community and Integration:
o Open Source Projects: Contribute to open-source projects and collaborate
with a global community of developers.
o Integration: Integrates with various tools and services for continuous
integration, deployment, and project management.
Creating a GitHub Account and Repository
In this tutorial, let's walk through the steps to create a GitHub account and set up your first
repository. GitHub is a platform that uses Git for managing and storing code online,
allowing collaboration with others.
Creating a GitHub Account
1. Sign Up for GitHub:
o Go to the GitHub sign-up page.
o Enter your email address. You can use a personal email address, which is
recommended because college email IDs might expire after graduation.
o Choose a strong password and continue with the sign-up process.
o Enter a username. This can be your name or any unique identifier you prefer.
o Decide if you want to receive product-related emails from GitHub.
o Verify that you are a human by completing the CAPTCHA.
o GitHub will send a verification code to your email. Enter the code to verify
your email address.
2. Setting Up Your Profile:
o Once your account is verified, GitHub may ask for some basic information
like the number of team members and your role (e.g., student).
o You can skip certain steps if they are not relevant.
Navigating the GitHub Dashboard
1. Dashboard Overview:
o Once signed in, you'll be taken to your GitHub dashboard. This might look a
bit overwhelming at first, but don't worry, we'll break it down.
o Profile: On the top right, you can access your profile where you can see your
contributions, activity, and repositories.
2. Creating a New Repository:
o Click on the + icon at the top right of the page and select New repository.
o Repository Name: Enter a name for your repository, e.g., our-college-demo.
o Description: Add a brief description, such as "This is my first Git repository."
o Public/Private: Choose whether you want your repository to be public
(anyone can see it) or private (only you and people you invite can see it).
3. Initializing the Repository:
o You can initialize the repository with a README file, which is a good practice
as it provides an overview of your project.
o Click Create repository to complete the setup.
Public vs. Private Repositories
• Public Repositories:
o Visibility: Everyone can see them, similar to a public Instagram profile.
o Sharing: Great for open-source projects and sharing your work with the
community.
• Private Repositories:
o Visibility: Only you and the collaborators you add can see them, like a
private story on Instagram.
o Security: Ideal for projects you don’t want the public to see.
Creating and Initializing a Repository
1. Create a Repository:
o Go to your GitHub profile and click on Repositories.
o Click New to create a new repository.
o Enter a repository name (e.g., our-college-demo).
o Add a description if you like (e.g., "This is my first Git repository").
o Choose between public or private.
o Check the box to initialize the repository with a README file.
2. Understanding README.md:
o A README.md file is essential for documenting your project.
o It typically contains:
▪ Project title and description.
▪ How to install and use the project.
▪ Features and additional information.
o GitHub will automatically create this file if you check the initialize option.
Navigating Your Repository
1. Your GitHub Dashboard:
o After creating your repository, you’ll see it listed in your profile under
Repositories.
o Clicking on the repository name will take you to the project’s main page.
Committing Changes
• Commit: Think of a commit like taking a snapshot of your project at a specific point
in time.
• Stages of Committing:
1. Change: Modify your file(s).
2. Add: Stage your changes for commit.
3. Commit: Finalize the changes with a message.
Example Workflow in GitHub
1. Modify the File:
o Edit README.md as needed.
o Describe the changes in a commit message.
o Click Commit changes.
2. Viewing Commit History:
o Click on the Commits tab to see a history of all the commits made to the
repository.
Key Terms
• Commit: A record of changes made to the repository.
• Branch: A separate line of development.
• Merge: Combining changes from different branches.
• Pull Request: A request to merge changes from one branch into another.
Understanding README.md and Basic Git/GitHub Setup
Now that we've gone over the basics of creating a repository and adding an initial commit,
let's dive into some additional details and steps to set up your development environment.
Working with README.md
Setting Up Git on Your Local Machine
To work with Git locally, you need to set it up on your machine. Here’s how:
1. Download Visual Studio Code:
o Visit the Visual Studio Code download page and download the version for
your operating system (Windows, macOS, or Linux).
Configuring Git and Using Visual Studio Code
Now that we've installed Git, let's configure it and start using it with Visual Studio Code.
Configuring Git
Configuring Git involves setting up your username and email address. This information is
used in commit messages to identify who made specific changes.
Using Visual Studio Code
Visual Studio Code (VS Code) is a versatile code editor that integrates well with Git. Let's
set it up for use with Git.
1. Open Visual Studio Code:
o Open VS Code on your computer.
2. Open a Folder:
o Click on File > Open Folder... and select a folder to work with. For example,
you might have a folder named git-demo.
3. Open Terminal in VS Code:
o You can open the integrated terminal in VS Code by clicking on Terminal >
New Terminal. This will open a terminal at the bottom of the VS Code
window.
Basic Git Commands
Let's start with some basic Git commands, including clone and status.
1. Clone a Repository:
o Cloning a repository means copying it from GitHub to your local machine.
o Go to your GitHub repository (e.g., our-college-demo) and click the green
Code button.
Understanding File Statuses
• Modified Files: When you change a file, Git marks it as modified.
• Staging Files: Before committing, you need to stage the modified files.
Using git status
The git status command helps you see the current state of your working directory and the
staging area.
1. Example Output:
o The output will show you:
▪ The current branch.
▪ Files that have been modified but not staged.
▪ Files that are staged and ready to be committed.
Modifying and Staging Files
Understanding Git Add and Commit
Let's delve deeper into how we manage and track changes in Git using the add and commit
commands. These steps ensure your changes are properly recorded and maintained.
Git File Statuses
1. Untracked Files: Files that are new and not yet being tracked by Git.
2. Modified Files: Files that have been changed but not yet staged for commit.
3. Staged Files: Files that have been added to the staging area and are ready to be
committed.
Working with Git
Creating a New Local Repository and Pushing to GitHub
Let's walk through the process of creating a new local repository, making changes, and
pushing those changes to a remote repository on GitHub.
Creating and Initializing a Local Repository
Adding Files to the Repository
1. Create and Edit Files:
Staging and Committing Changes
1. Stage the Files:
Creating a Remote Repository on GitHub
1. Create a New Repository on GitHub:
o Go to GitHub, navigate to your profile, and create a new repository.
o Name the repository (e.g., local-repo), add a description, and choose
whether it should be public or private.
o Do not initialize the repository with a README, .gitignore, or license.
2. Add the Remote Repository:
o Copy the HTTPS URL of the new repository from GitHub.
Setting Upstream with git push -u
Using the git push -u origin main command sets the upstream branch, meaning you won't
need to specify the branch name in future git push commands. Here's a detailed
explanation and further steps on managing branches:
Setting Upstream
Managing Branches
Branches allow you to work on different features or fixes independently. Let's explore
creating, switching, and deleting branches.
1. Creating a New Branch:
Branching and Merging
When you finish working on a feature branch, you might want to merge it back into the main
branch.
Working with Branches and Pull Requests
Great! Let's walk through the process of making changes in a feature branch, pushing those
changes to GitHub, and then merging those changes into the main branch using pull
requests.
Making Changes in a Feature Branch
1. Switch to Feature Branch:
Pushing Changes to GitHub
1. Push to Remote Repository:
Creating a Pull Request on GitHub
1. Go to GitHub Repository:
o Navigate to your GitHub repository where your project is hosted.
2. Create a Pull Request:
o GitHub will often display a prompt to create a pull request for the recently
pushed branch.
o Alternatively, click on the Pull requests tab, then click the New pull request
button.
3. Select Branches to Merge:
o Compare feature-one branch with the main branch.
o Click Create pull request.
4. Add Details and Submit:
o Add a title and description for your pull request. Explain the changes you
made.
o Click Create pull request.
Reviewing and Merging the Pull Request
1. Review the Pull Request:
o A team member or project manager will review the changes.
o They can comment on the code, request changes, or approve the pull
request.
2. Merge the Pull Request:
o Once approved, you or the reviewer can merge the pull request.
o Click the Merge pull request button, then Confirm merge.
3. Delete the Feature Branch:
o After merging, you can delete the feature-one branch to keep your repository
clean.
o GitHub will often prompt you with a Delete branch button after the merge.
Checking for Differences and Merging via Command Line
Managing Conflicts in Git
When working with multiple branches, you might encounter conflicts that Git can't
automatically resolve. Let's go through the steps to manage these conflicts and
ensure a smooth merge process.
Conflict Resolution
1. Identify the Conflict:
o When you try to merge two branches and there's a conflict, Git will notify
you and pause the merge process.
Resolve the Conflict:
• You have a few options for resolving conflicts:
o Accept Current Change: Keep the changes from the current branch.
o Accept Incoming Change: Keep the changes from the incoming branch.
o Accept Both Changes: Combine both changes manually.
Edit the Conflict Markers:
• Remove the conflict markers (e.g., <<<<<<<, =======, >>>>>>>) and manually
edit the file to incorporate the desired changes.
Merging Branches and Updating Local Repo
1. Merge Feature Branch into Main:
Undoing Changes in Git
Sometimes, we might add changes or commit something by mistake that we need to undo.
Git provides several ways to undo changes depending on the situation.
Undoing Staged Changes
1. Resetting Staged Changes:
o If you've added changes but haven't committed them yet, you can unstage
them using git reset.
Undoing Committed Changes
1. Resetting the Last Commit:
o If you have committed changes that you want to undo, you can reset the
commit using git reset.
Reverting Changes
1. Reverting a Commit:
o If you want to undo a commit but keep the history, you can use git revert.
Forking a Repository on GitHub
Forking a repository creates a copy of someone else's repository in your GitHub account.
This is useful when you want to contribute to an open-source project or collaborate on a
friend's project.
1. Find the Repository:
o Go to GitHub and search for the repository you want to fork. For example,
search for "express".
2. Fork the Repository:
o Click on the Fork button at the top right of the repository page.
o GitHub will create a copy of the repository in your account.
o You can rename the forked repository if needed.
3. Clone the Forked Repository:
orking a Repository and Creating Pull Requests
Forking a repository is a fantastic way to collaborate on projects and contribute to open-
source software. Let's go over the steps in detail.
Forking a Repository
1. Find the Repository:
o Navigate to the repository you want to fork on GitHub. For example, search
for "express" to find the Express repository.
2. Fork the Repository:
o Click on the Fork button at the top right corner of the repository page.
o Select your account to create a copy of the repository in your GitHub
account.
3. Copy is Created:
o Wait for GitHub to create a fork of the repository. This might take some time
for larger projects.
o Once the fork is created, you will see the copied repository in your account.
Making Changes to the Forked Repository
1. Commit Changes:
o After making your changes, commit them to the repository. Ensure you
provide a meaningful commit message.
Creating a Pull Request
1. Create a Pull Request:
o Navigate to the original repository where you want to propose the changes.
o Click on the Pull requests tab, then the New pull request button.
o Select the branches to compare. Your base branch will be the original
repository's branch (e.g., master), and the compare branch will be your
forked repository's branch.
2. Add Details and Submit:
o Add a title and description for your pull request. Explain the changes you
have made and why they are useful.
o Click Create pull request.
3. Review and Merge:
o The project maintainers will review your pull request. They might request
changes or approve the pull request and merge it into the main project.
Best Practices for Pull Requests
1. Make Meaningful Changes:
o Only create pull requests for changes that add value to the project, such as
fixing bugs or adding features.
2. Keep Pull Requests Small:
o Break down larger changes into smaller pull requests that are easier to
review and merge.
3. Follow Contribution Guidelines:
o Many repositories have contribution guidelines. Make sure you follow them
to increase the chances of your pull request being accepted.