Git 2.56 ยท independent examples

What do you want to change?

Choose a task, then match its starting state. Commands are text only. Nothing runs here, no repository is read and nothing is saved.

20 examples. Without JavaScript, every command and warning is readable below.

These are controlled examples using names such as note.txt and topic. Check your own files, branch and saved work before adapting a command. A matching label cannot inspect or verify your repository.

Last commit is local; keep its changes staged

Undo a commit

Before copying: Rewrites local history. Do not use on a commit others already depend on.

git reset --soft HEAD~1
What changes
Moves the current branch back one commit; index and files stay as they were.
Check the result
Compare log, status and staged diff before doing more work.
Undo boundary
A recorded old commit ID can restore the branch while its object remains available.

Read the measured example and its limits

Last commit is local; keep its changes unstaged

Undo a commit

Before copying: Rewrites history and replaces the staging selection. Record it first.

git reset --mixed HEAD~1
What changes
Moves the branch back and resets the index; working files remain.
Check the result
Inspect status and the unstaged diff.
Undo boundary
The old commit ID restores history; it does not recreate a separately edited index.

Read the measured example and its limits

Tracked note.txt is staged; keep its working edits

Stage or inspect files

Before copying: Replaces staged-only contents. Save the staged version first, for example: git diff --cached -- note.txt > staged.patch. Inspect that file before proceeding.

git restore --staged -- note.txt
What changes
Restores the index entry from HEAD without changing the working file.
Check the result
Inspect staged and unstaged diffs separately.
Undo boundary
Restaging the working file does not recover staged-only text that differed from it.

Read the measured example and its limits

Tracked note.txt has unwanted unstaged edits

Stage or inspect files

Before copying: Discards unstaged changes in note.txt. Save anything needed before proceeding.

git restore -- note.txt
What changes
Replaces the working file with its indexed contents.
Check the result
Read the file and compare it with the index.
Undo boundary
Uncommitted contents have no Git recovery guarantee.

Read the measured example and its limits

Current work should continue on a new topic branch

Manage branches

git switch -c topic
What changes
Creates topic at the current commit and switches to it.
Check the result
Check the current branch and status.
Undo boundary
Switch back before deleting an unused branch; preserve any new commits.

Read the measured example and its limits

Existing topic branch can be checked out safely

Manage branches

git switch topic
What changes
Switches to the existing topic branch when local edits do not obstruct it.
Check the result
Check the branch and working state.
Undo boundary
Switch back if safe; later edits still need their own handling.

Read the measured example and its limits

Current local branch needs a new name

Manage branches

git branch -m renamed
What changes
Renames the current local branch to renamed.
Check the result
List local branches; no remote branch is changed.
Undo boundary
Rename it back if the old name is available.

Read the measured example and its limits

Reverse the latest ordinary commit with a new commit

Undo a commit

git revert --no-edit HEAD
What changes
Creates a new reversal commit rather than moving the branch backward.
Check the result
Inspect the new commit and file contents.
Undo boundary
A further revert can reverse that reversal; conflicts may need resolution.

Read the measured example and its limits

Tracked edits should be saved in a stash

Save unfinished work

git stash push -m example
What changes
Saves tracked edits and restores their clean base state.
Check the result
List the stash and inspect what was saved.
Undo boundary
Plain stash apply does not restore the staged/unstaged split. Use git stash apply --index to attempt to restore staging too; inspect the result.

Read the measured example and its limits

Tracked and untracked edits should be saved together

Save unfinished work

Before copying: Moves untracked files out of the working tree. Verify the saved content before deleting the stash.

git stash push -u -m example
What changes
Includes untracked files in the stash, then removes their working copies.
Check the result
Inspect the stash and its untracked content before relying on it.
Undo boundary
Restore from the retained stash; ignored files were not included by this option.

Read the measured example and its limits

Restore a stash and keep the saved entry

Save unfinished work

git stash apply
What changes
Applies saved edits without dropping the stash entry; plain apply does not restore the staged/unstaged split.
Check the result
Inspect status, staged and working diffs, and the stash list. Use git stash apply --index when restoring staging matters.
Undo boundary
The saved entry remains; undoing newly combined edits needs care.

Read the measured example and its limits

Restore a stash and drop it after success

Save unfinished work

Before copying: A successful pop drops the saved entry. Use apply if it should remain available.

git stash pop
What changes
Applies saved edits and removes the entry only after a successful application.
Check the result
Inspect the files and stash list; conflicts change the outcome.
Undo boundary
After a successful drop the named entry is gone; do not rely on later recovery.

Read the measured example and its limits

Inspect the exact staged difference

Stage or inspect files

git diff --cached
What changes
Shows index changes relative to HEAD without editing them.
Check the result
Read the diff and check status.
Undo boundary
Read-only operation; no state restoration is needed.

Read the measured example and its limits

Inspect working edits not yet staged

Stage or inspect files

git diff
What changes
Shows working-file differences relative to the index.
Check the result
Compare with the staged diff when a file has both kinds of change.
Undo boundary
Read-only operation; no state restoration is needed.

Read the measured example and its limits

Inspect changes recorded for note.txt

Read history

git log --oneline -- note.txt
What changes
Lists commits touching the named path in the current history.
Check the result
Confirm the path and revision scope.
Undo boundary
Read-only operation; no state restoration is needed.

Read the measured example and its limits

A deleted branch tip is the verified previous HEAD entry

Manage branches

Before copying: HEAD@{1} means a specific reflog position, not any deleted branch. Verify the commit before copying.

git branch recovered 'HEAD@{1}'
What changes
Creates recovered at the retained reflog entry selected in this controlled example.
Check the result
Inspect the reflog first, then inspect the recovered commit and contents.
Undo boundary
The new branch can be removed after preserving its work; missing objects cannot be reconstructed.

Read the measured example and its limits

Stop tracking note.txt but keep its working copy

Stage or inspect files

Before copying: Stages a tracked-file deletion. This is not secret removal and does not erase earlier commits.

git rm --cached note.txt
What changes
Removes note.txt from the index while retaining its local file.
Check the result
Inspect status and confirm that the local file remains.
Undo boundary
Stage the file again if needed; a later committed deletion affects collaborators.

Read the measured example and its limits

Abort a merge that began from a clean commit

Leave a conflicted operation

Before copying: Can replace in-progress conflict edits. Preserve anything needed outside the operation first.

git merge --abort
What changes
Attempts to restore the pre-merge state.
Check the result
Check branch, status and file contents after aborting.
Undo boundary
The tested case began clean; pre-existing or later edits may not be recoverable.

Read the measured example and its limits

Abort a rebase that began from a clean commit

Leave a conflicted operation

Before copying: Abandons work done during the rebase. Inspect and preserve needed edits before aborting.

git rebase --abort
What changes
Returns to the branch state from before the rebase.
Check the result
Inspect the branch, log and working files.
Undo boundary
The controlled run restores the original branch; arbitrary edits during a rebase are outside it.

Read the measured example and its limits

Existing topic is not checked out elsewhere

Use another worktree

git worktree add ../topic-worktree topic
What changes
Adds a second working directory for the existing topic branch.
Check the result
List worktrees and inspect the second directory.
Undo boundary
Remove an unused clean worktree with Git after preserving its changes.

Read the measured example and its limits