fix: add -f flag to git rm --cached in revert for repos with no commits#304783
Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: add -f flag to git rm --cached in revert for repos with no commits#304783yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When unstaging files in a newly initialized repository (no commits yet), the revert method uses `git rm --cached -r` as a fallback. This fails when the staged content differs from the working tree copy, because git refuses to remove the cached entry without --force to prevent data loss. Adding the -f flag resolves this since the working tree file is preserved by --cached, so no data is actually at risk. Fixes microsoft#264338
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: @lszomoruMatched files:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
In a newly initialized repository (no commits yet), unstaging a file fails when the staged content differs from the working tree copy. For example:
git inita new repogit add)This fails with:
The root cause is that
Repository.revert()inextensions/git/src/git.tsusesgit rm --cached -ras a fallback when there are no branches (i.e., no commits yet). Without the-fflag,git rm --cachedrefuses to remove a cached entry when the staged content differs from the working tree, as a safety measure against data loss. However,--cachedonly removes from the index -- the working tree file is always preserved -- so the-fflag is safe here.Closes #264338
What is the new behavior?
The revert command now uses
git rm --cached -r -f, which allows unstaging files even when the staged content differs from the working tree copy. The working tree file is never deleted since--cachedensures only the index entry is removed.Additional context
This is a one-character change (adding
-fto the args array). Thegit rm --cachedpath is only taken when there are no branches (new repo with no commits), so this does not affect the normalgit resetpath used when commits exist.The
-fflag is safe in combination with--cachedbecause--cachedrestricts the operation to the index only -- the working tree file is never touched, regardless of whether-fis used.