Uploaded by Tony Chan

A Beginner's Guide to GitHub What You Need To Know

advertisement
A Beginner’s Guide
to GitHub: What
You Need To Know
This guide breaks down how GitHub works, how it compares
to GitLab and how to get started with it — as well as valuable
GitHub best practices for IT pros.
Getty Images
By Christopher Tozzi
NEXT
A Beginners Guide to GitHub: What You Need to Know
TABLE OF CONTENTS
What Is GitHub and What Is It Used For?............................................................... 4
GitHub vs. GitLab: Which One Is Better for Your Enterprise?.................................... 9
How to Use GitHub Personal Access Tokens........................................................ 13
How to Delete a Local or Remote Branch on Git and GitHub.................................... 16
2
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
INTRODUCTION
GitHub calls itself “the complete developer platform to build, scale, and
deliver secure software.” And the numbers are backing up that claim — more
than 83 million developers (and 90% of the Fortune 500) are currently using
GitHub, making it the most popular hosting service for storing and sharing
software.
For those already well-versed in GitHub, the report offers expert advice
to enhance even further your experience with the platform, such as how
to delete — and undelete — both local and remote branches on GitHub, as
well as use Personal Access Tokens as a more secure way of managing
access to GitHub resources than passwords.
If you’re a programmer new to GitHub (or a GitHub user wanting to learn
more tips and tricks to get the most out of the hosting service), this special
report breaks down how GitHub works, how to get started with it, and its
Despite its popularity, however, GitHub isn’t the only hosting service for
developers. For some, GitLab may be a better option. In this report, we also
compare GitHub to GitLab to help you choose which best suits your needs
benefits — as well as valuable GitHub best practices.
— especially if you’re an enterprise that needs to manage software source
code on a large scale.
3
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
What Is GitHub and
What Is It Used For?
Here’s what programmers need to know about GitHub — how to get started with GitHub,
its benefits, and best practices.
I
f you had to pick the single most important
platform for developers to know today,
GitHub would be a good choice. Although
using GitHub — which reports more than 83
million active users as of 2022 — is not strictly
required for modern programming, GitHub
(along with a handful of similar, competing
platforms) has become the de facto hosting
service that programmers use to store and
share software they write.
This article breaks down everything
modern coders need to know about GitHub —
including how it works, what its benefits are,
and how to use GitHub as a version control
and source code management solution.
How Does GitHub Work?
GitHub is a platform for hosting software.
It’s designed primarily for hosting source
code, meaning the “raw,” uncompiled code
that is used to build applications. You can also
upload, store, and share binary files (such as
compiled applications or images) on GitHub if
you wish, but it’s more common to use binary
repositories for this purpose; for example, if
you want to store and distribute container
images, you could use Docker Hub.
GitHub’s core features include support for:
• Storing source code
• Tracking changes to source code over time
• Sharing code with other developers or
end users, if desired
4
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
GitHub provides these features by allowing for each of them. You can then upload GitHub automatically keeps track of the
programmers to configure repositories. and store the various files associated version changes.
Because GitHub is hosted on the internet, it
Typically, each repository is dedicated to with each project — source code files,
a specific software project; for example, if configuration data, documentation, and can be accessed from anywhere. In addition,
multiple developers can work on code in
you’re building multiple applications, you’d so on — in the repository. Whenever you
the same repository at once — a feature
generally create a separate repository modify a file or replace it with a new one,
that is beneficial for projects where more
than one coder is developing software.
GitHub automatically monitors which
developers made which changes to data
inside repositories, so you get a record not
just of how your versions have changed
over time, but also who was responsible for
the changes. And you can revert changes
made by a specific developer, or changes
associated with a certain version, whenever
you want.
GitHub repositories can be either public
(meaning that anyone on the internet can
view and download code hosted in them)
or private (which means they are available
only to specific users). Public GitHub
repositories are a great way to host open
source code, while private repositories are
useful if you want to build software that
5
should be available only to developers
inside your organization or a select group
of external users.
GitHub vs. Git
Most of GitHub’s core collaboration
and version control features derive from
Git. Git is an open source version control
system created by Linus Torvalds (the
developer who introduced the Linux kernel)
to help programmers manage source
code. However, whereas Git is a tool that
works only from the command line, GitHub
provides a web interface for interacting with
Git repositories.
GitHub also provides a variety of additional
features — such as Issues, which helps
developers track their work, and Copilot,
which can automatically generate code using
AI — that are not part of Git.
GitHub Competitors and Alternatives
GitHub is not the only web-based,
Git-centered code hosting solution.
Alternative services, such as GitLab and
Bitbucket, provide approximately the same
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
functionality. The main differences between
these services and GitHub lie in which
add-on features they provide beyond core
Git functionality.
Public clouds also offer services that
are similar in some ways to Git — for
example, Azure Pipelines lets developers
manage and deploy source code, among
other features. However, because these
services aren’t designed to make it easy to
share code with the public at large, it makes
more sense to use them when developing
proprietary software, rather than code that
you want to be publicly accessible.
You can also set up your own GitHub-like
environment by deploying a Git server and
installing a web front end, like GitWeb, to
interact with it. This requires more effort
than using GitHub, where the underlying
infrastructure is provided for you and you
can launch repositories with just a few clicks.
But setting up your own Git server provides
more control because you fully own the host
infrastructure, and you can configure the
software in any way you want.
What Are the Benefits of Using GitHub?
There are many reasons to take advantage
of GitHub:
Social coding: GitHub is sometimes called
a social coding platform because it lets
developers collaborate easily when building
software. Even if you don’t specifically need
to collaborate with other coders, sharing your
code on GitHub allows other developers to
inspect, download, and (if you give them
permission) even modify your code, if
they wish.
Version control: As noted above, GitHub
automatically tracks versioning changes
to code.
Code review: GitHub makes it easy to
review your own code, as well as code written
by others. You can leave comments to
explain why you made a certain change, for
instance, or suggest a way to improve code.
Code sharing: By configuring public
repositories, you can use GitHub to share
code with anyone who may want it.
Documentation: Although GitHub isn’t
a complete documentation tool on its
6
5 Benefits of GitHub
but let’s take a deeper look at how GitHub’s
features work.
enables developers to share
1. GitHub
code and collaborate easily with other
GitHub for version control
Version control is a standard GitHub
feature that is enabled by default, for free.
Using version control, you can view changes
GitHub automatically tracks version
changes to code.
to code over time. Each change (or set of
changes) is known as a commit, and you
GitHub makes it easy to review both
can track how your code evolves as various
the code you write and the code others
developers make commits.
write — as well as leave comments
Beyond being able to see how your code
and suggestions.
has changed, you can use GitHub version
control to revert to an earlier version of your
GitHub enables you to share code with
anyone who may want it.
code if you introduce a change that causes a
bug, for example. You can also make multiple
Developers can use GitHub to manage
versions of your software available at the
documentation files.
same time, which is useful if, for instance,
some of your users want to run the “beta”
version of your application while others
own, developers can use it to manage prefer a fully tested, stable version.
documentation files in addition to code.
developers when building software.
2.
3.
4.
5.
What Are Some of the Features of GitHub?
Collaboration and social networking
for developers
Although it would be a stretch to call GitHub
We touched on GitHub’s core code hosting,
sharing, and collaboration features above, a social media platform in the traditional
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
sense of the term, it offers collaboration
features that help developers network with
each other.
Developers can, for instance, search
through GitHub to find projects — and
the programmers associated with them
— that are of interest to them. Developers
can also configure GitHub profile pages
to share information about themselves.
And, although you can’t send messages
directly through GitHub, it’s a common
practice for coders to share their contact
information on their GitHub pages so that
others can contact them if they wish.
In these ways, GitHub can help
boost developer productivity and
satisfaction by helping coders work together
based on shared interests.
Web-based version control
GitHub provides a convenient way of
managing code through a web interface.
As noted above, most of GitHub’s core
functionality derives from Git. Functionally
speaking, most of the things you can do in
GitHub could also be done using Git on a
command line.
However, it can be challenging to use
Git on the command line when you have
dozens of repositories to manage and
multiple developers working across them.
By allowing developers to view the contents
of repositories, open up individual files,
track changes, and much more directly
through a web browser, GitHub provides
convenience. It also makes version control
more scalable.
That said, GitHub isn’t strictly just for
open source projects. You can also set up
private repositories if you want to manage
proprietary code in GitHub.
Note, too, that although GitHub is widely
used for hosting open source projects,
most of the code behind GitHub itself
is not actually open source. For that
reason, developers who are ideologically
committed to open source software —
who, in other words, believe that software
should be open source as a matter of
Distributing code
principle — may prefer to use a GitHub
GitHub is a great solution not only for
alternative that is fully open source,
making code available to end users, but
like GitLab.
also for developers who want to share
their code with other developers in order How to Get Started with GitHub
to invite their feedback or ask them
The process for getting started with GitHub
to collaborate.
is simple. Here are the basic steps:
GitHub is particularly useful for
Create an account. Join GitHub by
managing open source projects, since configuring a username and password.
open source coders are typically eager
Choose a plan. You can set up an
for the community at large to build and account with basic features and quotas
use their code — and for outsiders to for free, although GitHub paid plans are
contribute to their projects, if they have available if you want enhancements like
the inclination.
added storage.
7
How to Get the Most
Out of GitHub


Create a repository for each project.

Use descriptive commit messages when
making a change to code in GitHub.


Don’t use GitHub as a backup service.
Test your code before submitting
pull requests.
Don’t use GitHub to host application
binaries; use GitHub for your source code.
Create a repository. With an account
set up, you can set up your first repository
by clicking the + button in the upper-right
corner of the GitHub interface and selecting
New project.
Fork a repository. A repository fork is
a copy of an existing repository. If you
want to work on someone else’s code (or
code that you host in one of your own
repositories) without changing the original
code, you can create a fork. To fork a repo,
click the Fork button on the repository you
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
want to copy.
Make a pull request. If you want the
“GitHub is not the only platform of its king,
authors of a repository to integrate
but it is one of the most popular solutions
changes you’ve made to their code
inside a forked repository, you can
today for hosting source code.”
submit a pull request by clicking the Pull
request button.
Watch a repository. To get updates users resolve challenges. If that fails, best practices. But if you’re new to GitHub,
whenever a repository changes, you can ask a question in the GitHub some basic best practices to keep in
you can “watch” it by setting up a community forum.
mind include:
repository subscription.
Create a repository for each
To dive deeper into GitHub’s features
Resources for Learning More About GitHub and become an advanced user, you can project: Although it may be simplest to
Unsurprisingly given that GitHub is a take courses on GitHub Learning Lab. create one repository for all of your code,
widely used platform, there are a variety of There are also, of course, a large number projects are much more manageable if each
resources you can leverage to learn more of free guides and resources available one has its own repository.
Test before making pull requests: Before
from YouTube and other third-party
about it.
One of the most important is the GitHub sites, although they vary in quality. For you submit a pull request, carefully vet
blog, where you’ll find announcements the most reliable content, stick with well- your code to increase the chances that the
about the latest GitHub feature releases known, official sources — like GitHub’s own developers you’re submitting to will accept
your request.
and changes, as well as guidance and YouTube channel.
Use descriptive commit messages:
perspective on getting the most out
Best Practices for Getting the Most Out
Whenever you make a change to code in GitHub,
of GitHub.
include a comment explaining why you did it.
If you run into trouble using GitHub, of GitHub
You should check out the resources Other developers — not to mention your future
start troubleshooting on the GitHub
help site, which is designed to help described above for full details on GitHub self — may want to understand your thinking.
8
Don’t use GitHub as a backup
service: Although you technically could
store data that you want to back up using
GitHub, that’s not what GitHub is designed
for. It’s also not especially cost-effective
for large-scale data backup storage. Use
a dedicated backup storage platform if
you need to back up information.
Don’t use GitHub to host binaries:
Likewise, GitHub is not intended to be
a place to host application binaries.
Store those in a binary repository or
container registry, and use GitHub for your
source code.
Why Start with GitHub
GitHub is not the only platform of its
kind, but it is one of the most popular
solutions today for hosting source code.
It’s easy to use, it supports both public
and private repositories, and it’s free of
cost for most projects that operate on a
small scale. If you’re looking for a way to
host your code and collaborate with other
developers, GitHub is an excellent place
to start.
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
GitHub vs. GitLab: Which One Is Better for Your Enterprise?
GitHub and GitLab are both excellent
platforms for hosting source code, so
choosing between the two can be difficult.
Here are factors to weigh to help you pick
the one that best suits your organization.
G
itHub is probably the best-known
website for hosting software
projects, but it’s certainly not the only
one. GitLab has become a popular alternative
solution for managing Git repositories.
And although GitLab and GitHub offer the
same basic functionality at a high level,
there are critical differences between the
two platforms.
Keep reading for a comparison of GitLab
and GitHub and tips on deciding which
platform is the best for your needs —
especially if you’re an enterprise that needs
to manage software source code on a
large scale.
From the standpoint of community and
support, too, GitLab and GitHub are similar.
They are each popular platforms with strong
communities. Users who need help can
find community-based support from online
forums. Professional support options are
also available for both platforms, although
they require paid service plans in some cases.
Likewise, when it comes to documentation
and related resources, there are no major
differences between GitLab and GitHub.
Users can find plenty of information about
how each platform works, as well as tutorials
and guides that offer deep dives into specific
features or best practices.
What Are the Most Important Things to
Know About GitLab and GitHub?
source code within them as it changes.
GitLab and GitHub are similar in the respect
that their main service offering is hosting
software repositories based on Git, the
open-source version control tool. GitLab
and GitHub both provide an easy solution
for setting up Git repositories and managing
with regard to their ease of use. They both What Are Some of the Key Features of
9
The two platforms are also comparable
provide simple, user-friendly web interfaces
GitLab That Make It Stand Out?
Despite these high-level similarities, there
for creating and managing repositories. are important differences between GitLab
There is no major difference surrounding and GitHub.
the skills required to use either platform.
The main characteristics that make GitLab
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
stand out include:
More development tools: In some
respects, GitLab’s product suite is more
expansive because GitLab aims to provide
a complete continuous integration/
continuous delivery (CI/CD) solution, in
addition to Git repository hosting. GitHub
also offers something approximating CI/
CD functionality through the GitHub Actions
feature, but its functionality is more limited,
and CI/CD hasn’t historically been a major
product focus for GitHub.
Project management tools: GitLab
also offers built-in tools for agile project
management. The GitHub Issues feature,
which provides some project management
functionality, is somewhat similar, but it’s rare
to see businesses using Issues to handle all
project management needs.
Branch handling: Unlike GitHub, GitLab
encourages users to create multiple branches
for each project they manage. The idea is to
generate separate branches for stable code
and testing code. This makes it easier to
separate untested, potentially buggy code
“Depending on your needs and priorities,
you may see these standout features of GitLab
as either an advantage or disadvantage.”
from production code, but it also complicates
code management in some respects. Testing
may be harder, too, because you’ll need to test
each branch separately. Under GitHub, you
typically have just one branch to work with.
Open source: GitLab itself is an opensource platform. That means the code that
powers GitLab is available to anyone who
wants it. GitHub, in contrast, is powered
mostly by closed-source code (although you
can host open-source projects on GitHub,
as you can on GitLab). If you believe that
open source is more trustworthy or secure
than closed-source software, you’ll probably
prefer GitLab.
Corporate ownership: GitHub and GitLab
were both born as independent projects,
but GitHub is now owned by Microsoft,
10
which acquired it in 2018. The acquisition
hasn’t resulted in any major platform
changes to GitHub, but it still means that
GitHub is now part of the Microsoft suite
of products. In contrast, GitLab remains a
totally independent platform, with no signs
of an acquisition on the horizon.
Depending on your needs and priorities, you
may see these standout features of GitLab
as either an advantage or disadvantage.
For example, if you already have a CI/CD
suite in place, GitLab’s integrated CI/CD
products may not matter to you. But if you’re
looking for a one-stop shop for source code
management and CI/CD in a single platform,
GitLab offers it.
Why Is GitLab Often Considered a Better
Option for Enterprises?
By some measures, GitLab is a better source
code hosting and management solution for
enterprises in particular than GitHub.
One reason is that GitLab, as noted
above, offers a full-featured CI/CD suite
in addition to code hosting. That’s useful
for enterprises that want to centralize
development operations on a single platform
to avoid the silos and administrative burdens
that can arise from juggling multiple CI/
CD tools within different departments or
development teams.
The fact that GitLab offers an extensive
set of integrated security tools is also
an advantage for enterprises that seek
to prioritize software security. GitLab
supports source code analysis, vulnerability
scanning, Dynamic Application Security
Testing (DAST), and more. GitHub also offers
some built-in security services, but they are
not as extensive, so enterprises would need
to rely on third-party tools to secure all stages
of the software delivery pipeline on GitHub.
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
GitLab’s built-in project management
features, too, may make GitLab a more
attractive solution for enterprises because
they don’t need to deploy other project
management tools, like Jira. Again,
GitHub also provides project management
features via GitHub Issues, but they are
limited mostly to tracking workflows inside
GitHub rather than providing end-to-end
project management.
The bottom line: Because GitLab offers
a more holistic set of services out-of-thebox, it is arguably a better fit for enterprises
that want to centralize all aspects of their
development operations on a single platform.
How Does GitLab Compare to GitHub in
Terms of Pricing?
The pricing for GitLab and GitHub is hard
to compare directly because the pricing
structures are somewhat different. GitLab’s
billing models are oriented primarily around
feature-based pricing, meaning that the more
features you want to access, the more you’ll
have to pay. In contrast, with GitHub, most
core platform features are available across all
plans, but pricing increases based on usage.
That said, GitLab and GitHub both charge
on a per-user basis, so what you pay will
reflect in part in how many developers you
have in your organization.
In addition, GitLab and GitHub both
offer free plans that provide access to the
11
platforms’ software hosting features. There
is also no limitation on how long you can use
the paid plans on either platform.
However, there are limits on how much
data you can store each month under the
free plans, and how many minutes you can
execute CI/CD pipelines. Currently, GitLab
offers more storage but less CI/CD time
under its paid plan.
Which Platform Offers More Support
for Developers?
Although GitHub is currently the most
popular platform for developers, it’s not really
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
accurate to say that GitHub offers any more or
less support for developers than GitLab does.
Both platforms provide a variety of
developer tools, in addition to the project
management and security services
mentioned above. GitLab’s tooling is
more extensive in some respects, which
may be an advantage for developers who
want to stick with just one platform for
all of their needs. But GitHub and GitLab
are equally capable of powering complex,
large-scale development operations, and
any functionality that developers find to be
missing in the platforms can be filled by
integrating them with third-party tools.
So, whether GitLab or GitHub is better for
developers boils down mainly to preference.
Developers who like one-stop shopping will
probably like GitLab a little better. But in
terms of meeting developers’ functionality
requirements, there are not huge differences
between the platforms.
What Are the Security Concerns with
Each Platform?
From a security perspective, too,
GitLab and GitHub are quite similar.
They both allow developers to protect
their code inside private repositories if
they don’t want it to be public, and they
both provide robust authentication and
authorization features.
In addition, as noted above, GitLab and
GitHub both offer integrated tools to help
developers find security issues in their
applications. GitLab’s offerings are a bit more
extensive in this respect, but GitHub’s are
broadly comparable.
comparing GitLab and GitHub include:
Company size: GitHub and GitLab can
both work for development teams of all
sizes — from single-person projects to
large enterprises. But because GitHub’s free
plans don’t offer as many features, GitLab
is arguably a little better suited for larger
organizations, whereas small teams that
don’t want to purchase a paid plan may get
more value out of GitHub.
Development toolchain: Teams that
already have a development toolchain in
place may not get as much value from
GitLab because they don’t need all of its
What Are Some Other Considerations to
Keep in Mind When Choosing Between
products and features. On the other hand,
GitLab and GitHub?
GitLab is an attractive solution for teams that
Beyond the factors discussed above, want a turnkey way of building a complete
other considerations to bear in mind when CI/CD pipeline.
“GitLab and GitHub can both work for development
teams of all sizes — from single-person projects to
large enterprises.”
12
Collaboration: While there is plenty of
information out there for both platforms,
it’s a little easier to find other developers
who are familiar with GitHub, due simply
to the fact that it is more popular. So, if
you want to collaborate heavily, GitHub
may be a better choice, at least as long
as it remains the most widely used source
code hosting platform.
GitLab or GitHub: A Tough Choice
GitLab and GitHub are both excellent,
widely used platforms for hosting source
code. They also both offer features designed
to provide CI/CD, project management, and
security functionality, although GitLab’s
product suite is broader and deeper in this
respect. Overall, GitLab may be a better
choice for large enterprises that want to
centralize development operations using a
single platform.
That said, deciding between GitHub
and GitLab requires weighing a variety of
factors that are unique to your organization.
It’s impossible to say that one platform is
objectively better than the other.
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
How to Use GitHub Personal
Access Tokens
When it comes to managing access to GitHub resources, Personal Access Tokens have
a number of benefits over passwords. This article explains why and how to generate a
Personal Access Token for your GitHub account.
A
lthough GitHub supports passwordbased authentication, a more secure
means of controlling access to
resources inside GitHub is to use
Personal Access Tokens. Personal Access
Tokens require more work to set up, but they
offer a variety of benefits compared with
conventional authentication.
This article explains how Personal Access
Tokens work in GitHub, why you may want
to use them, and how to generate a Personal
Access Token for your GitHub account.
What Is a GitHub Personal Access Token?
of authenticating with GitHub services without
using passwords. Each user can generate
a unique token, then define which types of
actions — known in GitHub as “scopes” — can
be performed based on that token.
You can also authenticate with GitHub using
a password. However, as explained below,
1. Click your portfolio button on any page
token-based authentication offers a number in GitHub, then press the Settings button.
of benefits, such as the abilities to configure
2. Navigate to Developer Settings>Personal
access rights in a more granular way and to access tokens.
grant access for a limited amount of time.
3. On the screen that loads, click
How to Generate a Personal Access Token
Creating Personal Access Tokens in GitHub
In GitHub, a Personal Access Token is a way is straightforward:
13
the Generate new token button, give your
4. Configure which scopes to assign to
the token. We’ll explain what scopes mean
in detail below, but basically, they are the
specific types of permissions that the token
should allow a user to perform.
5. When the configuration process is
token a name, and configure the expiration complete, click the Generate token button
period for the token.
to activate your new token.
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
Using a Personal Access Token
What Are the Benefits of Using a GitHub
Once you’ve created a token, you can enter Personal Access Token?
it in the password field when prompted for
a password within either the GitHub web
interface or on the CLI. Note that GitHub may
explicitly ask for a “password,” but you can
still enter a token in most cases.
However, you’ll only be able to log in
successfully with your token if you configured
a scope that grants you permission to access
whichever resource you are trying to access,
or whichever action you are trying to perform.
So, you may find that your token serves as
an alternative to a GitHub password in some
cases, but not in others, depending on what
you are trying to accomplish.
Note, too, that because your web browser
might cache login information for GitHub, you
may need to clear your browser’s cache to
use a token. Otherwise, GitHub will continue
using the password-based login that is stored
in the cache instead of giving you a prompt
where you can enter a token.
Compared with passwords, Personal
Access Tokens offer several key benefits:
Granular security settings: Because you
can select specific scopes for each token,
tokens can be used to define granular
permissions. In other words, you can create
tokens that grant access to some GitHub
actions or services but not others. You can’t
do this with passwords.
Predefined expiration: You can configure
tokens to expire at a predefined time. This
is convenient if you want to grant a GitHub
user temporary access to a resource. For
instance, you may want to allow the cloning
of a repository on a temporary basis, but not
allow permanent access.
Randomization: Because tokens are
totally random strings of significant length,
attackers effectively can’t “steal” them using
brute-force or dictionary attacks. Passwords
may be susceptible to such attacks if they
are not sufficiently complex.
Faster login: The login process using
14
tokens is faster because it requires less Understanding Scopes for Personal
computation. Although this is not likely Access Tokens
As noted above, Personal Access Tokens
to make a major difference for logins by
can be configured to grant different “scopes.”
humans, it may be advantageous if you are,
Scopes are the specific permissions that a
for instance, running scripts where you need
token allows a user to perform. Scopes can
to authenticate with GitHub repeatedly, and
be used to allow users to perform certain
saving just fractions of a second on each
actions or download or modify certain data
login will add up to a significant reduction from GitHub.
in overall runtime.
For example, if you want to create a
There’s certainly no requirement to use token that lets a user manage the teams,
tokens on GitHub, but from the perspective projects, and memberships inside a
of security, performance, and control, tokens GitHub organization, you can grant
are more advantageous than passwords.
the admin:org scope. But if you want to
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
restrict the user to viewing information
about teams, projects, and memberships
without being able to modify it, you could
grant the read:org scope, which provides
read-only access.
If you don’t define any scopes, tokens
grant read-only access to public resources
available on GitHub. So, tokens without
associated scopes don’t allow users to do
anything they couldn’t already do on GitHub
without authenticating at all, since publicly
available resources are viewable by default
for everyone.
The full set of available scopes is described
in GitHub’s documentation. You’ll also see
a summary of available scopes when you
configure a Personal Access Token in the
GitHub interface.
What Is the Expiration Time for a Personal
Access Token?
The expiration period for a Personal Access
Token — meaning the point at which the
token can no longer be used — depends on
which period you configure when you create
the token. You can select expiration periods
based on fixed periods of time, like one year
or one week. Or, you can choose a specific
expiration date from a calendar.
Note, however, that if you choose to revoke
a token before its expiration date, the token
will expire and no longer be usable, even if
the expiration period you set when creating
the token has not yet run out.
You can also change the expiration period
for an existing token on the Personal Access
Tokens settings page.
How Do You Delete a Personal
Access Token?
The process for deleting a Personal Access
Token is a bit more complicated than creating
one, but it is possible to delete tokens.
To delete a Personal Access Token, navigate
to Settings>Applications>Authorized OAuth
Apps. From there, you can select applications
whose authorization permissions you want
to revoke. Tokens associated with the
applications will be revoked as well.
You can also simply change the token
expiration date so that it expires immediately.
This isn’t quite the same as deleting the
15
token, but it effectively results in the same like passwords, you can use them to give
other people access to your GitHub account.
outcome: The token becomes unusable.
Indeed, tokens can be a useful means of
Are There Any Security Risks Associated
providing someone with limited access to
with Using a Personal Access Token?
certain resources or with certain permissions.
Although Personal Access Tokens are more
For example, you may want to allow someone
secure in certain respects than passwords, to view code in a private repository but not
it’s important to keep in mind that tokens modify it. You could do this using tokens. But
can still be abused.
you would not want to attempt this by sharing
Anyone who gains access to your token, and your password because someone with your
who also knows the usernames associated password would have unrestricted access to
with the token, will be able to use the token everything inside your GitHub account.
to authenticate with GitHub just like you
could. So, you should protect your tokens in Security Edge Goes to Personal
the same way that you protect passwords. Access Tokens
Because Personal Access Tokens can be
That said, the fact that tokens can be
revoked quickly means that you can easily used to grant limited permissions, they are
make a token unusable if it falls into the a more secure way of managing access to
wrong hands while still being able to use GitHub resources than passwords. They’re
other tokens or your password to connect also relatively easy to set up — although not
as simple as configuring a password, which
to GitHub. This is easier than having to reset
is something that happens when you sign
your password in the event that it is stolen.
up for GitHub. And the fact that you can
easily revoke tokens when they are no longer
Can I Use My Personal Access Token to
Give Someone Else Access to My Account? necessary adds a layer of security that you
Since Personal Access Tokens function won’t obtain from passwords.
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
How to Delete a Local or Remote Branch on Git and GitHub
There are times when it is advantageous
to delete GitHub branches. Here’s
what you need to know about deleting
— and undeleting — both local and
remote branches.
in it because, for example, the changes are
buggy or they address application features
that the developers decided not to add.
Local vs. Remote Branches
T
he process for deleting GitHub
branches is pretty straightforward.
However, it varies depending on which
interface you’re working with and whether
you’re dealing with a local or a remote branch.
Keep reading for a breakdown of how branch
deletion works in GitHub and how to delete
remote branches under various circumstances.
We’ll also explain how to delete multiple branches
That said, most developers don’t want to
at once, how you can undelete branches, and help developers manage source code. A
how to delete an entire repository if you find branch is a set of changes that a developer retain every GitHub branch that they create
yourself wanting to do so.
has made to code. By treating each set of for an indefinite period of time. Instead,
What Is a GitHub Branch, and Why Would
You Delete It?
changes as a distinct branch, it’s easy to they routinely delete branches once they’ve
decide which changes to keep and which to merged them into other branches. Developers
Like other Git repository hosting platforms discard. In addition, branches can be “merged” might also choose to delete a branch if they’ve
(such as GitLab), GitHub uses branches to in order to integrate multiple sets of changes. decided to abandon the changes contained
16
There are two types of branches you
can work with on GitHub: local and
remote branches.
Local branches are branches of code
stored on a developer’s local machine. These
branches aren’t actually hosted on GitHub,
but you might still want to delete them as
part of a GitHub-centered workflow. For
example, you may be working with code in
a local branch that you push to GitHub, and
decide to delete the local branch after it has
been merged into a remote branch.
Remote branches are branches of code
hosted on a remote machine — such as
GitHub servers. All branches that exist on
GitHub itself are remote branches from the
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
perspective of a local machine, although
again, you may have to work with local
branches in the context of using GitHub.
Indeed, in many cases branches that end up
on GitHub originate as local branches that a
developer builds on his or her own computer,
and then later pushes to GitHub.
What Happens When You Delete a Remote
Branch on GitHub?
When you delete a remote branch (or a
local branch, for that matter), the branch
disappears from your GitHub workflow. You’ll
no longer be able to track or view the branch,
and the branch will be removed from your
remote repository.
Usually, this is what you want to happen. If
you decide to delete a branch, it’s probably
because you are done using it and you no
longer want it to be a distraction when you
are working within a repository.
That said, there are situations where you
may end up deciding after the fact that
you didn’t actually want to delete a branch.
Fortunately, it’s possible to recover deleted
branches, as we explain later in this article.
do it on the CLI with a command such as:
git branch -d branchname
“All branches that exist on GitHub itself are remote branches from
This command deletes a local branch with
the perspective of a local machine, although again, you may have
the name branchname.
Because Git doesn’t provide a web
to work with local branches in the contest of using GitHub.”
interface, deleting local branches through
the command line is typically the only way
Steps for Deleting Remote Branches
to remove a local branch. The exception is if
steps in the GitHub documentation.
on GitHub
Then, you can use the git push [remotename] you run some type of local Git web interface
Deleting remote branches that are :[remotebranchname] command to delete a that supports branch deletion, but this is rare.
hosted on GitHub is easy, as long as you
Most developers manage local branches
branch. For example:
are managing them through your GitHub
through the CLI and push their code to
git push origin —delete :somebranch
account. Here are the steps:
GitHub (or a similar platform) if they want
This syntax may seem confusing because
Start by navigating to the main page of the
web-based management features.
it uses the git push command instead of git
repository that hosts the branch you want
branch. Unfortunately, however, because git Deleting All Local Branches
to delete.
branch doesn’t support deletion of remote
What if you want to delete all local branches
Then, click the Branches button. You’ll see
branches, this is how you have to manage within a repository at once? Instead of having
a list of branches in the repository.
to delete each branch manually one-by-one,
remote branch deletion with Git.
To delete a branch, click the trashcan icon
While this CLI approach works for removing you can use the git command in conjunction
next to its name.
remote branches, the best way to remove with grep to select multiple branches for
Branch Deletion Through the Command Line
a branch hosted on GitHub is to use the deletion via pattern matching. For example:
If you prefer to work from the CLI, you can web interface.
git branch -D `git branch —merged | grep
delete GitHub branches that way, too.
-v \* | xargs`
You must first set up Git on your computer Deleting Local Branches with Git
The grep -v /* command above selects
and integrate it with GitHub by following the
Deleting local branches is simpler. You can all branches.
17
PREVIOUS
NEXT
A Beginners Guide to GitHub: What You Need to Know
Keep in mind, of course, that this approach
leaves you liable to accidental branch
deletion because you may inadvertently
select branches that you didn’t mean to.
And Git won’t warn you before deletion. So, if
you decide to delete all local branches using
a single command, proceed with caution.
How to Undelete Git Branches
In the event that you accidentally delete a
Git branch — or you deleted it deliberately
but decide later that you want it back — all
is not lost. You can undelete branches both
locally and in GitHub.
To undelete a local branch, first run
the command:
git reflog —no-abbrev
This will generate a SHA1 value that
identifies the first commit associated with
the deleted branch. Using that information,
you can run:
git checkout [shastring]
To get back to that commit, from there, run:
git checkout -b branchname
This will re-create a new branch that is
identical to your deleted branch.
On the GitHub web interface, you can
restore deleted branches as long as they
were part of completed pull requests. Do this
by first clicking the Pull requests button for
the repository that hosted the branch. Then,
click Closed to view a list of completed pull
requests. You’ll see a Restore branch button
that allows you to recover branches
associated with closed pull requests.
How to Delete a Git Repository
as long as it has been 90 days or fewer since Conclusion: Delete with Caution
If you want to delete an entire local Git you deleted the repository.
Both the Git CLI and the GitHub web
repository instead of just a branch, you can
interface make it easy to work with branches.
delete the .git directory on your computer Changing a Branch Name
It’s possible to change a branch name on You can delete, rename, and (in most cases)
with a command like:
Git using the CLI. First, you have to check even undelete branches.
rm -r /path/to/.git
Bear in mind that there is no way to undelete out the branch you want to rename with a
However, the process for accomplishing
a repository if you take this approach (apart command such as:
these tasks varies a bit depending on whether
git checkout branchname
from trying to recover deleted data on your
Then, rename it using the git branch you are working with a local or a remote
file system, which is a process that yields
spotty results at best).
You can alternatively delete repositories
through GitHub by clicking the
repository’s Settings button, then clicking Delete
this repository. GitHub does provide a process
for restoring deleted repositories in most cases
18
-m command:
git branch -m newbranchname
On GitHub, you can rename a branch by
clicking the Branches button in a repository,
then clicking the edit button (which looks
like a pen).
branch. And keep in mind that although many
changes to branches can be reversed, that’s
not always the case, so you’ll want to be
careful before removing a branch (or, even
more so, an entire repository).
PREVIOUS
Download