Git Prodigy
Git Prodigy
Ebenezer Don
This book is for sale at http://leanpub.com/git-prodigy
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1. Introduction to Version Control . . . . . . . . . . 4
What is Version Control? . . . . . . . . . . . . . . . 5
Why is Version Control Important? . . . . . . . . 8
Introduction to Git . . . . . . . . . . . . . . . . . . . 11
Other Version Control Systems . . . . . . . . . . . 19
Installing Git . . . . . . . . . . . . . . . . . . . . . . . 23
2. Getting Started with Git - Command Line and
GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Basic Git Commands . . . . . . . . . . . . . . . . . . 29
CLI vs GUI: Understanding the Differences . . . 34
Performing Basic Commands in VSCode . . . . . 38
Creating Your First Repository and Making Your
First Commit . . . . . . . . . . . . . . . . . . . . 42
Viewing the Commit History and Reverting To
A Previous Commit . . . . . . . . . . . . . . . . 46
CONTENTS
growth.
In this journey, you’ll learn more than just commands
and features. You’ll understand the philosophy behind
Git, GitHub, and open source—the why, not just the
how. Whether you’re a beginner hoping to grasp the
basics, an experienced developer aiming to hone your
skills, or an open-source enthusiast seeking to contribute
more effectively, you’ll find “Git Prodigy,” a valuable
companion.
Let’s get started.
1. Introduction to
Version Control
This chapter sets the foundation for understanding the
importance and role of version control in software devel-
opment. We’ll delve into Git, one of the most widely used
version control systems, and explain the key differences
between Git and GitHub. We’ll end the chapter with a
guide on how to install Git.
want. And the best part? It’s all in one place; you don’t
need multiple versions of the same file cluttering your
workspace.
There are two types of version control systems: Central-
ized and Distributed.
Simplifying Collaboration
When working on a software project with a team, things
can get messy if you don’t have a good system in place.
Imagine multiple people making changes to the same files
- it’s a recipe for disaster without version control.
Let’s say you and your colleague are both working on
different features of a mobile app. Version control enables
you both to work on the same codebase without inter-
fering with each other’s work. When you’re both done,
1. Introduction to Version Control 10
you can combine your changes into the main project, and
if there are any conflicts, the version control system will
help you resolve them.
In essence, version control is not just a good-to-have, it’s a
must-have for any software development process. It’s
like a safety net, a time machine, and a team coordinator
all rolled into one. In the next section, we’ll start our
journey into one of the most popular version control
systems - Git.
1. Introduction to Version Control 11
Introduction to Git
Now that we’ve established how critical version control
is for software development let’s delve into Git. Git is
a Distributed Version Control System (DVCS) that has
become an industry standard for both small and large-
scale projects. In this section, we will lay the groundwork
for understanding what Git is, how it differs from other
version control systems, and why it has become so popular
among developers.
What is Git?
Git is a distributed version control system designed to
manage the source code history of software development
projects. It was created by Linus Torvalds in 2005 to
manage the development of the Linux kernel. Since then,
it has become the most widely used version control system
in the world.
The term “distributed” in its architecture means that each
user’s working copy of the codebase includes the full
history of changes and commits. Unlike Centralized Ver-
sion Control Systems (CVCSs) where there’s one central
repository, in Git, every developer’s working copy is a
repository.
1. Introduction to Version Control 12
In Summary:
Git is not just an ordinary tool—it’s an entire ecosystem
that stands on its own. The combination of its decen-
tralized structure, exceptional efficiency, and flexibility
makes it the favored version control system among many
developers. As we progress through this book, you’ll ob-
tain hands-on knowledge and understand how to harness
Git’s potential to manage your code effectively.
1. Introduction to Version Control 15
Understanding Git
As we discussed in the previous section, Git is a version
control system. It is a tool that manages the source code
history and allows multiple developers to work on a single
project without stepping on each other’s toes. Git is
software that you install locally on your computer, and
it works primarily from the command line (though there
are also graphical user interfaces for Git).
Key Points about Git:
Understanding GitHub
GitHub, on the other hand, is a web-based platform that
uses Git for version control. Think of it as a social
networking site for coders and their code. It allows you
to upload your Git repositories online, making it easier
to collaborate with others. GitHub provides additional
features such as Pull Requests, Issues, and Wikis for your
repositories.
Key Points about GitHub:
In Summary:
Git is the tool, and GitHub is the service that hosts your
repositories and enhances collaboration. They are distinct
but complementary. While you can use Git without using
GitHub, utilizing them together creates a more powerful
and efficient workflow, especially for collaboration on
larger projects or open-source contributions. Next, we’ll
look at other version control systems and how they com-
pare to Git.
1. Introduction to Version Control 19
Subversion (SVN)
Subversion, also known as SVN, is a centralized version
control system initially developed by CollabNet Inc in
2000, and now maintained by the Apache Software Foun-
dation. Unlike Git, which is a distributed system, SVN
follows a more linear approach to version control.
While SVN does support branching and merging, the
mechanism is quite different and can be more cumber-
some compared to Git. SVN requires a connection to the
central repository for commits, but it can track changes
offline to some extent. Some teams find SVN straight-
forward and suitable when transitioning from a no-VCS
setup, but it lacks some of the robust features offered by
distributed systems like Git.
1. Introduction to Version Control 20
Mercurial
Mercurial is a distributed version control system like Git,
but it was designed with simplicity in mind. Its commands
and workflow are more straightforward, making it easier
to learn, especially for those new to version control.
While Mercurial supports most of the basic features of Git,
such as local commits and easy branching, its simplicity
does come at the cost of fewer features by default. How-
ever, it does have a robust plugin system that allows you
to extend its functionality.
Perforce
Perforce, or Helix Core, is a version control system that
is widely used in the gaming industry. It excels in
handling large binary files, making it an excellent choice
for projects like game development, where you’re dealing
with 3D models, textures, and other large assets.
However, Perforce operates as a centralized version con-
trol system and is proprietary software. So depending
on your project’s needs or team size, you might need to
purchase a license.
Unlike Git, Perforce lacks a distributed architecture and
does not support local repositories in the same way that
1. Introduction to Version Control 21
Git does.
Installing Git
Now that we’ve learned what version control is and have
a basic understanding of Git, it’s time to start using
Git practically. The first step is to install Git on your
computer. This section will guide you through the process
of installing Git on Windows, macOS, and Linux.
Configuring Git
After installing Git, it’s a good idea to configure it with
your information.
Wrapping Up
Congrats! You have successfully installed and configured
Git on your system. You’re now ready to start creating
repositories, making commits, and delving deeper into
version control. In the next chapter, we’ll take a closer
look at creating your first repository and making your first
commit. Let’s dive right in!
2. Getting Started
with Git - Command
Line and GUI
In this chapter, we’ll discuss how to interact with Git, both
through the command line Interface (CLI) and Graphical
User Interface (GUI) tools, with a focus on understanding
when to use each. We’ll walk through some basic Git
commands and use Visual Studio Code to perform them
in a GUI. This chapter concludes by guiding you through
creating your first Git repository and making your first
commit.
git init
$ git init
git clone
git add
$ git add .
$ git add -A
git commit
git status
$ git status
git log
$ git log
Disadvantages:
Disadvantages:
Cloning a Repository
Instead of using git clone in the command line, in
VSCode you can:
Staging Changes
Remember git add in the command line? Here’s how to
do it in VSCode:
Committing Changes
Instead of using git commit in the command line, in
VSCode you can:
$ mkdir my-first-repo
$ cd my-first-repo
$ git init
$ git status
$ git log
commit a4d5b8d8ed4b2e0b4567df2b2
Author: Your Name <your.email@example.com>
Date: Thu Jun 18 10:45:00 2025 +0000
And that’s it! Now you know how to view and revert
commits in your repository, and how you can use this
powerful feature to undo mistakes or simply explore what
the project looked like at any point in its history. Un-
derstanding these concepts is fundamental in using Git
effectively for version control. And whether you prefer
2. Getting Started with Git - Command Line and GUI 50
Understanding Branches
If you think of your Git repository as a tree, your project
starts as a single trunk, growing taller with each commit
you make. However, sometimes you might want to try
out new ideas, make some experimental changes, or work
on a new feature without affecting the main part of the
project. In such cases, branches come to the rescue.
Imagine your project as a tree again, but this time, be-
sides the main trunk, it has smaller branches sprouting
out. Each branch represents a new timeline, allowing for
independent development and experimentation. This also
helps with collaboration, as multiple people can work on
different branches simultaneously without affecting each
other’s work. Once you’re satisfied with the changes in a
branch, you can merge it back into the main trunk.
Main Branch
By default, every Git repository has a main branch, often
called the master branch (or main in more recent repos-
itories). This is usually considered the definitive, stable
version of your project. All other branches you create will
be based on this branch, or a branch that was based on this
one, and so on.
3. Branching and Merging in Git 53
Feature Branches
When you’re working on a new feature or an experimen-
tal idea, you create a new branch, commonly known as
a feature branch. The feature branch is created from the
main branch, so it contains all its code and history at the
point of creation. This allows you to work on your new
ideas separately. If your experiments are successful, you
can later integrate them back into the main branch. If not,
you can simply discard the branch without any harm done
to your project.
$ git branch
feature-x
* main
When you switch back to the main branch, you will notice
that the new line you added to the README.md file is no
longer there. This is a good way to keep your main branch
clean and stable while you work on new features.
Let’s learn how to merge branches so that we can integrate
the changes from the feature-y branch into the main
branch.
3. Branching and Merging in Git 58
Merging Branches
Once you’ve completed your work on a feature branch,
the next step is to incorporate those changes back into the
main branch, or another branch if needed. This is known
as merging.
Let’s continue with our example. We made some changes
in the feature-y branch, and now we want to integrate
those changes into the main branch. To do this, you need
to be on the main branch. That’s because when we merge,
the changes are applied to the currently active branch. If
you’re not already on the main branch, switch to it using
the git checkout command.
With the main branch active, we can use the git merge
command to merge the changes from the feature-y
branch:
Fast-Forward Merges
In our example, since there were no new changes in the
main branch after we created the feature-y branch, Git
performed a “fast-forward” merge. This means that Git
simply moved the main branch pointer to the last commit
of the feature-y branch, without needing to create a
separate merge commit.
Merge Commits
However, sometimes both the branch you are merging
into and the branch you are merging from might have
changes that diverge. In such cases, Git will create a
new commit that brings together the changes from both
branches. This is known as a merge commit.
Auto-merging README.md
CONFLICT (content): Merge conflict in REA\
DME.md
3. Branching and Merging in Git 63
This means that Git was not able to resolve the differences
automatically, and it’s waiting for you to step in and make
the decision.
<<<<<<< HEAD
This is the change on the current branch.
=======
This is the conflicting change on the bra\
nch you're merging.
>>>>>>> feature-y
What is GitHub?
Wow, you’ve come a long way already! You’ve taken
your first steps into the world of version control, learned
about Git, its core concepts like commits, branching, and
merging, and even used both the command line and a
graphical user interface with VSCode. That’s impressive,
and you should be proud of yourself!
Now it’s time to up the ante and dive into a platform that
brings even more power to your version control skills –
GitHub. If you recall, we touched on GitHub in the first
chapter, but let’s dig a bit deeper and see why it’s such a
crucial tool for developers around the world.
GitHub is an online platform that utilizes Git for version
control and collaboration. It allows you to work on
projects with other people, keep track of issues, and even
host web pages. Essentially, it takes all the version control
goodness from Git and wraps it in a user-friendly interface
that’s accessible from anywhere.
Alternatives to GitHub
While GitHub is incredibly popular and widely used
among developers for hosting Git repositories, it’s not the
only option available. There are other platforms that offer
similar functionalities, with some distinct advantages:
GitLab
Bitbucket
SourceForge
And there you have it! You’re now the proud owner of
a GitHub account. Take a moment to explore, customize
your profile, and maybe even jot down ideas for your first
project.
Next, we’ll get acquainted with the GitHub interface.
Remember that practice makes perfect, so don’t be afraid
to click around and learn by doing.
4. Introduction to GitHub 79
GitHub Homepage
Upon logging into GitHub, you land on your dashboard.
Here you’ll find:
Navigation Bar
Regardless of where you are on GitHub, there’s a constant
Navigation Bar. It typically contains:
4. Introduction to GitHub 80
Profile Page
Access your profile by clicking on your profile picture and
selecting “Your profile”. Your profile page is where you
can:
Repository Page
Clicking on a repository, yours or someone else’s, will take
you to its page. This page usually contains:
Account Settings
To reach your account settings, click on your profile
picture and select “Settings”. This is where you can set
your preferences, such as email notifications and account
security.
As you spend more time on GitHub, you’ll become more
comfortable with the platform. It’s a powerful tool, so take
your time and explore its many features. We’ll discuss
creating your first GitHub repository and using GitHub
for version control in the next section. Get ready!
4. Introduction to GitHub 83
Understanding Remote
Repositories
You’ve learned about repositories, or “repos”, in the con-
text of Git. These repositories contain your project’s files
and the history of changes made to them. When working
with Git, you’ve been dealing with local repositories that
reside on your computer. Remote repositories, on the
other hand, are versions of your project that are hosted
on the internet or another network.
Remote repositories serve several purposes:
1. Open VSCode.
• 6.5 Issues
Understanding Collaborators
and Permissions
By now, you’ve gained a solid understanding of how
to work with Git and GitHub. You’ve learned to make
commits, manage branches, and synchronize your local
repository with a remote one on GitHub. As we’ve men-
tioned before, one of the main advantages of using GitHub
is its capacity to facilitate collaboration. So, let’s dive into
that aspect, starting with the concept of collaborators and
permissions.
In GitHub, a collaborator is a person who has been granted
permission to a repository. This role isn’t limited to
coding; collaborators can contribute to the project in var-
ious ways, including writing documentation, testing, and
managing issues and pull requests (which we’ll discuss in
detail later in this chapter).
Permissions control what collaborators can and can’t do
in a repository. There are two main types of access
permissions in GitHub:
Inviting Collaborators to a
Repository
As you work on your projects, you’ll likely want to invite
others to collaborate with you. This could be colleagues
or friends who you want to work with on a project. Let’s
learn how you can invite collaborators to a repository and
manage their permissions right from GitHub. Remember,
these steps might vary slightly depending on how much
GitHub changed its interface since this guide was written,
but the general idea should be the same.
Forking a Repository
Forking is one of the fundamental concepts on GitHub that
allows you to create a copy of a repository under your own
account. This enables you to experiment with changes
without affecting the original project. It’s especially
popular in open-source projects, where it’s often used as
a way to contribute. Let’s dive into what forking is, why
you might want to do it, and how it’s done.
Pull Requests
Pull requests are an essential part of the collaborative
development process, especially on GitHub. They are a
way to propose changes from one repository to another,
typically from a fork back to the original repository, or
between branches in the same repository. They not only
suggest changes but also provide a way to review and
discuss these proposed changes before they are integrated.
Issues
Now that we have a good understanding of pull requests
and their importance in proposing and reviewing changes,
let’s discuss another essential aspect of project collabora-
tion on GitHub - Issues.
Issues are a great way to keep track of tasks, enhance-
ments, and bugs for your projects. They act like a con-
versation thread where you can discuss these items with
your team or contributors, assign people to tasks, and keep
track of progress.
will create a reference link in the issue, and once the pull
request is merged, issue number 12 will automatically be
closed. The issue number is different from the issue title.
You’ll see the issue number beside the title after you open
an issue.
Code Review
Code reviews are a critical practice in modern software
development, designed to catch bugs, improve code qual-
ity, and spread knowledge among team members. On
GitHub, code reviews happen in the form of comments
on a pull request.
When a pull request is opened, it’s common for the
maintainers of the project or other collaborators to review
the changes. This can involve several steps:
Merging on GitHub
Once the code review is done and any necessary changes
have been made, it’s time to merge the pull request.
6. Collaborating on GitHub 127
been fulfilled.
Repository Organization
A well-structured repository makes your project easy to
understand and navigate. Here are a few recommenda-
tions for organizing your repository:
yarn.lock
package-lock.json
README Files
The README is one of the most important documents
in your repository. It serves as the first point of contact
for anyone who stumbles upon your project and as such,
should be clear, informative, and inviting. Here are some
things you should include in your README:
MIT License
The MIT License is one of the most permissive and
widespread open-source licenses. It permits users to
do almost anything with the code, including using it in
proprietary software, provided that the original copyright
notice and the license’s text is included in all copies or
substantial uses of the software.
8. Licensing and Open Source 155
2 https://choosealicense.com/
8. Licensing and Open Source 157
Communicate Effectively
Communication is key in the open-source community. Be
clear and concise in your messages, whether in issues,
pull requests, or mailing lists. If you are proposing a new
feature or change, explain why it’s necessary and how you
8. Licensing and Open Source 159
Conclusion
The open-source movement is a testament to the power
of collaboration and shared knowledge. It has shaped the
software industry in unimaginable ways and continues to
be a driving force for innovation. As a contributor, you
have the opportunity to be a part of this change.
Remember, open source is not just code. It’s a community.
It’s about mutual respect, collaboration, and helping each
other to achieve common goals. By participating in open
source, you are joining a global community of individuals
who believe in the power of openness and collaboration.
Whether you’re contributing to improve your coding
skills, to give back to tools you use, or to make a positive
impact, your contributions are valuable. Always be open
to learning and adapting to the practices and conventions
of the community you are joining.
9. Advanced Git
Features
Congratulations on making it this far! By now, you’ve
gained a solid understanding of the basics of Git and
GitHub. You’ve mastered the art of commits, branching,
merging, and collaborating in open source. But the beauty
of Git is its depth - there’s always more to learn. In
this chapter, we’ll dive into some of the more advanced
features of Git, such as stashing changes, rebasing and
rewriting history, using Git hooks, and tracking changes
with Git blame and bisect.
Stashing Changes
Imagine you are in the middle of developing a new feature,
and your working directory is full of changes when a
critical bug is reported. Now, you need to switch branches
to work on a fix. But you can’t switch branches with
uncommitted changes. You don’t want to commit half-
done work, and you don’t want to lose your changes
either. What do you do?
This is where Git stashing comes in. Stashing allows you
to temporarily save changes that you have made but do
not want to commit yet. It’s like putting your changes in
a drawer so you can work on something else, and then
taking them back out when you’re ready. Since you can’t
switch branches with a “dirty” working directory, stashing
is a great way to save your work so that you can come back
to it later.
Or:
9. Advanced Git Features 165
This takes all the modified tracked files and stages them
and saves them on a stack of unfinished changes that you
can reapply at any time. When you stash your changes,
Git resets your working directory to the last commit. At
first, it might look like you’ve lost all your changes, but
don’t worry! They are still there, and you can get them
back whenever you want.
It’s important to note that the behavior of the git stash
or git stash push command can vary depending on
the version of Git you are using and your particular
Git configuration. In many typical scenarios and newer
versions of Git, these commands will stash both staged
and unstaged changes, meaning you don’t necessarily
need to stage your changes before stashing.
If you are using an older version of Git (earlier than
version 2.35), then the git stash command will only stash
staged changes by default. In this case, you will need to
stage your changes before stashing.
To set the stash.default configuration setting to “stash-
all”, so that both staged and unstaged changes will be
stashed by default, you can run the following command:
9. Advanced Git Features 166
You can check the default behavior of the git stash com-
mand by running the following command:
# output
stash-all
You can also check the specific settings in your Git envi-
ronment that affect the behavior of the git stash command
by running the following command:
# output
stash.default=stash-all
This will show you a list of all your stashes, along with a
unique identifier for each stash. You can use this identifier
to reapply or delete a specific stash. The output will look
something like this:
Partial Stashing
Sometimes you might not want to stash all the changes in
your working directory. Git allows you to stash just the
changes in certain files. Use the following command and
list the files you want to stash:
What is Rebasing?
Rebasing is a way of integrating changes from one branch
into another. If that sounds like merging, you’re not
wrong. Both merging and rebasing are methods to inte-
grate changes from one branch into another. However,
they do it in different ways and the result can be quite
different.
In Git, a branch is essentially a pointer to a specific
commit. Each commit, in turn, points to its parent commit
(the commit that came before it). This forms a chain that
9. Advanced Git Features 172
Rewriting History
Along with rebasing, Git also allows you to alter the
commit history in other ways. Using commands such as
git commit --amend and git rebase -i (interactive
rebase), you can change commit messages, combine com-
mits, reorder commits, and even remove commits entirely.
Just like rebasing, rewriting history changes the exist-
ing commit history. This can be dangerous and is not
recommended if you’re working with others, as it can
9. Advanced Git Features 174
#!/bin/sh
Now, every time you try to commit, this script will check
the commit message.
Note: Make sure your hook scripts are always tested and
maintained. A broken hook can cause problems with the
repositories it’s installed in.
As powerful as Git hooks are, remember that they are local
to your Git repository and not versioned. This means they
won’t be shared when you push to or clone a repository.
If you need to share automated tasks with others, you’ll
need to use a different method, such as continuous inte-
gration/continuous delivery (CI/CD) pipelines, which are
commonly used for tasks like running tests or deploying
code.
In the next section, we’ll dive into more advanced features
to trace changes and debugging in Git. These are essential
skills for when things start to go wrong.
9. Advanced Git Features 178
Git Blame
git blame is a command that helps you determine who
made changes to a file and what those changes were, line
by line. This can be particularly useful when you’re trying
to understand why a line of code exists or who to ask about
it.
Let’s say you have a file called my_script.py, and you
want to see the revision history for this file. You can use
the following command:
This will print each line of the file, along with the commit
9. Advanced Git Features 179
Git Bisect
git bisect is another powerful debugging tool in Git,
designed to help you find the commit that introduced a
bug into your project. It uses a binary search algorithm,
allowing you to efficiently pinpoint the problematic com-
mit, even among hundreds or thousands of commits. The
binary search algorithm operates by dividing the data set
9. Advanced Git Features 180
5. Repeat as Necessary
Git will continue to checkout commits, narrowing
down the range. Keep testing and marking them as
good or bad until Git points out the first bad commit.
Authentication Issues
When working with remote repositories, especially on
platforms like GitHub, authentication is a key aspect of
ensuring that only authorized users have access to certain
repositories and operations. While it is crucial for secu-
rity, authentication can sometimes be a source of issues
and frustrations. In this section, we’ll explore common
authentication issues and how to resolve them.
Permission Denied
• Wait for a few minutes and then try again with the
correct credentials. Make sure to have them at hand
to avoid another lockout.
10. Troubleshooting Common Git and GitHub Issues 189
$ git reflog
You will see a list of every change you’ve made, each with
its index, and the most recent operation at the top.
To recover a commit, find its SHA (commit hash) in the
list, and then create a new branch with that commit as
the head.
Cherry-Picking a Commit
Sometimes, you might want to apply a specific commit
from one branch into another without merging the entire
branch. This is where the cherry-pick command comes
in handy. It allows you to apply a commit from one branch
to another.
Cherry-picking can be useful when, for example, when
you make a hotfix on a release branch and need to apply
that fix to the main development branch.
10. Troubleshooting Common Git and GitHub Issues 192
Resetting a Branch
The reset command is a powerful tool that can be used
to undo changes to a branch. It can be used to revert
commits, unstage files, and even reset the entire branch
to a previous commit.
For example, you can use reset to undo the last commit
on a branch:
This will undo the last commit and move the HEAD pointer
to the previous commit. The --hard flag can be used to
discard any changes in the working directory.
1 https://wwww.newdev.io
2 https://git-scm.com/doc
3 https://github.blog/
11. Conclusion and Next Steps 200
Join a Community
Being part of a community can greatly enhance your
learning experience:
$ git init
Staging Changes:
• Stage a file
$ git add .
Committing Changes:
Viewing History:
$ git log
$ git status
Branches:
• Delete a branch
Appendix A: Git Command Cheat Sheet 207
$ git branch
Merging:
Remote Repositories:
$ git remote -v
Stashing:
$ git stash
Undoing Changes:
• Unstage a file
• Revert a commit
216
Appendix B: Glossary of Git and GitHub Terms 217