Resetting Paths
Resetting Paths
After this page you can change one path in the index while the branch name stays put.
A path means the branch stays
With no path, git reset moves HEAD. With a path, HEAD stays and Git copies that path from a commit into the index only.
cd ~/desk/desk-api
git switch main
git status
BEFORE=$(git rev-parse --short HEAD)
echo "path lab" >> README.md
git add README.md
git commit -m "lab: path reset"
git reset HEAD~1 -- README.md
git rev-parse --short HEAD
echo "parent was $BEFORE"
git status
git diff -- README.md
git diff --staged -- README.mdThe short hash is the commit you just made, lab: path reset. It is not $BEFORE. Status lists README.md in two places:
- Changes to be committed. The index holds the parent’s blob.
- Changes not staged for commit. The working tree still has the
path labline.
That is one edit, seen from both sides. git diff shows the line added. git diff --staged shows the line removed. Git also prints Unstaged changes after reset:.
HEAD stays on "lab: path reset"
index README ← parent blob
worktree README ← still has "path lab"
Older notes unstage with git reset HEAD -- README.md. This book unstages with git restore --staged (chapters 3 and 6). The path form is how you aim the index at an older blob, and how you read a script that still uses it.
Put the file back, then drop the commit
HEAD is still the scratch commit. Copy that commit’s README.md into the index and the working tree:
git restore --source=HEAD --staged --worktree -- README.md
git statusStatus is clean. The log still shows lab: path reset.
git reset --hard HEAD~1
git log --oneline -3The scratch commit is off main, and README.md has no path lab line. The path reset did not do that. The last command is a branch reset from the first page of this chapter.
Whole tree versus one path
git reset HEAD~1
main slides to the parent
git reset HEAD~1 -- README.md
main stays; the index path changes
git restore --source=HEAD~1 --staged --worktree -- README.md
main stays; index and worktree take the old blob
The third command is how you make a file look like an older commit and then record that with a new commit. The older commit stays in history. switch and restore on the next page are the same split: names versus bytes.