1

I accidentally pushed a file to my Github repository, and I am trying to understand where it is.

I have read many posts in here, but none of them had the solution, so I wanted to ask.

My steps were:

git status
git add normal_file.txt
git add sensitive_content_file.txt
git rm --cached sensitive_content_file.txt
#It was the time that I realized I have added a wrong file and need to remove it.
git commit -m "new changes"
git push

I saw that it pushed the "normal_file.txt " into my repository however I have not seen the "sensitive_content_file.txt" in my repository, so I assume it is not being pushed? So that all is okay?

I have tried to clean cache but now my repository complains as:

" Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively."

However, I did not do any changes to itself.

How can I clear every connection to this file with I tried to remove its caches?

Mouloud85
  • 3,826
  • 5
  • 22
  • 42
bapors
  • 887
  • 9
  • 26

1 Answers1

1

git rm --cached sensitive_content_file.txt before the commit was enough to not pushing it to GitHub.

But now, it would be best to add that file to your .gitignore, in order to be sure to not add it again by mistake:

echo sensitive_content_file.txt>>.gitignore
git add .gitignore
git commit -m "Ingore sensitive_content_file.txt"
git push

If you did any rebase/commit --amend, that would change the local history compared to what was pushed.
If you have not done any local modification, a git reset --hard origin/master would reset master, then you can modify the .gitignore and push.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your answer. I was worried as I did not "amend" it and just said "commit" . I have removed the whole commit and cleared the history for incase. Would 'git reset --hard origin/master' push everything in local repsoritory to remote one? – bapors Feb 15 '18 at 09:06
  • @bapors `git reset --hard origin/master` only reset the local `master` to what was fetched. It does not push anything. Then you can make new commits on top of that reset `master`, and the push will be straightforward. – VonC Feb 15 '18 at 09:08
  • Thank you. It reseted it so there is no more warning message. One other question, I can still see the commit in my recent activity, so the commit is still there. How can I get rid of it 100%? – bapors Feb 15 '18 at 09:16
  • @bapors The commit does not included your sensitive file, since you removed it before committing, so you can leave it there. – VonC Feb 15 '18 at 09:17
  • But would it be possible to do it? – bapors Feb 15 '18 at 10:07
  • @bapors Yes: https://www.clock.co.uk/insight/deleting-a-git-commit or https://stackoverflow.com/a/17025745/6309. But you will have to do a git push --force (or rather force-with-leave: https://developer.atlassian.com/blog/2015/04/force-with-lease/) – VonC Feb 15 '18 at 10:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165208/discussion-between-bapors-and-vonc). – bapors Feb 15 '18 at 10:19
  • @bapors A git push --force will override the remote history (which is displaying the bad commit) – VonC Feb 15 '18 at 11:42
  • I did it.. but I can still see the unwanted_SHA in commits/unwanted_SHA URL.. it did not remove it – bapors Feb 15 '18 at 12:58
  • @bapors That is expected: https://stackoverflow.com/a/48020000/6309. For truly remove that commit, you need to contact GitHub support: https://github.com/contact – VonC Feb 15 '18 at 13:01
  • @bapors Same answer, but you need to contact BitBucket support then ;) – VonC Feb 15 '18 at 13:02
  • And this is the only way? – bapors Feb 15 '18 at 13:03
  • @bapors Yes, because of the nature of a Git repo, which keeps every unreferenced commit in its reflog for 90 days by default. The support will be able to delete it in no time. – VonC Feb 15 '18 at 13:03
  • Thank you very much @VonC! You helped me greatly. I have contacted and waiting for their reply – bapors Feb 15 '18 at 13:31