# Push to Git

Commit notebook changes directly to your Git repository from Jupyter. This requires setting up SSH credentials in Valohai.

### Setup Overview

You'll need to:

1. Generate SSH keys
2. Add the public key as a deploy key in your Git repository
3. Add the private key to Valohai
4. Test the connection from a notebook

This setup is one-time per repository. Once configured, any notebook in your project can push changes.

### Generate SSH Keys

Create an SSH key pair on your local machine:

```shell
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```

When prompted for a file location and passphrase, accept the defaults or customize as needed.

This creates two files:

* `id_rsa` (private key) — stays secret, goes into Valohai
* `id_rsa.pub` (public key) — goes into GitHub/GitLab as a deploy key

### Add Deploy Key to GitHub

In your GitHub repository:

1. Go to **Settings → Deploy keys**
2. Click **Add deploy key**
3. Paste your public key (`id_rsa.pub` contents)
4. Give it a descriptive title like "Valohai Notebooks"
5. Check **Allow write access**
6. Click **Add key**

For GitLab or other providers, the process is similar—look for "Deploy Keys" or "Access Keys" in repository settings.

### Add Private Key to Valohai

In Valohai:

1. Click your name in the top-right corner
2. Select **Manage Organization**
3. Navigate to **App Credentials**
4. Click **Add credentials**
5. Enter the Git hostname (e.g., `github.com`)
6. Paste your private key (`id_rsa` contents)
7. Click **Save**

Your notebooks now have write access to the repository through this credential.

### Test from a Notebook

Launch a notebook execution in your Valohai project. Once it's running:

1. Open Jupyter
2. Create or modify a notebook file
3. Save your changes
4. Open a terminal in Jupyter (**New → Terminal**)
5. Run these commands:

```shell
git status
git add <your_file>
git commit -m "Update notebook from Valohai"
git push origin main
```

If the push succeeds, check your GitHub repository to verify the changes appeared.

> 💡 **Tip:** Use a separate branch for notebook experiments to keep your main branch clean. Push to a feature branch, then create a pull request for review.

### Best Practices

**Use dedicated branches**

Don't push directly to main from notebooks. Create feature branches:

```shell
git checkout -b experiment/new-feature
git add my_notebook.ipynb
git commit -m "Add new feature exploration"
git push origin experiment/new-feature
```

**Commit meaningful changes**

Notebooks auto-save cell outputs, which creates noisy diffs. Consider:

* Clearing outputs before committing: `Cell → All Output → Clear`
* Committing only when you've made substantial progress
* Using descriptive commit messages

**Test in a separate project first**

If you're setting up Git pushes for the first time, test the workflow in a non-production project to avoid accidentally pushing broken code.

### Troubleshooting

**Permission denied errors**

If you see "Permission denied (publickey)" when pushing:

* Verify the private key in Valohai matches the public key in GitHub
* Check that "Allow write access" is enabled on the deploy key
* Confirm the hostname in Valohai credentials matches your Git provider

**Push rejected errors**

If your push is rejected:

* Pull the latest changes first: `git pull origin main`
* Resolve any merge conflicts
* Then retry the push

**No remote tracking branch**

If Git complains about no upstream branch:

```shell
git push --set-upstream origin <branch-name>
```

### Next Steps

* [Save Your Work](https://docs.valohai.com/notebook-executions/save-and-version) — Version outputs and log metrics
* [Convert to Production Script](https://docs.valohai.com/notebook-executions/notebook-to-execution) — Move from experimentation to execution
* [Connect Repo](https://docs.valohai.com/git-integration) — Set up Git integration for your project
