1

I added some new files to a local copy of a Github repo and accidentally ran git add * and git commit, but not git push.

I think the new files should only be on my local copy of the repo. How do I safely remove those files from the next git push command. I want to keep the files locally, but not include them in the next push.

What is the best way to remove them from git, but leave them in place on my hard drive?

Hugo y
  • 1,421
  • 10
  • 20
Donald S
  • 1,583
  • 1
  • 10
  • 26

2 Answers2

2

You can run the following:

git reset --soft HEAD~1

--soft ensures that those files aren't deleted from your hard drive.

~1 refers to the latest commit.
You can also do the same for the last n commits by using HEAD~n

If you don't want those files to ever be pushed to your remote repository, consider creating a .gitignore and adding those files to it.

You can read more about .gitignore here.

  • 2
    This is mostly correct asuming the most recent commit (and *only* the most recent commit) contains the files. If the files are particularly sensitive, then it is worth noting that this does *not* remove them from your local repo. They wouldn't be reachable from any ref, and you generally wouldn't expect git to transmit them during a push, but I'm not aware of anything in the docs that guarantees it won't; so for example if there are passwords involved, it might be worth some additional steps to properly sanitize the local repo. Those procedures are covered in answers to existing questions – Mark Adelsberger Jul 15 '20 at 16:32
  • what does the ~1 do in the command above? – Donald S Jul 16 '20 at 01:04
  • @DonaldS I have updated the answer, you can have a look –  Jul 16 '20 at 04:39
-1

I think git reset --soft HEAD~1 should do the trick.

BOUNCE
  • 147
  • 6