This question is about rewriting git history. Six months ago I decided to make changes over an existing subfolder of my repository, and now I’ve changed my mind and want to undo that and all the associated history. Note, I want changes outside that subfolder should be left as is.
So, specifically, I am trying to remove the history of all the changes made within one of the subfolders of the repo, from a certain date onwards.
The end result I am hoping for should look like no edits/adds/deletes were ever made in the subfolder after that date.
Looking at the the answer to Remove file from git repository (history), and also http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository, the closest I’ve got is this:
git filter-branch --index-filter 'git rm -r -f --cached --ignore-unmatch TheFolderToRemove' --prune-empty --all xxxxxx..HEAD
git reset --hard
git remote rm origin
rm -r .git/refs/original/
git reflog expire --expire=now --all
git gc --aggressive
git prune
This removes all the files in the subfolder.
Can someone please show me how to modify this so that it just removes the changes applied within the folder? Many thanks
Running the
filter-branchwithgit rm --cachedtells git to remove the directory and stage that removal! It only did what you told it to do.If you still have the original objects: (i.e. you didn’t actually run
gcandprune)You want to use
git resetto unstage changes to that directory from the index, not remove the directory itself:where
xxxxxxis the commit with state you want the folder to be in.If you do not still have the original objects: (i.e. you pruned the old tree from your repository)
Checkout the commit in which the folder is shown as removed (I believe it should be the commit after
xxxxxx), undo the removal, amend the commit, and rebase the rest of history on top:Then, if
masteris your only branch: