0

I committed hundreds of files within a core folder accidentally during my last git commit.

I do not want to lose these files, but I don't want them in my repo. How do I undo the last git commit whilst also retaining the files?

This is a local build, prior to git pushing, so I don't care about history being lost and I would prefer to do this in the simplest possible way, i.e. if I can avoid making new branches or detaching from head, etc. then that would be preferred.

Auxiliary Joel
  • 173
  • 1
  • 13
  • 1
    Possible duplicate of [How can I unstage my files again after making a local commit?](https://stackoverflow.com/questions/6682740/how-can-i-unstage-my-files-again-after-making-a-local-commit) – phd Jul 28 '19 at 12:37
  • https://stackoverflow.com/search?q=%5Bgit%5D+undo+most+recent+commit+preserving+files – phd Jul 28 '19 at 12:38

2 Answers2

0

The first thing you need to do is described in this stackoverflow question: How do I undo the most recent local commits in Git?

Which is to undo the last commit which will have them saved, but not committed on your machine

$ git reset HEAD~

Then to remove the files that you don't want from git you should use the git rm command (I'm assuming you want the -r (recursive) option to remove the whole directory

$ git rm -r <dir path>

Afterwards you should have all the changes from your last commit, in git, but it shouldn't track the ones you added by accident!

I would also suggest adding this core folder to your .gitignore file

ngood97
  • 513
  • 5
  • 16
  • Thanks for your reply. I read through your answer and the link you shared and I'd like to clarify some things: Does "git reset HEAD~" only reset (i.e. remove) the LAST commit, or ALL commits? And if it only resets (i.e. removes) the LAST commit, then what is the difference between "git reset HEAD-1" and "git reset HEAD~"? Also, with the answer by Mark Amery from the linked thread, do his 5 steps keep everything on the existing/only/master branch, meaning I can then "git push origin master" to my github repo or do I need to do anything else after step 5 commit to return to master branch? – Auxiliary Joel Jul 28 '19 at 06:20
  • "git reset HEAD~" should just reset the last commit, and "HEAD-1" isn't valid as far as I know, but you could use "HEAD~1" and that would be the same. (check this out for a bit more info: https://stackoverflow.com/questions/26785118/head-vs-head-vs-head-also-known-as-tilde-vs-caret-vs-at-sign/26785200") As for the 5 steps they should get you to the point where the code is committed onto your local master branch so at that point you could "git push origin master" like you said with no issues :) – ngood97 Jul 28 '19 at 06:28
  • I seem to be having trouble with this. I followed the steps but it seems I have only uncommitted them, but they are still in my add list? for example I have subsequently added this line to my .gitignore file: core/ (I want to igore the core directory, and all it's files and subdirectories). But when I hit "git status" I still keep getting shown the entirety of all the core files – Auxiliary Joel Jul 28 '19 at 06:34
  • The issue here is that since you added them, git is still tracking those files so I would use the `git rm` command to remove them from the tracked files. It works the same way as git add, but just removes them from the tracked files. – ngood97 Jul 28 '19 at 06:35
  • I have already ran that command, I did this: "git rm --cached -r core" (I understand that would remove my core directory and subdirectories from the git add, whilst not deleting or reverting the actual content of the files?) but now when running "git status" the files still show up, except now they are under the heading "Changes to be committed: (use "git reset HEAD ..." to unstage)" – Auxiliary Joel Jul 28 '19 at 06:38
  • Oh I see, you want to run that command without the `--cached` option since that removes the files "only from the index". So the command would be `git rm -r core`. Don't worry, this won't change the actual files on your disk, but just whether git is tracking them. – ngood97 Jul 28 '19 at 06:43
  • I ran that command: 'git rm -r core' but it returns this error: 'fatal: pathspec 'core' did not match any files'. Core is a folder, not a file - so does the command understand I'm talking about a folder? or what other reasons would this not work (I have double-checked and the terminal is running from correct parent directory and the core folder does exist). – Auxiliary Joel Jul 28 '19 at 22:49
  • I ended up running this: 'git reset HEAD core/' which gave me this list 'Unstaged changes after reset:' (showing all my core folder/subfolder files). Then when I ran 'git status' all my "core"content was under the list heading "Changes not staged for commit:" which sounds good. I then added 'core' to my .gitignore. and ran 'git status' again, but the files kept appearing in the "not staged for commit" list. I'd rather git does not even see them but I can't figure out how to do that? – Auxiliary Joel Jul 28 '19 at 23:24
  • Did you make a typo in the path to the core folder when you ran `git rm -r core`? – ngood97 Jul 29 '19 at 00:10
  • no, in fact when I ran that command again (git rm -r core) it added the files BACK into the "Changes to be committed" list, so I then re-ran the git reset HEAD core/ to get them out of there, but now I'm trying to get git to ignore them/untrack them and I can't do it? – Auxiliary Joel Jul 29 '19 at 00:29
-1

below command :-

git reset --soft HEAD^^

  • Thanks ER Softwiil Fam, so that one command will take me back to the previous commit 1 prior to my last one and my files won't be effected? Then next time I git add / git commit it will only commit new things I've added and forget about these accidental commits? – Auxiliary Joel Jul 28 '19 at 05:21
  • correct. use git reset --soft HEAD^ for linux and git reset --soft HEAD^^ fow windows – ER Softwiil Fam Jul 29 '19 at 07:29