4

I wanted to remove a file added by mistake in a commit in Git

I tried this code, but was surprised that it remove the file definitively. who can help me please?

Please who can help me? All my tests have failed.

I did when following this link Remove files from Git commit:

git reset --soft HEAD^ 
git reset --soft HEAD~1
git reset HEAD path/to/unwanted_file
git commit -m 'remove unwanted file'
biwia
  • 407
  • 1
  • 4
  • 10
  • 1
    Looks like you've executed the first two lines without noticing that they're in fact *alternatives*. If you do both, you've gone back one commit too far. – Romain Valeri Jul 05 '21 at 13:23
  • 1
    You literally cannot remove a file from an existing commit. No existing commit can be modified. All you can do is stop using the bad commit(s); if appropriate, you can instead make new-and-improved (corrected) commits to be used instead. None of this has anything to do with *removing* the file, as Git is not about *files*, but rather about *commits*. The files you see and work with in your working tree are not in Git at all. The issue you're running into is that you told Git to get some working tree file *from* a commit, and ... – torek Jul 05 '21 at 13:31
  • ... and you're now saying "switch from that (bad) commit to this other (good/improved) commit" and when Git does that it will *remove* the file from your working tree because the file *came out of* the bad commit and *isn't* in the *good* commit, so therefore, the way to update your working tree is to remove the file. If you want to *keep* the file, move it out of the way, switch commits, then move it back. – torek Jul 05 '21 at 13:32
  • ok im gonna try the first row then @RomainValeri do you know why the files is removed definitively? – biwia Jul 05 '21 at 13:38

1 Answers1

0

I would do it like this.

git checkout revision-where-it-was-added
git rm --cached the-file
git commit --amend --no-edit
git cherry-pick HEAD@{1}..the-branch # replay the other revisions in the branch
# if you like this resulting history:
git branch -f the-branch
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • @etfshift0 what is revision-where-it-was-added ? – biwia Jul 05 '21 at 13:35
  • the id of the revision where the file was added by mistake. – eftshift0 Jul 05 '21 at 13:38
  • when tried to run **git cherry-pick HEAD@{1}..the-branch** i got fatal: bad revision 'HEAD@{1}..feat-test01-_35,' – biwia Jul 05 '21 at 13:42
  • can you write the whole command that you are using? And make sure that you are using the right branch name.... and, just in case, you can use the current id of the tip of the branch instead of the branch name. – eftshift0 Jul 05 '21 at 13:53
  • ... and, you can use the same id of the revision that you used in the checkout for the cherry-pick: `git cherry-pick revision-i-used-for-checkout..the-branch`. – eftshift0 Jul 05 '21 at 13:55
  • thank you ! then should i do commit and push? – biwia Jul 05 '21 at 14:00
  • error: The following untracked working tree files would be overwritten by merge: src/file.py Please move or remove them before you merge. Aborting fatal: cherry-pick failed – biwia Jul 05 '21 at 14:03
  • After you have run your cherry-pick, you should get a history like the original one, but the file should not be added.... then you are done. You might want to push into a remote branch, if you are using one? – eftshift0 Jul 05 '21 at 14:03
  • i got this error **error: The following untracked working tree files would be overwritten by merge: src/file.py Please move or remove them before you merge. Aborting fatal: cherry-pick failed** – biwia Jul 05 '21 at 14:40
  • @etfshift0 it means that cherry-pick doesn't work :/ – biwia Jul 05 '21 at 14:54
  • I guess you should stash the changes on that file because git likes to do some operarions on a clean tree. – eftshift0 Jul 05 '21 at 15:02
  • how to do it? please.? – biwia Jul 05 '21 at 15:17
  • `git stash save "Will come back in a few minutes"`. Then, when you finish the whole thing: `git stash pop`. – eftshift0 Jul 05 '21 at 15:41
  • thank you, now my file is on the Untracked files. how can i do then ? – biwia Jul 05 '21 at 15:44
  • Given that you want to _not_ save it, I think it would be a great idea to add it to `.gitignore` so that you don't add it later on again. – eftshift0 Jul 05 '21 at 15:46
  • no i want to remove it from the commit but i want to keep it in my project of course! – biwia Jul 05 '21 at 16:13
  • Let me rephrase what I said: "given that you don't want to _keep it in git_, I think it would be a great idea to add it to `.gitignore` so that you don't add it later on again". – eftshift0 Jul 05 '21 at 16:14
  • Sorry it's me! the file should be in git, it concern another branch1 and i added by mistake in a commit of branch2. Am i clear please tell me if im not? – biwia Jul 05 '21 at 16:16
  • do you see what i mean? @eftshift0 – biwia Jul 05 '21 at 16:24
  • I think you are talking about an unrelated problem to your question. Your question is about _removing a file from a branch_. How to then put that file into another branch is another problem (just in case, if you removed it from the branch and now it's in untracked files, then it's ok.... go to the other branch and add it there... then when you switch back to the first branch, the file will go away... about as expected). – eftshift0 Jul 05 '21 at 16:26
  • Let us @eftshift0 [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234529/discussion-between-biwia-and-eftshift0). – biwia Jul 05 '21 at 16:32