Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way of doing this is the inverse - remove everything but the file you want to keep.</p> <p>Basically, <strong>make a copy</strong> of the repository, then use <a href="http://git-scm.com/docs/git-filter-branch" rel="noreferrer"><code>git filter-branch</code></a> to remove everything but the file/folders you want to keep.</p> <p>For example, I have a project from which I wish to extract the file <code>tvnamer.py</code> to a new repository:</p> <pre><code>git filter-branch --tree-filter 'for f in *; do if [ $f != "tvnamer.py" ]; then rm -rf $f; fi; done' HEAD </code></pre> <p>That uses <code>git filter-branch --tree-filter</code> to go through each commit, run the command and recommit the resulting directories content. This is extremely destructive (so you should only do this on a copy of your repository!), and can take a while (about 1 minute on a repository with 300 commits and about 20 files)</p> <p>The above command just runs the following shell-script on each revision, which you'd have to modify of course (to make it exclude your sub-directory instead of <code>tvnamer.py</code>):</p> <pre><code>for f in *; do if [ $f != "tvnamer.py" ]; then rm -rf $f; fi; done </code></pre> <p>The biggest obvious problem is it leaves all commit messages, even if they are unrelated to the remaining file. The script <a href="http://github.com/jwiegley/git-scripts/blob/master/git-remove-empty-commits" rel="noreferrer">git-remove-empty-commits</a>, fixes this..</p> <pre><code>git filter-branch --commit-filter 'if [ z$1 = z`git rev-parse $3^{tree}` ]; then skip_commit "$@"; else git commit-tree "$@"; fi' </code></pre> <p>You need to use the <code>-f</code> force argument run <code>filter-branch</code> again with anything in <code>refs/original/</code> (which basically a backup)</p> <p>Of course this will never be perfect, for example if your commit messages mention other files, but it's about as close a git current allows (as far as I'm aware anyway).</p> <p><strong>Again, only ever run this on a copy of your repository!</strong> - but in summary, to remove all files but "thisismyfilename.txt":</p> <pre><code>git filter-branch --tree-filter 'for f in *; do if [ $f != "thisismyfilename.txt" ]; then rm -rf $f; fi; done' HEAD git filter-branch -f --commit-filter 'if [ z$1 = z`git rev-parse $3^{tree}` ]; then skip_commit "$@"; else git commit-tree "$@"; fi' </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload