This error occurs when Git cannot resolve a reference like HEAD~ because the repository has no commits, only one commit (no parent exists), or the reference is otherwise invalid. It commonly appears when trying to undo commits in a new repository.
The "fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree" error means Git cannot find the commit you're trying to reference. HEAD~ (or HEAD~1) refers to the parent commit of your current HEAD position. This error typically occurs in three scenarios: 1. **New repository with no commits**: In a freshly initialized repository with no commits, HEAD doesn't point to anything valid yet. 2. **Initial commit with no parent**: If your repository only has one commit (the initial commit), there is no parent commit to reference. HEAD~ asks for the commit before the current one, but the first commit has no predecessor. 3. **Shallow clone**: If you cloned with `--depth=1`, Git only fetched the most recent commit, so parent commits may not exist locally. This is a common issue when using VSCode's "Undo Last Commit" feature or running `git reset HEAD~` after making only one commit.
First, verify how many commits exist in your repository:
git log --onelineIf this shows only one commit or fails entirely, that confirms you're trying to reference a non-existent parent commit.
Check if HEAD points to a valid commit:
git rev-parse HEADIf this fails with "unknown revision or path not in the working tree", your repository has no commits yet. Make an initial commit first:
git add .
git commit -m "Initial commit"To undo the very first commit while keeping your files, use git update-ref:
git update-ref -d HEADThis deletes the HEAD reference, effectively reverting to a state before the initial commit. Your staged files remain in the index. To also unstage files:
git update-ref -d HEAD
git resetIf you're working with a shallow clone, fetch the complete history:
# Fetch all history
git fetch --unshallow
# Or fetch a specific depth
git fetch --depth=100After this, parent commit references should work correctly.
For the initial commit, consider these alternatives:
Amend instead of reset:
git commit --amend -m "New message"Create an orphan branch to start fresh:
git checkout --orphan new-start
git add .
git commit -m "Fresh start"
git branch -D main # Delete old branch
git branch -m main # Rename current branchIn automated scripts, check commit count before using HEAD~:
# Check if there are at least 2 commits
if [ $(git rev-list --count HEAD) -gt 1 ]; then
git diff HEAD~
else
echo "Not enough commits for HEAD~ reference"
fiFor CI/CD, ensure you're not using a shallow clone if you need history:
# GitHub Actions example
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all historyUnderstanding Git references: HEAD~ is shorthand for HEAD~1, meaning "the first parent of HEAD." HEAD~2 means "the grandparent of HEAD." These only work when those ancestors exist.
Orphan branches: An orphan branch has no commit history. Creating one is useful when you want a completely fresh start without deleting the repository:
git checkout --orphan fresh-branchThe difference between HEAD~ and HEAD^: Both reference parent commits, but they differ for merge commits. HEAD~ always follows the first parent, while HEAD^ can specify which parent (HEAD^2 for second parent in a merge).
Repository integrity: If you suspect corruption, run git fsck to check for missing or corrupt objects. Missing commits may cause reference errors even in repositories with valid history.
Interactive rebase limitations: Like reset, interactive rebase (git rebase -i HEAD~N) requires N ancestor commits to exist. For the initial commit, use git rebase -i --root instead.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server