Skip to content

Commit d578fa4

Browse files
Merge pull request #349 from UBC-DSCI/patch-vc-pa-token
added section on personal access tokens.
2 parents efc4fbe + 7057e1a commit d578fa4

File tree

4 files changed

+92
-26
lines changed

4 files changed

+92
-26
lines changed

img/generate-pat_01.png

120 KB
Loading

img/generate-pat_02.png

402 KB
Loading

img/generate-pat_03.png

202 KB
Loading

version-control.Rmd

Lines changed: 92 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ version control: you won't be able to see, interpret, or find changes in the his
468468
of any of the actual content of your project!
469469

470470
## Working with local repositories using Jupyter {#local-repo-jupyter}
471+
471472
Although there are several ways to create and edit files on GitHub, they are
472473
not quite powerful enough for efficiently creating and editing complex files,
473474
or files that need to be executed to assess whether they work (e.g., files
@@ -477,14 +478,76 @@ remote repository that was created on GitHub to a local coding environment. Thi
477478
can be done by creating and working in a local copy of the repository.
478479
In this chapter, we focus on interacting with Git via Jupyter using
479480
the Jupyter Git extension. The Jupyter Git \index{git!Jupyter extension} extension
480-
can be run via Jupyter on your local computer, or on a JupyterHub server.
481+
can be run by Jupyter on your local computer, or on a JupyterHub server.
481482
*Note: we recommend reading Chapter \@ref(getting-started-with-jupyter)*
482483
*to learn how to use Jupyter before reading this chapter.*
483484

485+
### Generating a GitHub personal access token
486+
487+
To send and retrieve work between your local repository
488+
and the remote repository on GitHub,
489+
you will frequently need to authenticate with GitHub
490+
to prove you have the required permission.
491+
There are several methods to do this,
492+
but for beginners we recommend using the HTTPS method
493+
because it is easier and requires less setup.
494+
In order to use the HTTPS method,
495+
GitHub requires you to provide a *personal access token*. \index{GitHub!personal access token}
496+
A personal access token is like a password—so keep it a secret!—but it gives
497+
you more fine-grained control over what parts of your account
498+
the token can be used to access, and lets you set an expiry date for the authentication.
499+
To generate a personal access token,
500+
you must first visit [https://github.com/settings/tokens](https://github.com/settings/tokens),
501+
which will take you to the "Personal access tokens" page in your account settings.
502+
Once there, click "Generate new token" (Figure \@ref(fig:generate-pat-01)).
503+
Note that you may be asked to re-authenticate with your username
504+
and password to proceed.
505+
506+
(ref:generate-pat-01) The "Generate new token" button used to initiate the creation of a new personal access token. It is found in the "Personal access tokens" section of the "Developer settings" page in your account settings.
507+
508+
```{r generate-pat-01, fig.cap = '(ref:generate-pat-01)', fig.retina = 2, out.width="100%"}
509+
image_read("img/generate-pat_01.png")
510+
```
511+
512+
Next you will be asked to add a note for your personal access token.
513+
This should be a descriptor of the token's purpose.
514+
Finally, you need to select permissions for the token; this is where
515+
you can control what parts of your account the token can be used to access.
516+
Make sure to choose only those permissions that you absolutely require. In
517+
Figure \@ref(fig:generate-pat-02), we tick only the "repo" box, which gives the
518+
token access to our repositories (so that we can push and pull) but none of our other GitHub
519+
account features. Finally, to generate the token, scroll to the bottom of that page
520+
and click the green "Generate token" button (Figure \@ref(fig:generate-pat-02)).
521+
522+
(ref:generate-pat-02) Webpage for creating a new personal access token.
523+
524+
```{r generate-pat-02, fig.cap = '(ref:generate-pat-02)', fig.retina = 2, out.width="100%"}
525+
image_read("img/generate-pat_02.png")
526+
```
527+
528+
Finally, you will be taken to a page where you will be able to see
529+
and copy the personal access token you just generated (Figure \@ref(fig:generate-pat-03)).
530+
Since it provides access to certain parts of your account, you should
531+
treat this token like a password; for example, you should consider
532+
securely storing it (and your other passwords and tokens, too!) using a password manager.
533+
Note that this page will only display the token to you once,
534+
so make sure you store it in a safe place right away. If you accidentally forget to
535+
store it, though, do not fret—you can delete that token by clicking the
536+
"Delete" button next to your token, and generate a new one from scratch.
537+
To learn more about GitHub authentication,
538+
see the additional resources section at the end of this chapter.
539+
540+
(ref:generate-pat-03) Display of the newly generated personal access token.
541+
542+
```{r generate-pat-03, fig.cap = '(ref:generate-pat-03)', fig.retina = 2, out.width="100%"}
543+
image_read("img/generate-pat_03.png")
544+
```
545+
484546
### Cloning a repository using Jupyter
485547

486-
The first step is to *clone* the \index{git!clone} remote repository on GitHub to create a local repository,
487-
i.e., make a
548+
Now that we have everything we need for authentication,
549+
the next step is to *clone* the \index{git!clone} remote repository on GitHub
550+
to create a local repository, i.e., make a
488551
copy that knows where it was obtained from so that it knows where send/receive
489552
new committed edits. In order to do this, first copy the URL from the HTTPS tab
490553
of the Code drop down menu on GitHub (Figure \@ref(fig:clone-02)).
@@ -521,9 +584,9 @@ image_read("img/version_control/clone_04.png") %>%
521584
```
522585

523586
### Specifying files to commit
524-
Now that we have cloned the remote repository from GitHub to create a local repository,
525-
you can get to work editing, creating, and deleting files.
526-
For example, suppose we created and saved a new file (named `eda.ipynb`) that we would
587+
Now that you have cloned the remote repository from GitHub to create a local repository,
588+
you can get to work editing, creating, and deleting files.
589+
For example, suppose you created and saved a new file (named `eda.ipynb`) that you would
527590
like to send back to the project repository on GitHub (Figure \@ref(fig:git-add-01)).
528591
To "add" this modified file to the staging area (i.e., flag that this is a
529592
file whose changes we would like to commit), click the Jupyter Git extension
@@ -537,8 +600,8 @@ This opens the Jupyter Git graphical user interface pane. Next,
537600
click the plus sign (+) beside the file(s) that you want to "add" \index{git!add}
538601
(Figure \@ref(fig:git-add-02)). Note that because this is the
539602
first change for this file, it falls under the "Untracked" heading.
540-
However, next time we edit this file and want to add the changes,
541-
we will find it under the "Changed" heading.
603+
However, next time you edit this file and want to add the changes,
604+
you will find it under the "Changed" heading.
542605

543606
You will also see an `eda-checkpoint.ipynb` file under the "Untracked" heading.
544607
This is a temporary "checkpoint file" created by Jupyter when you work on `eda.ipynb`.
@@ -552,9 +615,9 @@ image_read("img/version_control/git_add_02.png")
552615
```
553616

554617
Clicking the plus sign (+) moves the file from the "Untracked" heading to the "Staged" heading,
555-
flagging this file so that Git knows we want a snapshot of its current state
618+
flagging this file so that Git knows you want a snapshot of its current state
556619
as a commit (Figure \@ref(fig:git-add-03)).
557-
Now we are ready to "commit" the changes.
620+
Now you are ready to "commit" the changes.
558621
Make sure to include a (clear and helpful!) message about what was changed
559622
so that your collaborators (and future you) know what happened in this commit.
560623

@@ -567,13 +630,13 @@ image_read("img/version_control/git_add_03.png")
567630
### Making the commit
568631

569632
To snapshot the changes with an associated commit message,
570-
we put a message in the text box at the bottom of the Git pane
633+
you must put a message in the text box at the bottom of the Git pane
571634
and click on the blue "Commit" button (Figure \@ref(fig:git-commit-01)). \index{git!commit}
572635
It is highly recommended to write useful and meaningful messages about what
573636
was changed. These commit messages, and the datetime stamp for a given
574637
commit, are the primary means to navigate through the project's history in the
575-
event that we need to view or retrieve a past version of a file, or
576-
revert our project to an earlier state.
638+
event that you need to view or retrieve a past version of a file, or
639+
revert your project to an earlier state.
577640
When you click the "Commit" button for the first time, you will be prompted to
578641
enter your name and email. This only needs to be done once for each machine
579642
you use Git on.
@@ -582,9 +645,9 @@ you use Git on.
582645
image_read("img/version_control/git_commit_01.png")
583646
```
584647

585-
After "committing" the file(s), you will see there are 0 "Staged" files
586-
and we are now ready to push our changes
587-
to our remote repository on GitHub (Figure \@ref(fig:git-commit-03)).
648+
After "committing" the file(s), you will see there are 0 "Staged" files.
649+
You are now ready to push your changes
650+
to the remote repository on GitHub (Figure \@ref(fig:git-commit-03)).
588651

589652
```{r git-commit-03, fig.cap = 'After recording a commit, the staging area should be empty.', fig.retina = 2, out.width="100%"}
590653
image_read("img/version_control/git_commit_03.png")
@@ -593,7 +656,8 @@ image_read("img/version_control/git_commit_03.png")
593656
### Pushing the commits to GitHub
594657

595658
To send the committed changes back to the remote repository on
596-
GitHub, we need to *push* them. \index{git!push} To do this we click on the cloud icon with the up arrow on the Jupyter Git tab
659+
GitHub, you need to *push* them. \index{git!push} To do this,
660+
click on the cloud icon with the up arrow on the Jupyter Git tab
597661
(Figure \@ref(fig:git-push-01)).
598662

599663
(ref:git-push-01) The Jupyter Git extension "push" button (circled in red).
@@ -602,23 +666,25 @@ GitHub, we need to *push* them. \index{git!push} To do this we click on the clou
602666
image_read("img/version_control/git_push_01.png")
603667
```
604668

605-
We will then be prompted to enter our GitHub username
606-
and password, and click the blue "OK" button (Figure \@ref(fig:git-push-02)).
669+
You will then be prompted to enter your GitHub username
670+
and password. Instead of your password, though, you will
671+
need to enter the personal access token that you generated
672+
earlier. Click the blue "OK" button to initiate the push (Figure \@ref(fig:git-push-02)).
607673

608674
```{r git-push-02, fig.cap = 'Enter your Git credentials to authorize the push to the remote repository.', fig.retina = 2, out.width="100%"}
609675
image_read("img/version_control/git_push_02.png")
610676
```
611677

612-
If the files were successfully pushed to our project repository on
613-
GitHub we will be given the success message shown below.
614-
Click "Dismiss" to continue working in Jupyter (Figure \@ref(fig:git-push-03)).
678+
If the files were successfully pushed to the project repository on
679+
GitHub, you will be shown a success message (Figure \@ref(fig:git-push-03)).
680+
Click "Dismiss" to continue working in Jupyter.
615681

616682
```{r git-push-03, fig.cap = 'The prompt that the push was successful.', fig.retina = 2, out.width="100%"}
617683
image_read("img/version_control/git_push_03.png")
618684
```
619685

620686
If you visit the remote repository on GitHub,
621-
you will see that the changes now exist there via their web interface
687+
you will see that the changes now exist there too
622688
(Figure \@ref(fig:git-push-04))!
623689

624690
```{r git-push-04, fig.cap = 'The GitHub web interface shows a preview of the commit message, and the time of the most recently pushed commit for each file.', fig.retina = 2, out.width="100%"}
@@ -756,7 +822,7 @@ image_read("img/version_control/merge_conflict_03.png") %>%
756822

757823
### Handling merge conflicts
758824

759-
To fix the merge conflict, \index{git!merge conflict} we need to open the offending file
825+
To fix the merge conflict, \index{git!merge conflict} you need to open the offending file
760826
in a plain text editor and look for special marks that Git puts in the file to
761827
tell you where the merge conflict occurred (Figure \@ref(fig:merge-conflict-04)).
762828

@@ -865,7 +931,7 @@ Now that you've picked up the basics of version control with Git and GitHub,
865931
you can expand your knowledge through the resources listed below:
866932

867933
- GitHub's [guides website](https://guides.github.com/) and [YouTube channel](https://www.youtube.com/githubguides),
868-
and [Happy Git](https://happygitwithr.com/) are great resources to take the next steps in learning about Git and GitHub.
934+
and [*Happy Git with R*](https://happygitwithr.com/) are great resources to take the next steps in learning about Git and GitHub.
869935
- [Good enough practices in scientific computing](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510#sec014) [-@wilson2014best] provides more advice on useful workflows and "good enough" practices in data analysis projects.
870936
- In addition to [GitHub](https://github.com), there are other popular Git repository hosting services such as [GitLab](https://gitlab.com) and [BitBucket](https://bitbucket.org). Comparing all of these options is beyond the scope of this book, and until you become a more advanced user you are perfectly fine to just stick with GitHub. Just be aware that you have options!
871-
937+
- [GitHub's documentation on creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) and the *Happy Git with R* [cache credentials for HTTPS](https://happygitwithr.com/credential-caching.html) chapter are both excellent additional resources to consult if you need additional help generating and using personal access tokens.

0 commit comments

Comments
 (0)