Git reset delete history?

I accidentally did something that could be sensitive information in git (only locally), and I want to remove it from the git history in a simple way. Will it

git reset --soft HEAD~1 

and then turn off sensitive information and add to gitignore enough to completely remove it from git history?

+5
source share
3 answers

No, soft reset is not enough. This will cause the file to be listed in your index (where you put the files you want to commit). This means that git is still tracking the file.

You will need to do a mixed reset, which will also disable these files. As Rene noted, it is also recommended that you delete the file or add it to your .gitignore so that you do not accidentally allow it again.

This is enough, so confidential information will not be transmitted to the remote server on git push .

However, the information is still in your local repository. If you've ever β€œlost” a commit by accidentally dropping too far, git reflog is a very useful tool.

Now, to clear everything, all commits inaccessible through a branch or tag:

 git reflog expire --expire=1.minute --all git prune git gc 
  • Deletes all entries older than 1 minute from the log. The end is not deleted if there is any reference to it. Such a link may come from another commit, branch, tag, and also reflog.
  • Deletes all commits that are not available.
  • Performs a number of homework. See the documentation for more information.

reflog expire and prune are destructive operations. I recommend that you run these commands first with the --dry-run argument to see what exactly is being deleted.

+4
source

No need for --soft : git reset HEAD~1 will lock the current commit files, resetting the index in addition to moving the HEAD.

Then a .gitignore will be enough to ignore this file.

Do you mean that I have to do git reset --soft HEAD~1 and then git reset HEAD <file> ?

No, you need to delete all the commits to make sure that the story will not contain your confidential file.

 git reset HEAD~1 echo sensitiveFile >> .gitignore git add .gitignore git add -A . git commit -m "Redo commit, without sensitiveFile" 
+2
source

git reset --soft HEAD~1 only resets HEAD to the previous commit until the index and working directory are affected.

This means that the index and working directory still reflects the state containing confidential information. Just remove these files from the git reset HEAD <file>... index and commit again.

After deleting confidential data files from the index, they will remain in the working directory. You might also want to remove them from the working directory or add them to .gitignore .

EDIT

Do you mean that I have to do git reset --soft HEAD ~ 1 first and then git reset HEAD?

Yes. Usually you create git add .. files and then commit them. If you do git reset --soft HEAD~1 , your repository reflects the state before this commit. You can see this if you run git status . This way you can change the index and commit again.

Also, does data delete 100% from git history?

No, not completely. If the commit is no longer specified, git will delete it after 90 days (the default is see git gc ). If you want to remove it immediately, you must make sure that it is no longer referenced. Therefore, you should remove it from any reflog containing it, for example. git reflog --delete HEAD@ {3} . Usually it will be contained in the HEAD block and your branch. Also make sure that it is not contained in any branch, for example. if commit, if A do a git branch -a --contains A The output must be empty.

You can then collect the garbage using git gc --prune=now . Verify that the commit has been removed using git log -n1 COMMIT-ID . It should output fatal: bad object .

For more information on deleting files and directories from git, see my blog https://www.link-intersystems.com/blog/2014/07/17/remove-directories-and-files-permanently-from-git/ .

+2
source

All Articles