I need to reset all uncommitted Git changes while working in a local Git repository. I’m using this answer to the 12 year old Unstaged changes left after git reset –hard question in a Python script
git rm .gitattributes
git add -A
git reset --hard
though I use Python os.remove
rather than git rm
to delete the .gitattributes
file. I’m using Git 2.43.0 and Python 3.12.3 on Ubuntu 24.04:
The problem I’m having is that this seems to intermittently leave uncommitted files as reported by git status
(branch and file names redacted):
[09/11/24 16:34:11] Executing command: ["git", "status"], execution time: 0.031 seconds
On branch ****
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: ****
modified: ****
modified: ****
no changes added to commit (use "git add" and/or "git commit -a")
[09/11/24 16:34:11] Executing command: ["git", "status"], execution time: 0.029 seconds
On branch ****
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: .gitattributes
modified: ****
modified: ****
modified: ****
no changes added to commit (use "git add" and/or "git commit -a")
[09/11/24 16:34:11] Executing command: ["git", "add", "-A", "-f"], execution time: 0.029 seconds
[09/11/24 16:34:11] Executing command: ["git", "status"], execution time: 0.029 seconds
On branch ****
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: .gitattributes
[09/11/24 16:34:11] Executing command: ["git", "reset", "--hard"], execution time: 0.046 seconds
HEAD is now at 9c1d6ae 592: setup
[09/11/24 16:34:11] Executing command: ["git", "status"], execution time: 0.033 seconds
On branch ****
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: ****
modified: ****
modified: ****
no changes added to commit (use "git add" and/or "git commit -a")
Rerunning the Python script may succeed or fail in the same way on different set on uncommitted changes.
One thing that seems strange to me is that three changes not staged for commit before git add -A -f
don’t show after either as staged or as not staged; only the .gitattributes
delete shows as staged.
Does anyone know why this happens. Could be possible that git add -A -f
returns before all files are staged? Is there a better way to reset all uncommitted changes?
Additional Information:
The problem has to do with Git’s end-of-line conversion. My .gitattributes
file contains a * text
entry as I want end-of-line conversion enabled for most files. For those files which should not have end-of-line conversion enabled, I add a <file-path> -text
entry to .gitattributes
.
The problem is that git reset --hard
resets the index and working tree and then Git end-of-line conversions “makes” some files as “Changes not staged for commit”, which is not what I want. My understanding is that the solution I used from the 12 year old question is supposed to address this.
5