Say you cloned a repository, then you created a new branch and did several commits on this branch ( without creating new branches or pushing to any remotes ). In some of these commits you introduced (committed) a file fileA
by a mistake. Now, how can you remove that file from the history, but not from the local file system (i.e. keep the file as untracked)? Also assume that the name of the parent branch is not known (in order to completely automate the process).
Delete a file from from all commits up to first branch point but not delete it from local file system
40 views Asked by Håkon Hægland At
1
We here use git filter-branch to remove the file from the index. It is important that we only use
git filter-branch
for the commits up to the first branch point; if not, we may not be able to merge the branch back into the original parent, and we could also accidentally delete a file with the same name in the parent branch.A difficulty arises if we assume the name of the parent branch to be unknown. However, this can be resolved using
git branch --contains
: