How To Use Git and GitHub - A Guide For Beginners and Experienced Developers
How To Use Git and GitHub - A Guide For Beginners and Experienced Developers
By the end of this journey, you'll have a solid foundation in Git and
GitHub. You'll be equipped with practical knowledge to streamline
your coding workflow, collaborate seamlessly with teams, and
contribute to open-source projects.
So, let's dive in and get started on your Git and GitHub adventure!
Table of Contents
Who is this guide for?
Technologies
Terms
What is GitHub?
Wrapping Up
Whether you're:
just starting your tech career and need to learn the basics of
version control.
Technologies
Before you start, make sure:
Terms
They are a lot of terms around Git and Github that you may meet
when you're working with version control. Let me break it down for
you before we start:
Stage: The area where Git tracks changes that are ready to be
included in the next commit. Files in the staging area are
prepared (staged) for the next commit.
Origin: The default name Git gives to the server from which
you cloned.
Staging Area: Also known as the "Index," it's an area where Git
tracks changes that are ready to be committed.
Index: Another name for the staging area, where Git tracks
changes that are ready to be committed.
What is GitHub?
What is Git?
Git is a free and open-source distributed version control system. It's
designed to handle everything from small to very large projects with
speed and efficiency
Create a branch
Stage changes
Commit changes
Merge changes
Revert changes
Delete a branch
How to Install Git Forum Donate
To set up your name, you need to type the following command in your
terminal:
After entering your name, press Enter to save it. You won't receive
any response, but rest assured, your name has been stored
successfully.
Just like we set up the user name, we also need to set up the user
email. This email will be associated with each commit you make. Let's
learn how to use the git config --global command to set your
email globally on your local machine.
Now that we have finished setting up your username and email for Git
and GitHub, let's verify that everything is configured correctly.
This command will list the username and email being used in the
console for you to see.
Now, let's see how to configure Git to use a default editor by using this
command:
There are two ways to create a repository: using the GitHub website
or the command line. Let's get started. In this section, we'll focus on
creating a repository using the GitHub website and the command line.
After logging into your GitHub account, you can create a new
repository by following these steps:
1. Click on the + sign in the top right corner of the page and
select New Repository from the dropdown menu.
Forum Donate
2. You will be directed to a new page where you can fill in the
details of your new repository. You will need to enter the
following information:
The image above shows the form where you'll fill in the details of your
new repository.
3. Once you have created your new repository, you can start
adding files and making changes to it. You can also connect
your local repository to a remote repository on GitHub by
following the instructions provided on the GitHub website.
The image above shows the code button to copy the URL of your
repository.
The image above shows the command to connect your local repository
to the remote repository on GitHub.
2. Use the git pull command to pull changes from the remote
repository to the local repository.
Forum Donate
The image above shows the command to pull changes from the remote
repository to the local repository.
After that, navigate the main branch by using the following command:
First, I will add some files so that we can start using the Git commands.
2. Use a text editor to make changes to the file. For example, you
can use the code command to open the file in Visual Studio
Code.
3. Once you have made your changes, save the file and close the
text editor.
The image above shows the new file I added which is a React and
TypeScript app.
Visual Studio Code (VS Code) includes a source control feature that
allows you to interact directly with your GitHub repository. This
feature supports a variety of operations, including staging,
committing, pushing, and pulling changes.
In addition to the source control feature, you can also use the
integrated terminal in VS Code to interact with your GitHub
repository.
Next, let's explore how to use the terminal to interact with our GitHub
repository.
Now, let's use the git status command to check the status of the
current branch.
How to Check the Status of Forum the Donate
CurrentLearn
Branch
to code — free 3,000-hour curriculum
The git status command shows the status of the current branch,
including any changes that have been made to the files in the
repository. It provides information about which files have been
modified, which files have been staged, and which files are untracked.
The image above shows the command to check the status of the
current branch.
You may notice that parts of the file are highlighted in different
Forum colors.
Donate
The red color indicates that the file has been modified, while the
Learn to code — free 3,000-hour curriculum
green color signifies that the file has been added to the staging area.
Let's add the file to the staging area using the git add command.
If you want to add a specific file, use the git add <file-name>
command, replacing <file-name> with the name of your file. This
process is known as staging, which prepares files for the next commit.
or
Think of it like this: getting into the car is like adding files to the
staging area, and driving the car is like making a commit.
Now, let's use the git commit command to commit the changes
Forum to the
Donate
current branch.
Learn to code — free 3,000-hour curriculum
$ git push origin main # For example, git push origin master
Before we start adding files, let's ensure our local repository is up-to-
date with the remote repository by pulling any changes.
If the term branch seems unfamiliar, don't worry. In the next section,
we'll cover how to create a branch and how to pull changes from the
remote repository to the local repository.
The git branch command creates a new branch but does not switch
to it. To switch to your newly created branch, use the git checkout
command.
# Delete a branch
Let's proceed to the next section, where we'll cover how to create a
pull request. This is a crucial step in the collaborative workflow, as it
allows you to propose changes and request a review from other
collaborators.
How to Create a Pull Request
Forum Donate
For instance, let's say you have a branch named feature-branch and
you want to merge it into the main branch. We'll walk you through
how to create a pull request for this scenario. Let's get started.
First, let's make a change to our feature branch by adding a file to it:
$ touch feature-branch-file.txt
Forum Donate
After running the command, you should see a new file called feature-
branch-file.txt in your directory.
This command adds the text "This is a file in the feature branch" to the
feature-branch-file.txt file.
The echo command is used to add content to a file. In this case, we're
adding the text "This is a file in the feature branch" to the feature-
branch-file.txt file.
Now that we have some text in the file, let's stage and commit the
changes to the feature branch.
$ git add .
The git add . command stages all the changes in the current
Forum Donate
directory.
Learn to code — free 3,000-hour curriculum
The git push command is used to push changes from your local
repository to the remote repository on GitHub. Replace feature-
branch with the name of your branch. In this case, we're pushing the
changes to the feature-branch branch.
The image above shows the Compare & pull request button on
GitHub.
After clicking on the Compare & pull request button, you will be
directed to a new page where you can fill in the details of your pull
request.
The image above shows the form to fill in the details of your pull
request.
You can decide to file the details of your pull request or create the pull
request. After creating the pull request, you can view it on the GitHub
website. You can also request a review from specific collaborators and
make changes to your pull request if necessary.
Once your pull request has been reviewed and approved, you can
merge it into the main branch. In our case we not going to file the
form but we are going to create the pull request. Forum Donate
Now that we have created a pull request, let's proceed to the next
section, where we'll cover how to merge a pull request. This is the final
step in the collaborative workflow, as it allows you to incorporate
changes into the main codebase.
In this section, we'll navigate the process of merging a pull request via
the GitHub website.
After creating a pull request, you can merge it into the main branch by
Forum Donate
following these steps:
Learn to code — free 3,000-hour curriculum
After that you should see a message indicating that the pull request
has been successfully merged. You can also delete the branch after
merging the pull request.
Forum Donate
Now you have successfully merged the pull request into the main
branch. You can now delete the feature-branch branch, as it is no
longer needed.
Wrapping Up
Throughout this guide, we've delved into the core concepts of Git and
GitHub, equipping you with a robust understanding of version control
and collaborative practices.
If you read this far, thank the author to show them you care.
Say Thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
Code of Conduct Privacy Policy Terms of Service Copyright
Forum Policy Donate