-1

I had a local file (.foo) that was part of the working directory that should have been in .gitignore.
I added the file in the .gitignore but in the end by doing:

git add .  
git commit -m "added files"
git push 

So the file is now in the master branch.
I am trying to remove it but trying to do something like go to the previous SHA I get errors as a non recognised file.
What can I do to remove the file from the master branch. It is the top of the branch commit

Update

The file had been committed/deleted in the past based on git log .foo My commit added the entry .foo in the .gitignore along with the file.
My commit is the not the top commit in the branch. The top commit is the merge commit for my commit.
I want to remove .foo as I never pushed the file in my commit.

Jim
  • 3,845
  • 3
  • 22
  • 47
  • 2
    Are you trying to remove the file from the current revision, or are you trying to rewrite your git history so that the file was never tracked? – Brian61354270 Aug 20 '23 at 19:49
  • 1
    Does this answer your question? [How do I make Git forget about a file that was tracked, but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-do-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – evolutionxbox Aug 20 '23 at 19:50
  • 1
    If the file was properly ignored, and untracked, the commands you ran would not have added the file. Either it was not actually ignored in the `.gitignore`, or it was already being tracked before it was ignored. – TTT Aug 20 '23 at 20:04
  • Try `git log ` to see the history of that file. The oldest commit is probably where it got added. Was the file first added in the current commit, or in some other commit in the past? – TTT Aug 20 '23 at 20:06
  • @Brian61354270: Like it was never tracked – Jim Aug 20 '23 at 23:04
  • @TTT: The file was not part of the `.gitignore`. I added the filename in the `.gitignore` in the same commit that I added the file too – Jim Aug 20 '23 at 23:04
  • @TTT: I can see that the file was added again in the past and the following commit deleted it. It should have been in the `.gitignore`. I added the file again by mistake in the same commit as the entry about it in the `.gitignore` – Jim Aug 20 '23 at 23:07
  • Does this answer your question? [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – Brian61354270 Aug 21 '23 at 00:03
  • @Brian61354270: The issue I have is that the top commit is actually a merge commit and not my commit. I just realised that – Jim Aug 21 '23 at 07:14
  • OK. With the edits it sounds like you want to rewrite your local `master`, so you'll just need to redo the commit without that file, and then redo the merge. Perhaps a graph of what your `master` branch looks like now with some commit IDs would help- then we can answer with exact instructions. – TTT Aug 21 '23 at 14:35
  • Side note, another way to remove the file is with `git filter-repo`. But that's probably overkill for removing it from a recent local commit. If you wanted to remove it both from the recent commit and also the old commit where it was added and deleted, to make it look like it was never there at all, then you'd probably want to use `git filter-repo`. I assume you don't want that though as you'd likely have to force-push if you change an old commit. – TTT Aug 21 '23 at 21:23

0 Answers0