Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove history of a file from just a single branch, not the entire repo

Tags:

git

I have a topic branch (off master) has that lots of changes. Throughout the history of this topic branch there's been numerous changes to a certain file. How can remove all changes to this file throughout the history of my topic branch?

Additional notes:

  • Note that the file existed before the topic branch was created. I don't want to delete the file per se.
  • I have a couple of solutions, but so far they seem tedious. I'll post them as separate answers.
  • Can git filter-branch somehow be used for this?
  • My topic branch contains around...say...60 commits.
like image 742
Ztyx Avatar asked Sep 25 '13 09:09

Ztyx


People also ask

How do I remove a file from git 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. The git filter-repo tool and the BFG Repo-Cleaner rewrite your repository's history, which changes the SHAs for existing commits that you alter and any dependent commits.

How do you remove all changes to a file in a branch in git?

The git reset –hard command will revert uncommitted changes that exist in files that have been added to the index, whether those files are newly created files, or files that were added to the index in the past and have been edited since the last commit.

How do I remove a file from git without removing it from file system?

Using the git rm –cached Command We've mentioned that git rm FILE will remove files from the index and local working tree by default. However, the git rm command provides the –cached option to allow us only to remove files from the repository's index and keep the local file untouched.

Does git rm remove history?

No, git rm will only remove the file from the working directory and add that removal into the index. So only future commits are affected. All previous commits stay the same and the history will actually show when you removed the file from the repository.


1 Answers

Using git filter-branch:

$ git checkout topic-branch
$ git filter-branch --index-filter 'git checkout master -- myfile' master..HEAD
like image 110
Ztyx Avatar answered Oct 03 '22 08:10

Ztyx