0

I've been here:

And also tried to make some questions in chatGPT.

Those solutions shows how to get a new branch with the history of files that are stored inside a folder. They use git filter-branch and git subtree commands.

But, I did not get the solution of how to create a new git branch containing full history of only one single file.

Example: Supose I have a subfolder in my project: ./src/js/mains/ containing many files, including one named test.js.

What is the git command to get a branch containing full history of only that one single file?

Is that possible?

I'm using git for windows: git version 2.39.2.windows.1

Diogo
  • 590
  • 6
  • 23

1 Answers1

2

The easiest way to rewrite an entire repo with the history of a single file (or files) is probably by using git-filter-repo. One you have it installed, the steps are simply:

  1. Make a new clone of the repo.
  2. git filter-repo --path src/js/mains/test.js

One of the amazing things about git-filter-repo is how fast it is, compared to the older git-filter-branch.

Note that even though git-filter-repo is not actually part of Git and must be installed separately, even the official Git docs recommend using it instead of git-filter-branch:

git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance). These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended. Please use an alternative history filtering tool such as git filter-repo.

TTT
  • 22,611
  • 8
  • 63
  • 69