Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove / cut off Git's revision / commit history

Tags:

git

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.

like image 358
Alex Avatar asked Jul 27 '12 12:07

Alex


People also ask

How do I clean up my commit history?

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.

How do I remove a commit from git history?

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.

How do I remove sensitive data from git history?

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.

How do I get rid of committed changes?

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”).


2 Answers

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
like image 135
vergenzt Avatar answered Oct 07 '22 00:10

vergenzt


Github has a good article about removing sensitive data (so the commits you want too):

Remove Sensitive Data from Git

like image 25
Sandro Munda Avatar answered Oct 07 '22 00:10

Sandro Munda