# Restore an earlier version

Give a branch exactly the files of an earlier commit, in one new commit that keeps the history.

A restore makes a new commit on top of a branch whose files are exactly those of an earlier commit:
files added since are deleted, files deleted since come back, and changed files get their old
content. Nothing is rewritten or force-pushed, so every later commit stays in the history and can
itself be restored. The restore is the same act as a push: it passes branch protection and starts
CI and webhooks like one. Replace `5d6c2a1e-7b3f-4c8d-9e0a-1b2c3d4e5f60` with the repository's id
and 3f9c1e2a7b4d6c8e0f1a2b3c4d5e6f7a8b9c0d1e with the commit to restore.

## Prerequisites

- Permission for `git:PushRepository` on the repository, the same permission a push needs.
- The commit is reachable from a branch or tag of the repository. List a branch's commits with
  `dcloud git commits list` to find its id.
- The repository is not archived, not a mirror and not still importing.

## Restore it

Send the full commit id: a branch or tag name also works, but it can point somewhere else when you
retry. Send the commit the branch is at now as `expectedOid`, so a save that arrived meanwhile is
not overwritten unseen.

**CLI**
```sh
dcloud git commits restore 5d6c2a1e-7b3f-4c8d-9e0a-1b2c3d4e5f60 --branch main --commit 3f9c1e2a7b4d6c8e0f1a2b3c4d5e6f7a8b9c0d1e
```

**API**
```http
POST /git/api/repositories/5d6c2a1e-7b3f-4c8d-9e0a-1b2c3d4e5f60/restore
Content-Type: application/json

{
  "branch": "main",
  "commit": "3f9c1e2a7b4d6c8e0f1a2b3c4d5e6f7a8b9c0d1e",
  "expectedOid": "9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b",
  "message": "Restore version 3",
  "author": { "name": "Erika Mustermann", "email": "erika@example.com" }
}
```

`message` is optional; without it the commit is titled `Restore to <short id>: <subject of the
restored commit>`. `author` is optional too: a tool that saves on behalf of its own users names the
user there, and the commit records the caller as committer. The same `author` field is accepted
when you commit file changes with `git.commits.create`.

## Result

The call answers `200` with `{ "commitOid": "…", "restoredFrom": "…", "created": true }`:
`commitOid` is the new tip of the branch and `restoredFrom` the full id of the restored commit.
When the branch already holds exactly those files, no commit is made and the answer is `200` with
`created: false` and the current tip, also when `expectedOid` no longer matches, so a retry after a
timeout is safe.

Other answers:

- `409` `The branch moved since you started editing.` when the branch is no longer at
  `expectedOid`: read the new tip and decide again.
- `404` for a commit the repository does not hold or no branch or tag reaches.
- `422` for an object that is not a commit, or an abbreviated id that matches several objects.
- `409` with the rule's reason when branch protection refuses the change, and `403` without
  `git:PushRepository`.

## Related

- [Browse a repository](browse-a-repository.md)
- [Protect a branch](protect-a-branch.md)
- `git.commits.restore` · `dcloud git commits restore`
