Skip to content
Versora

Manual

Changes

Discarding changes

12 min read

On this page

Discarding is returning a file to what it was before your changes: the edit disappears from disk. It's what you do with a test that didn't work out, a log the program wrote in the wrong place or a forgotten debug line.

In plain git, discarding can't be undone. In Versora it can: before any discard, the app keeps a copy of what is about to be lost, and Ctrl+Z brings it back. Even so, each discard asks for confirmation.

Which one to use

You want to throw away…UseScope
Everything that changed in a file (unstaged)Discard changes… in the file menuThe file on disk goes back to what's in the stage; a new file is deleted.
A block of changesDiscard hunk, in the diffOnly that hunk.
A few linesDiscard lines, in the diffOnly the marked lines.
Absolutely everythingDiscard all changes…Stage, changes and new files of the whole repository.
The file itselfDelete file…Deletes the file from disk.

Caution

Discarding a file, a hunk or lines acts on what is unstaged. What has already been staged stays as it is. To throw away a staged change, unstage it first (see Staging) or use Discard all changes….

Discard a file's changes

Returns the file on disk to what is in the stage (for a file that isn't staged, that's the same as the last commit). On a new file, which git doesn't track yet, discarding means deleting it.

Where to find it

  • File menu in Unstaged files: Discard changes… (in red).

Step by step

  1. In the changes panel, right-click src/login.ts, in the Unstaged files section.
  2. Choose Discard changes….
  3. The Discard changes window asks: "Discard the changes in "src/login.ts"? You can undo it right after (Ctrl+Z).". On a new file, it adds: "Untracked files will be deleted from disk.".
  4. Click Discard. The toolbar shows "Discarding changes…" and the file leaves the section.

What Versora does in git

bash
# 1. Backup copy of the file (see "Undoing a discard" below)

# 2. Tracked file: the disk goes back to what's in the stage
git --literal-pathspecs restore --worktree -- src/login.ts

# 2. New (untracked) file: it's deleted
git --literal-pathspecs clean -f -q -- notas-temporarias.txt

How to undo

Ctrl+Z (or the toolbar's undo button, "Undo: Discard changes (Ctrl+Z)") puts the file back as it was, including a deleted new file. See "Undoing a discard" below for the cases where undo refuses.

Caveats and messages

  • On a file that is in both sections, only the unstaged part is discarded; the staged part stays there.
  • The item doesn't appear for conflicted files (use the conflict editor) or for files in the Staged files section.

See also

Discard a hunk

Throws away a block of changes from a file, leaving the rest of the file as it is.

Where to find it

  • In the "unstaged" diff, on each hunk's header: Discard hunk (in red, between Stash and Stage hunk). Only in the Diff view (not in the File view).

Step by step

  1. Open the file's diff in the Unstaged files section.
  2. Find the hunk and click Discard hunk.
  3. The Discard this hunk window asks "Discard this hunk from the file? You can undo it right after (Ctrl+Z).".
  4. Click Discard ("Discarding hunk…"). The hunk's added lines disappear from the file and the removed ones come back.
OptionWhat it doesDefault
Context lines ("− 3 +" on the diff bar)With less context, nearby changes stay in separate hunks and you discard only the one you want.3

What Versora does in git

The app builds a patch with only that hunk and applies it in reverse on the file on disk:

bash
git apply --whitespace=nowarn --reverse hunk.patch

(In the app the patch goes through standard input; hunk.patch is just for illustration.) Before that, the file is copied for undo.

How to undo

Ctrl+Z puts the file back as it was before the discard ("Undo: Discard hunk (Ctrl+Z)").

Caveats and messages

  • With "Ignore whitespace" on, the button is disabled: "Unavailable while "Ignore whitespace" is on: the diff without the whitespace doesn't match the real file and can't be safely applied back.".
  • There's no Discard hunk in the "staged" diff or in a new file's diff.
  • On a file in another encoding read the wrong way, "The diff was read as UTF-8, but the file has bytes that aren't valid UTF-8. Choose the file's correct encoding before staging or discarding by line or by hunk." appears. See Legacy encodings.
  • If the file changed on disk after the diff was read, git may refuse the hunk (the message comes from git itself). Refresh (F5) and try again.

See also

Discard lines

Throws away only the lines you mark in the diff.

Where to find it

  • In the "unstaged" diff, mark the lines by clicking their numbers (with Shift+click for a range). On the bar that appears at the bottom: Discard lines.

Step by step

  1. Open the file's diff in the Unstaged files section.
  2. Click the numbers of the lines you want to throw away. The selection bar shows, for example, "2 lines selected".
  3. Click Discard lines.
  4. The Discard 2 selected lines window asks "Discard 2 selected lines from the file? You can undo it right after (Ctrl+Z).".
  5. Click Discard ("Discarding lines…").

What happens to each line

Marked lineAfter the discard
Added (+)Leaves the file.
Removed (−)Comes back to the file.
Not markedStays as it is.

What Versora does in git

The app builds a patch with only the marked lines (the added ones you didn't mark become context; the removed ones you didn't mark leave the patch) and applies it in reverse on the disk:

bash
git apply --whitespace=nowarn --reverse lines.patch

How to undo

Ctrl+Z puts the file back as it was ("Undo: Discard lines (Ctrl+Z)").

Caveats and messages

  • With "Ignore whitespace" on, the lines can't be chosen.
  • Discard lines only appears in the "unstaged" diff. In the "staged" diff, the bar only offers Unstage lines.
  • The same encoding and changed-file messages as for discarding a hunk apply here.

See also

Discard all changes

Takes the whole repository back to the last commit: discards everything that is staged and unstaged and deletes the new files git doesn't track. Files ignored by .gitignore aren't touched.

Where to find it

  • The trash can button at the top of the changes panel ("Discard all changes…").
  • // WIP row menu in the graph: Discard all changes….
  • Palette (Ctrl+P): "Discard all changes…" ("Asks for confirmation"). Without changes, the palette shows the command as unavailable: "there are no changes".

Step by step

  1. Click the trash can button of the changes panel.
  2. The Discard all changes window asks "Discard ALL changes (staged or unstaged) and delete untracked files? You can undo it right after (Ctrl+Z).".
  3. Click Discard all ("Discarding changes…").
  4. The panel becomes empty and the // WIP row disappears from the graph.

What Versora does in git

bash
# 1. Backup copy of the whole working directory and the stage

# 2. Stage and tracked files back to HEAD
git reset -q --hard HEAD

# 3. Untracked files and folders (the ignored ones stay)
git clean -f -d -q

In a repository that doesn't have commits yet, only step 3 runs.

How to undo

Ctrl+Z brings everything back: the changed files, what was staged and the new files ("Undo: Discard all changes (Ctrl+Z)").

Caveats and messages

  • Ignored files stay: git clean doesn't get -x. Builds, node_modules and other ignored files stay on disk (and don't go into the backup copy either).

  • Nested git repositories (untracked folders with their own .git) aren't deleted by git clean.

  • With a stopped operation, the confirmation says what happens to it:

    • in a merge, cherry-pick or revert, git reset --hard also ends the operation: "With the merge in progress, this also ends the merge (like Abort); Ctrl+Z brings the merge back, with the conflicts.";
    • in a rebase, the operation stays: "The rebase stays stopped: only this step's changes go, conflict resolutions included.".

    Ctrl+Z brings back the files, the stage with the conflicts and, in the merge, the operation itself (see "Undoing a discard" below). If what you want is to give up on the merge, Abort on the operation bar is the direct way: see Operation in progress.

  • To keep the work instead of throwing it away, use a stash or a snapshot.

See also

  • Hard reset: moving the branch back to another commit and discarding the changes.

Delete a file

Deletes a file from disk through Versora itself, without opening File Explorer. On a tracked file, the deletion then appears as a change to commit.

Where to find it

  • File menu in Unstaged files, at the end: Delete file… (in red). It only appears for a file that exists on disk and isn't conflicted.

Step by step

  1. Right-click the file and choose Delete file….
  2. The Delete file window asks:
    • on a tracked file: "Delete "docs/rascunho.md" from disk? The deletion will show up as a change to commit.";
    • on a new file: "Delete "notas.txt" from disk? The file isn't in Git; only Versora's undo (Ctrl+Z) can bring it back.".
  3. Click Delete file ("Deleting file…").

What Versora does in git

The app keeps the file's backup copy and deletes it directly from disk (without a git command). On a tracked file, git then shows it as deleted:

bash
# equivalent in the terminal
rm docs/rascunho.md
git status   # docs/rascunho.md appears as deleted

How to undo

Ctrl+Z brings the file back ("Undo: Delete file (Ctrl+Z)").

Caveats and messages

  • "Couldn't delete C:\Projetos\loja\docs\rascunho.md: …": Windows didn't allow the deletion (file open in another program, no permission). Close the program and try again.
  • To remove a file from git without deleting it from disk, use Ignore › "file — ignore and stop tracking" (see Ignoring files).

See also

Undoing a discard: the backup copy

Every time you discard a file, a hunk, lines or everything, or delete a file, Versora first keeps a copy of what is about to be lost. That copy is what Ctrl+Z uses to bring the files back.

Where to find it

  • Shortcut: Ctrl+Z undoes, Ctrl+Shift+Z or Ctrl+Y redoes (outside text fields).
  • The undo and redo buttons on the toolbar, with the tooltip "Undo: Discard hunk (Ctrl+Z)".
  • Palette (Ctrl+P): "Undo" and "Redo".
  • The timeline (Alt+Z) shows the undo stack, with what each operation changed.

Step by step

  1. Discard a hunk by mistake.
  2. Press Ctrl+Z (with the focus outside a text field).
  3. "Undoing…" appears and then "Undone: Discard hunk". The file comes back as it was, including what was staged of it.

What Versora does in git

Before the discard, without touching your files or your stage:

bash
# A temporary index receives the files that will be discarded, as they are on disk
GIT_INDEX_FILE=.git/versora-undo-index-<pid> git add -A -- src/login.ts
GIT_INDEX_FILE=.git/versora-undo-index-<pid> git write-tree
git commit-tree <tree> -p HEAD -m "versora: restore point"
git update-ref refs/versora/undo/<date-and-time> <commit>

# The current stage is also saved, as a tree
git write-tree

On Ctrl+Z:

bash
git --literal-pathspecs restore --source=<copy> --worktree -- src/login.ts
git --literal-pathspecs restore --source=<stage-tree> --staged -- src/login.ts

With a stopped operation or conflicted files, the copy is made file by file, to also keep the conflicting versions in the stage:

bash
git ls-files --stage -z -- src/login.ts                  # stage entries, with stages 1, 2 and 3
git hash-object -w --no-filters --stdin-paths            # the content on disk, byte by byte
git mktree                                               # a tree with these objects
git commit-tree <tree> -m "versora: restore point (stopped operation)"
git update-ref refs/versora/undo/<date-and-time>-f<n> <commit>

The operation's state files (MERGE_HEAD, MERGE_MSG, CHERRY_PICK_HEAD…) are also read. On Ctrl+Z, the app restores the stage entries with git update-index -z --index-info, rewrites the files on disk from the saved objects and, if the discard had ended the merge, recreates its state files.

The refs/versora/undo/* refs don't appear as branches, don't go into a push and keep git gc from deleting the copy. Versora keeps the 20 most recent for each repository and deletes the older ones on its own. When the operation involves only a few files, only those are copied (it's much faster in a large repository).

When undo refuses

Undo never runs over new work. It refuses, with one of these messages, when:

MessageWhat happenedWhat to do
"The files were changed again after this operation. Undoing now would overwrite those changes."You (or the editor) touched the same files after the discard.Undo the new edit in the editor, or recover the content from the local history.
"The repository has changed since this operation, so it isn't safe to undo it automatically."A branch, a tag, HEAD or the stashes changed outside the app (from the terminal, for example).See How to recover lost work.
"A merge is in progress. Finish or abort it (in the changes panel) before undoing." (or rebase, cherry-pick, revert)An operation is stopped halfway, and the step to undo is from before it. (Discards made during the stop are undone normally.)Finish or abort the operation and try again.
"A bisect is in progress. End it (in the bisect bar, below the toolbar) before undoing."A bisect is active.End the bisect.

If "Nothing to undo." appears, the undo stack is empty.

Caveats and messages

  • Undo undoes the last operation. If you did something else after the discard (a commit, for example), the first Ctrl+Z undoes that other thing; the next one, the discard.
  • An undone discard isn't redone: after the files are brought back, Ctrl+Shift+Z doesn't discard again. To discard once more, use the button again. The exception is a discard made with a stopped operation or with conflicted files (next item): that one is redone.
  • Discards with a stopped operation (merge, rebase, cherry-pick, revert or bisect) or with conflicted files also go into undo. In this case the copy is different: it keeps, file by file, the stage entries with the conflicting versions (the base, current and incoming stages), the content on disk and the merge's state files (MERGE_HEAD, MERGE_MSG…). That's why Ctrl+Z brings the conflicts back as they were and, after a Discard all in a merge, brings the merge back.
  • This undo only applies while the operation stays stopped. When it ends (with Continue, with the commit, with Abort or even from the terminal), the steps taken during the stop leave the undo history: if it was completed, what goes in is the whole operation (see Operations stopped halfway). If the operation changed outside the app and the step was still on the stack, undo refuses with "The operation this was done in is no longer as it was (it was finished, aborted or restarted): this step can no longer be undone." and adds "That step was removed from the undo history; press Ctrl+Z again to go on with the previous one.".
  • The undo stack lives while the app is open (up to 40 operations per repository). When you close Versora, Ctrl+Z no longer reaches the earlier discards, but the copies in refs/versora/undo/* stay in the repository.
  • If the backup copy fails for some reason, the discard happens anyway; it just can't be undone.
  • Files ignored by .gitignore don't go into the copy (and aren't discarded either).

Tip

Recovering an old copy by hand (after closing the app, for example): list the copies and restore the file from one of them in the terminal.

bash
git for-each-ref --sort=-refname refs/versora/undo
git restore --source=refs/versora/undo/<date-and-time> -- src/login.ts

Each copy keeps the files of that operation as they were on disk; the others, as they were in the stage. For the common cases, the local history is simpler.

There's no preference to turn off the backup copy and no policy key that changes this behavior.

See also