If you accidentally delete a script or folder inside a repository in the
[Earth Engine Code Editor](https://developers.google.com/earth-engine/guides/playground), or need to migrate
a repository to a new account, the full commit history is preserved in the Git
logs on our servers. While the repository
icon in the Code Editor reverts an
entire repository to a previous state, cloning the repository locally with
standard Git command-line tools lets you selectively restore individual scripts
or folders without losing newer changes to other scripts.

## What can be recovered

In the Code Editor **Scripts** tab, your work is organized as
`users/username/repository:folder/script`:

- **Scripts and folders (recoverable):** Deleting a script or folder inside a repository only removes it from the latest commit. Because the repository itself still exists on `earthengine.googlesource.com`, you can clone the repository and restore the deleted files from its Git commit history using [Option A](https://developers.google.com/earth-engine/guides/script_recovery#option_a_restore_a_deleted_script) or [Option B](https://developers.google.com/earth-engine/guides/script_recovery#option_b_restore_a_deleted_folder).
- **Entire repositories (not recoverable):** Deleting a repository (`users/username/repository`) permanently removes the Git repository, its scripts, and its commit history from `earthengine.googlesource.com` (returning `"Repository is not found"`). Once deleted, a repository can't be cloned or recovered.

First complete the [Prerequisites](https://developers.google.com/earth-engine/guides/script_recovery#prerequisites), and then choose one of the
following options depending on your task:

- [Option A: Restore a deleted script](https://developers.google.com/earth-engine/guides/script_recovery#option_a_restore_a_deleted_script)
- [Option B: Restore a deleted folder](https://developers.google.com/earth-engine/guides/script_recovery#option_b_restore_a_deleted_folder)
- [Option C: Migrate a whole repository](https://developers.google.com/earth-engine/guides/script_recovery#option_c_migrate_a_whole_repository)

## Prerequisites

### Step 1: Generate Git credentials

Because Google-hosted Git repositories use secure token authentication, you must
first generate your local Git credentials:

1. Open an **incognito or private browsing window** in your web browser and visit the Git password generator: [earthengine.googlesource.com/new-password](https://earthengine.googlesource.com/new-password) *(Note: Sign in exclusively with your Earth Engine registered Google
   Account. If you are migrating a repository to a new account under
   [Option C](https://developers.google.com/earth-engine/guides/script_recovery#option_c_migrate_a_whole_repository), first share the old
   repository with your new account and sign in here with your **new** Google
   Account.)*
2. Copy the entire block of generated shell commands shown on the screen.
3. Open a terminal (command line) on your computer, paste the copied commands, and run them. This configures your machine's Git cookie credentials automatically.
4. If this is your first time using Git on your computer, configure your author
   name and email so Git lets you create commits:

       git config --global user.name "Your Name"
       git config --global user.email "you@example.com"

### Step 2: Clone your repository (Options A and B)

If you are migrating a repository between accounts, skip to
[Option C](https://developers.google.com/earth-engine/guides/script_recovery#option_c_migrate_a_whole_repository). To restore a deleted script or
folder, clone your repository in your local terminal by replacing `username` and
`repository` with your Earth Engine details (you can also copy the exact clone
command by clicking the icon next
to the repository name in the **Scripts** tab):

    git clone https://earthengine.googlesource.com/users/username/repository

Once complete, navigate into the cloned folder:

    cd repository

## Option A: Restore a deleted script

To recover a deleted script file (for example, `MyScript` or
`MyFolder/MyScript`):

> [!NOTE]
> **Note:** Scripts created in the Code Editor don't have a `.js` file extension in Git unless you explicitly included `.js` in the script name. If you don't remember the exact path or name of the deleted script, run `git log --diff-filter=D --summary --oneline` to list all commits that deleted files and their exact file paths.

1. View the file's history to find the deletion commit and the commit
   immediately prior to it (listed right after it in the log output):

       git log --oneline -- "MyScript"

2. Restore the last version where the file still existed to your folder
   (replace `<commit_hash>` with the hash of the commit *prior* to deletion, or
   use `<deletion_commit_hash>~1`):

       git checkout <commit_hash> -- "MyScript"

3. Commit and push the restored script back to the server:

       git commit -m "Restore MyScript from history"
       git push origin

Once pushed, reload the Code Editor **Scripts** tab in your browser (or click
the button at the top of the
**Scripts** tab) to see the restored script.

## Option B: Restore a deleted folder

To recover a deleted folder and all the scripts within it (for example,
`MyFolder/`):

> [!NOTE]
> **Note:** If you don't remember the exact name or path of the deleted folder, run `git log --diff-filter=D --summary --oneline` to see the paths of all deleted files.

1. Check your Git commit logs to find the deletion commit and the commit
   immediately prior to it (listed right after it in the log output):

       git log --oneline -- "MyFolder/"

2. Restore the last version where the folder still existed to your folder
   (replace `<commit_hash>` with the hash of the commit *prior* to deletion, or
   use `<deletion_commit_hash>~1`):

       git checkout <commit_hash> -- "MyFolder/"

3. Commit and push your recovered folder back to the server:

       git commit -m "Restore MyFolder and all its scripts"
       git push origin

Once pushed, reload the Code Editor **Scripts** tab in your browser (or click
the button at the top of the
**Scripts** tab) to see the restored folder.

## Option C: Migrate a whole repository

If you need to migrate an entire repository and its complete commit history to a
new account (for example, when transitioning between institutional and personal
accounts):

**Credentials and permissions** :
If you are migrating a repository between two **different Google Accounts** (for
example, from a university or work account to a personal account), you must
ensure your new account has permission to read the old repository before you
start:

- *The Sharing Prerequisite* : Sign in to your **old** Code Editor account, click the icon next to your old repository name, and share it with your **new** Google Account email address, granting it **Reader** access.
- By doing this, you only need to authenticate on your local computer **once** (using your new account's Git credentials generated in [Step 1](https://developers.google.com/earth-engine/guides/script_recovery#step_1_generate_git_credentials)). Your new account will have permission to both clone from your old repository and push to your new one, avoiding credential conflicts.

1. Clone your old repository locally using standard Git:

       git clone https://earthengine.googlesource.com/users/old_username/old_repository

2. Create a new, empty repository from the **New \> Repository** menu in your
   new Code Editor account. *(Note: Don't create or save any scripts or
   folders in the new repository before pushing, or the push will fail.)*

3. Navigate into your local cloned directory:

       cd old_repository

4. Push your entire local commit history and scripts directly to your new,
   empty Earth Engine repository:

       git push https://earthengine.googlesource.com/users/new_username/new_repository --all

Once pushed, reload the **Scripts** tab in your new account's Code Editor (or
click the button) to see the
migrated repository and history.

> [!CAUTION]
> **Caution:** Moving a repository to a new namespace doesn't automatically update `require('users/old_username/old_repository:...')` paths in scripts that import modules from the old repository. If other users rely on public modules in your old repository, keep the old repository active so their workflows don't break. If your scripts are private, update any `require()` statements to reference the new repository path before deleting the old repository.