Time: 20 minutes. Track: developer. Before this: lesson 2.
By the end of this lesson you will be able to:
- take back your last operation, and put it forward again;
- put files of an open change back as they were;
- take back a change that has landed, and say why that is a new change.
Three ways back
What you take back decides how. An operation, such as a snapshot or a rebase, is taken back with af undo, as in jj. Files of a change still open are put back with af restore. A landing is part of main’s history, which nobody rewrites: af revert makes a new change that reverses it, and that change lands through the gate like any other.
Start a forge, and land a first version of a file:
$ af-server --dev
...$ source ~/af-dev/dev/env
$ af workspace add ~/back
Workspace at /home/you/back
Paths: **
main has no landing yet
$ cd ~/back
$ af new -m "Add the limits"
Working on change yolwxwutonzxslrosympymwsnoxmkskk at commit bc38f42db8b4d114409f436bd548f50d2ed2a702975fabd4d2d516db42b16674
$ printf 'retries = 3\ntimeout = 30\n' > limits.toml
$ af submit
Submitted commit cd545060b7fbb92d2c06a9844441532aa3cb17f499cfbb6ad45a85a7a0e34be2, 1 edit in its last snapshot$ af status
Change yolwxwutonzxslrosympymwsnoxmkskk (landed) at commit cd545060b7fbb92d2c06a9844441532aa3cb17f499cfbb6ad45a85a7a0e34be2Undo an operation
Start a change and save two drafts of a note. af status saves the first, as every command does before it acts:
$ af new -m "Raise the limits"
Working on change qkqussuosvppvzlpnvnrsmqsrulrvklv at commit 4545f242560dc73cebbac09611ad935a879573761beaab366eb29b92352f5888
$ printf 'first draft\n' > NOTES
$ af status
Change qkqussuosvppvzlpnvnrsmqsrulrvklv (open) at commit 0b3c8dcd37ef63ad5976252f88cd8545a71a4295d706f3a054ee523f83f76229
Based on landing 1
Landing waits for: scan
$ printf 'second draft\n' > NOTESaf undo saves the second draft too, and takes it back: the note is the first draft again. af redo puts it forward:
$ af undo
undid operation 21991fb5faf4: 1 change put back, as operation af6a541e8cda
$ cat NOTES
first draft
$ af redo
redid operation af6a541e8cda: 1 change put back, as operation 8bd843eedac6
$ cat NOTES
second draftaf op log lists every operation, with who did it and whether you can still take it back; Undo a mistake shows it in full.
Restore files
The note was never meant for this change. Raise the limits, then put the note back as it is in the change’s parents, where it does not exist:
$ printf 'retries = 5\ntimeout = 60\n' > limits.toml
$ af restore NOTES
Rewrote change qkqussuosvppvzlpnvnrsmqsrulrvklv at commit 30e62f10d92c3a4a8b1a47715353e07da997f10b98b47e79b6edbf0a6e95bed3
$ ls
limits.toml
$ af diff
M limits.toml
--- a/limits.toml
+++ b/limits.toml
@@ -1,2 +1,2 @@
-retries = 3
-timeout = 30
+retries = 5
+timeout = 60Land the change:
$ af submit
Submitted commit 30e62f10d92c3a4a8b1a47715353e07da997f10b98b47e79b6edbf0a6e95bed3, 0 edits in its last snapshot$ af status
Change qkqussuosvppvzlpnvnrsmqsrulrvklv (landed) at commit 30e62f10d92c3a4a8b1a47715353e07da997f10b98b47e79b6edbf0a6e95bed3
Based on landing 1Revert a landing
The new limits were a mistake. af revert makes a new open change on main that reverses the landing, and leaves your workspace where it is; af edit moves you to the new change:
$ af revert -r 'main' | tee ~/revert.out
Started change vqmzrqpruruuzxplxpuuvpkkptwvomtu at commit ad2c09502d3e8106d97106a61f8cb801ca98aefd866a5c045cf98a5e4f3e7ce6
$ export REVERT=$(sed -n 's/^Started change \([k-z]*\) .*/\1/p' ~/revert.out)
$ af edit $REVERT
Working on change vqmzrqpruruuzxplxpuuvpkkptwvomtu at commit ad2c09502d3e8106d97106a61f8cb801ca98aefd866a5c045cf98a5e4f3e7ce6
$ cat limits.toml
retries = 3
timeout = 30
$ af submit
Submitted commit ad2c09502d3e8106d97106a61f8cb801ca98aefd866a5c045cf98a5e4f3e7ce6, 0 edits in its last snapshot$ af status
Change vqmzrqpruruuzxplxpuuvpkkptwvomtu (landed) at commit ad2c09502d3e8106d97106a61f8cb801ca98aefd866a5c045cf98a5e4f3e7ce6
Based on landing 2Main’s history keeps both: the landing, and the revert after it.
$ af log -r '::main'
@ vqmzrqpr dev@localhost 2026-11-25T08:16:12Z ad2c0950
│ Revert "Raise the limits"
○ qkqussuo dev@localhost 2026-11-25T08:16:12Z 30e62f10
│ Raise the limits
○ yolwxwut dev@localhost 2026-11-25T08:16:12Z cd545060
│ Add the limits
○ zzzzzzzz 1970-01-01T00:00:00Z 00000000
(no description set)Exercises
-
Start a change and take back its start with
af undo. What becomes of the change?$ af new -m "Try something" Working on change uzwxorxtmmwzukkvlkrnuztswozkvxwr at commit e0ca138df9b049c546e08b1695db914c92b79880689fe52cdbd685415ce0299e $ af undo undid operation fbe02ea21dc3: 1 change put back, as operation 680b0f891863 $ af status Change uzwxorxtmmwzukkvlkrnuztswozkvxwr (abandoned) at commit e0ca138df9b049c546e08b1695db914c92b79880689fe52cdbd685415ce0299e Based on landing 3 -
Undo an edit, then make any other operation, and try
af redo.$ af new -m "Edit the limits" Working on change zqvutpzryknuzvkukmptlrtltmtzlmtl at commit e5f738cd9303e0eb0670d88947575e78f58cb22ac43a5bd9ce22d1059a4dd697 $ printf 'retries = 4\ntimeout = 30\n' > limits.toml $ af status > /dev/null $ printf 'retries = 6\ntimeout = 30\n' > limits.toml $ af undo undid operation 3e489ddf5077: 1 change put back, as operation 556b8c6cb5b7 $ af describe -m "Edit the retries" Change zqvutpzryknuzvkukmptlrtltmtzlmtl is now at commit 68b777a71dc1e56ce277d30c14da342d97f4dd0e102ce3418151f0bb8c9287ce $ af redo Error: InvalidState: there is nothing to redo: your last operation was no undo [1]
Check your understanding
Why can af undo not take back a landing?
A landing is part of main’s history, which everyone builds on; taking it back in place would rewrite what others have. af revert instead adds a change that reverses it, and that change lands through the same gate as any other.
af restore NOTES took the note out of the change. Where did it put it back from?
From the change’s parents, where the note did not exist, so restoring it removed it. --from restores from another commit instead.
Why did af redo refuse in the second exercise?
Redo puts forward only the undo just before it. Describing the change was an operation of its own, so the undo was no longer your last operation, as in jj.
Next
Lesson 8: Workspaces and their paths holds only the part of the repository you work on, and changes it as you go.