---
url: /developers/tools/git.md
---
# Git

This guide is for contributors working on Open Library pull requests. It covers the standard day-to-day workflow first, then troubleshooting and advanced git operations.

Prerequisites: complete [Quick Start](/developers/quick-start.md) and ensure your local Open Library repository is set up.

## Git Workflow (Start Here)

### Quick Workflow Checklist

1. Sync `master` with `upstream` using [Canonical Sync Commands](#canonical-sync-commands).
2. Create or switch to your working branch.
3. Make and test your changes.
4. Push your branch and open a pull request.
5. If your PR needs updates, repeat from step 1 and rebase.

## Detailed Workflow

### Fork the Open Library repository

Log in to GitHub. Go to <https://github.com/internetarchive/openlibrary> and click the Fork button in the upper right corner:
![GitHub Fork](https://archive.org/download/screenshot20191211at11.12.56/fork.jpg)

### Clone the forked repository on to your local computer

This creates a local copy of your own fork of the Open Library repository, in a directory called *openlibrary*. Your fork on the GitHub servers is a remote called *origin*. By default, you are looking at the *master* branch.

Make sure you `git clone` openlibrary using `ssh` instead of `https` as the `infogami` git submodule may not fetch correctly otherwise.

```sh
git clone git@github.com:USERNAME/openlibrary.git
```

### Permission denied while cloning

If you have not added your public SSH key to GitHub you may see:

```sh
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
```

To fix this, first [generate a new SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) if you have not already done so, and then [add the SSH key to your GitHub account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account).

### Modifying a repository that was incorrectly cloned using `https` (or is missing the infogami module)

You can modify an existing openlibrary repository that was inadvertently cloned with `https` by running:

```sh
git remote rm origin
git remote add origin git@github.com:USERNAME/openlibrary.git
git submodule init; git submodule sync; git submodule update
```

### Windows: clone inside WSL2 (recommended)

Open Library runs in Linux Docker containers. On Windows, clone and run it from [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install), not from a normal Windows folder. Git in WSL2 checks out LF line endings and real symlinks, which the containers need, with no extra setup. Docker Desktop also mounts files much faster from the WSL2 filesystem.

1. Install WSL2 with Ubuntu: `wsl --install` in PowerShell, then restart.

2. In Docker Desktop, turn on **Settings → Resources → WSL integration** for your Ubuntu distribution.

3. Open the **Ubuntu** terminal (not PowerShell or Git Bash) and clone into your Linux home directory:

   ```sh
   cd ~
   git clone --recurse-submodules git@github.com:USERNAME/openlibrary.git
   cd openlibrary
   ```

4. Open the project in VS Code with `code .` from that terminal. This uses the [WSL extension](https://code.visualstudio.com/docs/remote/wsl), so files you save keep LF endings.

Run every `git`, `make` and `docker compose` command in the Ubuntu terminal.

> \[!WARNING]
> Do not clone under `/mnt/c/...` from WSL2, and do not clone with Git for Windows, GitHub Desktop or VS Code's clone button into a Windows folder. Those checkouts break symlinks and are slow. If you already have one, follow the next section or clone again inside WSL2.

### Fix line endings, symlinks and git submodules (only for Windows users not using a Linux VM)

Use this section only if you cloned with Git for Windows instead of [inside WSL2](#windows-clone-inside-wsl2-recommended).

The repository has symlinks, such as `infogami` and `config`. Without `core.symlinks=true`, Git for Windows writes each one as a small text file that holds the target path. The app then can't load infogami: every page returns 404, or Infobase can't connect. To check, open `infogami` in the project root. If it is a text file that contains `vendor/infogami/infogami`, your symlinks are broken.

Text files must also have LF line endings, because they run in Linux containers. The repository's `.gitattributes` enforces LF on checkout. A clone made before that rule was added may still have CRLF, which shows up as errors like `required file not found` or `env: 'python\r': No such file or directory`.

First, turn on [Windows Developer Mode](https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development). It lets Git create symlinks without running as Administrator.

For a new clone, run this in Git Bash:

```sh
git clone -c core.symlinks=true --recurse-submodules git@github.com:USERNAME/openlibrary.git
```

To fix an existing clone:

```sh
# Get in the project root directory
cd openlibrary

# Keep LF line endings on checkout
git config core.autocrlf input

# Enable symlinks
git config core.symlinks true

# Build submodules
git submodule init; git submodule sync; git submodule update

# Stage indexed files for removal so git reset updates them
git rm --cached -r .  # Don't forget the "."

# Reset the repo (removes any uncommitted changes you've made to files)
git reset --hard
```

For more on git and line endings, see [Configuring Git to handle line endings](https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings).

### Add 'upstream' repo to list of remotes

```sh
cd openlibrary
git remote add upstream https://github.com/internetarchive/openlibrary.git
```

### Verify the new remote named 'upstream'

```sh
git remote -v
# origin  git@github.com:USERNAME/openlibrary.git (fetch)
# origin  git@github.com:USERNAME/openlibrary.git (push)
# upstream        https://github.com/internetarchive/openlibrary.git (fetch)
# upstream        https://github.com/internetarchive/openlibrary.git (push)
```

Note that `origin` is `git@`. If it is not, see [Fork the Open Library repository](#fork-the-open-library-repository).

### Working on Your Branch

#### Canonical Sync Commands

Because new commits are frequently merged into the Open Library repository, it's important to make sure that the branch you're working on is up to date with the upstream `master` version.

Before creating a new branch and **each time** you start work on an existing branch, you should run the following commands to make sure your master branch is up to date:

```sh
git switch master
git pull --ff-only upstream master
git push origin master
```

**Note:** When running `pull --ff-only`, you may encounter the error `fatal: Not possible to fast-forward`. If this occurs, refer to the [Out-of-Sync Branches](#out-of-sync-branches) section for instructions on resolving this issue.

You can then create a new branch for your issue:

```sh
git switch -c 1234/fix/fix-the-thing
```

Or, if you are returning to work on a previously created branch, rebase\* with master:

```sh
git switch existing-branch-name
git rebase master
```

\***Note:** Rebasing is the equivalent of "lifting" all the commits in your branch, and placing them on top of the latest master. It effectively changes the *base* of your branch/commits. If you encounter any errors in the rebase process, see the [troubleshooting guide](/developers/tools/git-troubleshooting.md#troubleshooting).

You can confirm everything is up to date by running `git status` and/or visiting your remote master branch at `github.com/your-username/openlibrary`, where you should see the following:&#xA;

If everything looks good, you can continue following the steps in [Making Changes and Creating a Pull Request](#making-changes-and-creating-a-pull-request) to commit, test and submit your changes.

If not, read on!

#### Out-of-Sync Branches

Your master or working branch may get out-of-sync. In general, **do not use VSCODE or GITHUB MERGE** to resolve merge conflicts. Here are some commands to run for common out-of-sync situations:

(Note: you check the status of your branch by running `git status` or going to `github.com/your-username/openlibrary`)

**Local master is behind upstream master**
![OL_Git_UnsyncedMaster](https://github.com/internetarchive/openlibrary/assets/79802377/80fe990e-72be-4eff-beb2-b4ad7f888378)

```sh
git switch master
git pull --ff-only upstream master
git push origin master
```

**Local master is both behind and ahead of upstream master**
![OL_Git_UnsyncedMaster2](https://github.com/internetarchive/openlibrary/assets/79802377/a28aa6f0-2136-4bad-8564-bea9b893c44d)

```sh
git switch master
git reset --hard HEAD~[the number of commits you are ahead by]
git pull --ff-only upstream master
git push -f origin master
```

> \[!WARNING]
> `git reset --hard` deletes commits permanently. You cannot recover these commits. Run this command only on the `master` branch, never on a working branch.

Your local `master` branch must match `upstream` `master`. If you accidentally made commits on `master`, remove these commits with the commands above. Then pull from `upstream` again.

**Working branch is behind upstream master (it will be ahead if you've made local changes):**
![OL_Git_UnsyncedBranch](https://github.com/internetarchive/openlibrary/assets/79802377/2074f643-1bf0-45aa-ab10-cb68e9763e78)

```sh
git switch master
git pull --ff-only upstream master
git push origin master
git switch your-branch-name
git rebase master
```

If rebasing your branch fails or provokes merge conflicts, see [Troubleshooting](/developers/tools/git-troubleshooting.md#troubleshooting).

When you're done working and try to push your branch with `git push origin HEAD`, you may encounter the following error:

This usually means that, as a result of the rebase, you'll need to force push\* your updates:

```sh
git push -f origin HEAD
```

\***Note:** While regular pushing just adds new commits to the remote branch, force pushing *replaces* the commits on the remote branch with the commits on your local branch, effectively re-writing the remote commit history. Sometimes when you perform a rebase, you will have to force push to your branch.

If so, be sure to **force push with care:** You should *only* force push if working on one of your own branches. If working on a branch which other people are also pushing to, force pushing is dangerous because it can override others' work. In that case, use `--force-with-lease`; this will force push *only* if someone else hasn't made any changes to the branch.

If none of the above solved the issue, you can consult a staff member or the issue's lead, and/or see [Troubleshooting](/developers/tools/git-troubleshooting.md#troubleshooting).

### Making Changes and Creating a Pull Request

**1. Make sure master is up-to-date (use [Canonical Sync Commands](#canonical-sync-commands)):**

For troubleshooting or to learn more, see [working on your branch](#working-on-your-branch).

**2. [Create a new branch for the feature or issue you plan to work on](https://github.com/internetarchive/openlibrary/blob/master/CONTRIBUTING.md#development-practices) and switch to it:**

```sh
git switch -c 1234/fix/fix-the-thing
```

Specifying `-c` creates a new branch, and `switch` switches to it. The typical branch name format is `[issue number]/[fix/feature/refactor/docs/test/hotfix]/[short-issue-description]`

**3. Make changes/commit:**

```sh
git add the-file.html
git commit -m "Fixed the thing"
```

A commit message should answer three primary questions:

* Why is this change necessary?
* How does this commit address the issue?
* What effects does this change have?

**4. Test your changes:**

```sh
docker compose run --rm home make test
```

When you submit your pull request, the [GitHub CI server](/developers/tools/pre-commit.md#the-github-ci-server) will automatically run a few more tests and formatting checks.

If you'd like, you can run these checks before you submit by [installing `pre-commit` locally](/developers/tools/pre-commit.md#installing-pre-commit).

**5. Push the branch:**

```sh
git push origin HEAD
```

**Note:** `HEAD` refers to your current branch, so make sure you're on the right branch.

**6. Submit your pull request:**

Go to [github.com/internetarchive/openlibrary/pulls](https://github.com/internetarchive/openlibrary/pulls); find the message at the top re: your branch and click `Compare & pull request`.
![OL_Git_PR](https://github.com/internetarchive/openlibrary/assets/79802377/d58c3d92-e281-4775-9eab-084490887d11)

Confirm that the changes in `Files Changed` match the changes you have made on your working branch and intend for this PR. If not, see [Troubleshooting](/developers/tools/git-troubleshooting.md#troubleshooting).

If everything looks right, you can write out an explanation of your changes using the provided template and submit. Your code is now ready for review.

**Reminder:** Any time you return to your branch to make changes, be sure to follow steps in [Working on Your Branch](#working-on-your-branch) to make sure your branch stays up to date.

## Updating Your Pull Request

Pull requests often receive feedback; to make [requested changes](https://github.com/internetarchive/openlibrary/blob/master/CONTRIBUTING.md#submitting-pull-requests) to your existing pull request:

### 1. Make sure your branch is up to date with master

Use [Canonical Sync Commands](#canonical-sync-commands).

### 2. Switch to your branch

```sh
git switch your-branch-name
git rebase master
```

For more on rebasing, see [Working on Your Branch](#working-on-your-branch).

### 3. Make your changes and commit (same as step 3 above)

### 4. Push your changes up

```sh
git push origin HEAD
```

**Note:** When trying to push you may see a warning like the following:

This usually means you need to *force-push* your changes as a result of the rebase:

```sh
git push -f origin HEAD
```

To learn more, see [Working on Your Branch](#working-on-your-branch).

## Troubleshooting

Common problems have documented fixes. Do not improvise with destructive commands.
See the [Git Troubleshooting guide](/developers/tools/git-troubleshooting.md) for help with rebase conflicts, unrelated commits, POT check failures, disconnected remotes, and manual merge conflicts.

## References

### Pre-commit and the GitHub CI

To automatically ensure that certain formatting practices are maintained throughout the codebase, and that any incoming pull requests pass a set of requisite JavaScript and Python checks, Open Library uses GitHub's Continuous Integration (CI) Server with a set of [`pre-commit` hooks](https://pre-commit.com/) to run a series of automated checks on incoming PRs.

For detailed information about pre-commit, including how to install and run it locally, troubleshooting CI failures, and handling POT file generation, see the [Pre-Commit Guide](/developers/tools/pre-commit.md).

### Tips on Code Reviews

On GitHub, you can select a range of lines related to your feedback. You can also make a code `suggestion` that can be directly applied by the author using the `+-` suggest/edit UI:

![Screenshot 2025-05-20 at 6 58 13 AM](https://github.com/user-attachments/assets/f808b162-5c76-4f42-8ca0-db85291cf899)

You can edit the code within the `suggestion` backticks to propose your changes:

![image](https://github.com/user-attachments/assets/52e021b2-d926-4337-8c05-b88ecfd69b57)

Result:

![Screenshot 2025-05-20 at 7 00 14 AM](https://github.com/user-attachments/assets/099d50f1-d5f8-490c-a496-9a1f21bbf71a)

* Getting Started flow roughly based on [this gist by Chaser324](https://gist.github.com/Chaser324/ce0505fbed06b947d962)
* Example of [code suggestions in a PR](https://github.com/ArchiveLabs/lenny/pull/23/files#r2098066506)
