Using a GitHub Personal Access Token (PAT) to Push/Pull from a Vertex AI Notebook
Last updated on 2025-08-27 | Edit this page
Estimated time: 35 minutes
Overview
Questions
- How can I securely push/pull code to and from GitHub within a Vertex
AI Workbench notebook?
- What steps are necessary to set up a GitHub PAT for authentication
in GCP?
- How can I convert notebooks to
.py
files and ignore.ipynb
files in version control?
Objectives
- Configure Git in a Vertex AI Workbench notebook to use a GitHub
Personal Access Token (PAT) for HTTPS-based authentication.
- Securely handle credentials in a notebook environment using
getpass
. - Convert
.ipynb
files to.py
files for better version control practices in collaborative projects.
Step 0: Initial setup
In the previous episode, we cloned our forked repository as part of the workshop setup. In this episode, we’ll see how to push our code to this fork. Complete these three setup steps before moving forward.
Clone the fork if you haven’t already. See previous episode.
Start a new Jupyter notebook, and name it something like
Interacting-with-git.ipynb
. We can use the default Python 3 kernel in Vertex AI Workbench.Change directory to the workspace where your repository is located. In Vertex AI Workbench, notebooks usually live under
/home/jupyter/
.
Step 1: Using a GitHub personal access token (PAT) to push/pull from a Vertex AI notebook
When working in Vertex AI Workbench notebooks, you may often need to push code updates to GitHub repositories. Since Workbench VMs may be stopped and restarted, configurations like SSH keys may not persist. HTTPS-based authentication with a GitHub Personal Access Token (PAT) is a practical solution. PATs provide flexibility for authentication and enable seamless interaction with both public and private repositories directly from your notebook.
Important Note: Personal access tokens are powerful credentials. Select the minimum necessary permissions and handle the token carefully.
Generate a personal access token (PAT) on GitHub
- Go to Settings in GitHub.
- Click Developer settings at the bottom of the left
sidebar.
- Select Personal access tokens, then click
Tokens (classic).
- Click Generate new token (classic).
- Give your token a descriptive name and set an expiration date if
desired.
-
Select minimum permissions:
- Public repos:
public_repo
- Private repos:
repo
- Public repos:
- Click Generate token and copy it immediately—you won’t be able to see it again.
Caution: Treat your PAT like a password. Don’t share it or expose it in your code. Use a password manager to store it.
Step 2: Configure Git settings
PYTHON
!git config --global user.name "Your Name"
!git config --global user.email your_email@wisc.edu
-
user.name
: Will appear in the commit history. -
user.email
: Must match your GitHub account so commits are linked to your profile.
Step 3: Convert .ipynb
notebooks to
.py
Tracking .py
files instead of .ipynb
helps
with cleaner version control. Notebooks store outputs and metadata,
which makes diffs noisy. .py
files are lighter and easier
to review.
- Install Jupytext.
- Convert a notebook to
.py
.
- Convert all notebooks in the current directory.
Step 4: Add and commit .py
files
Step 5: Add .ipynb
to .gitignore
PYTHON
!touch .gitignore
with open(".gitignore", "a") as gitignore:
gitignore.write("\n# Ignore Jupyter notebooks\n*.ipynb\n")
!cat .gitignore
Add other temporary files too:
PYTHON
with open(".gitignore", "a") as gitignore:
gitignore.write("\n# Ignore cache and temp files\n__pycache__/\n*.tmp\n*.log\n")
Commit the .gitignore
:
Step 6: Syncing with GitHub
First, pull the latest changes:
If conflicts occur, resolve manually before committing.
Then push with your PAT credentials:
Step 7: Convert .py
back to notebooks (optional)
To convert .py
files back to .ipynb
after
pulling updates:
Challenge: GitHub PAT Workflow
- Why might you prefer using a PAT with HTTPS instead of SSH keys in
Vertex AI Workbench?
- What are the benefits of converting
.ipynb
files to.py
before committing to a shared repo?
- PATs with HTTPS are easier to set up in temporary environments where
SSH configs don’t persist.
- Converting notebooks to
.py
results in cleaner diffs, easier code review, and smaller repos without stored outputs/metadata.
- Use a GitHub PAT for HTTPS-based authentication in Vertex AI
Workbench notebooks.
- Securely enter sensitive information in notebooks using
getpass
. - Converting
.ipynb
files to.py
files helps with cleaner version control. - Adding
.ipynb
files to.gitignore
keeps your repository organized.