I have a project that contains traces to another project of myself that I've used as a kind of a template project. Now I want to remove these traces entirely from the repository. Basically I want to cut off the old junk commits. So I have
A -- B -- C -- D -- E -- F
and want to get something like
D -- E -- F
with A -- B -- C
being completely removed from the repository.
Steps to get to a clean commit history:understand rebase and replace pulling remote changes with rebase to remove merge commits on your working branch. use fast-forward or squash merging option when adding your changes to the target branch. use atomic commits — learn how to amend, squash or restructure your commits.
All you need to do is typing "drop" at the beginning of each commit you want to delete. Be careful while using the git rebase command, as it may cause sudden problems. So, it is more recommended to use the git revert command.
If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history. To entirely remove unwanted files from a repository's history you can use either the git filter-repo tool or the BFG Repo-Cleaner open source tool.
In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).
Assuming master
is at commit F
:
# create a new branch with D's content
$ git checkout --orphan temp <d-sha1>
$ git commit
# rebase everything else onto the temp branch
$ git rebase --onto temp <d-sha1> master
# clean up
$ git checkout master
$ git branch -d temp
If you want to completely remove the old loose objects (A
, B
, & C
), first make sure you have exactly what you want. This cannot be undone. Once you have confirmed it's what you want, run:
$ git reflog expire --expire=now --all
$ git gc --prune=now
Github has a good article about removing sensitive data (so the commits you want too):
Remove Sensitive Data from Git
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With