Git Deleting Commits from History: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:
=Overview=
=Overview=


Commits can be deleted by rebasing, or by resetting:
Commits can be deleted by [[#Deleting_Commits_by_Rebasing|rebasing]] or by [[#Deleting_Commits_by_Resetting|resetting]].


=Deleting Commits by Rebasing=
=Deleting Commits by Rebasing=
Line 29: Line 29:


=Deleting Commits by Resetting=
=Deleting Commits by Resetting=
This method works if the commits to be deleted are at the top of the branch.
This deletes one commit at the top of the branch:
  git reset --hard HEAD~1
  git push --force


=Filter Branch=
=Filter Branch=

Latest revision as of 22:40, 14 October 2019

Internal

Overview

Commits can be deleted by rebasing or by resetting.

Deleting Commits by Rebasing

This procedure is useful when a file containing sensitive information has been committed, and the sensitive information must be removed from repository, including from older commits. Deleting the file from the HEAD of the branch is insufficient, because the file can be accessed in oder commits. If the local branch was pushed remotely, the sensitive information is present on the remote branch as well. This procedure eliminates the commits locally and remotely:

git rebase -i HEAD~3

where 3 is the number of commits to be eliminated.

In the interactively edited file, change "pick" to "d" or "drop" for the commits to be deleted.

Save the file. There should be a message similar to:

Successfully rebased and updated refs/heads/master.

Alternatively, resolve conflicts.

To update the remote branch, do a:

git push --force

Deleting Commits by Resetting

This method works if the commits to be deleted are at the top of the branch.

This deletes one commit at the top of the branch:

 git reset --hard HEAD~1
 git push --force

Filter Branch

Alternative method TODO, not tested.:

http://www-cs-students.stanford.edu/~blynn/gitmagic/ch05.html#_rewriting_history