Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:18:07+00:00 2026-06-03T16:18:07+00:00

I am trying to move a single file (call it foo.txt) from one repository

  • 0

I am trying to move a single file (call it foo.txt) from one repository to another (unrelated) repository, preserving its history. ebneter’s question shows how to do this for a subdirectory. taw’s question has some hints and suggestions, but not a step-by-step procedure to follow. jkeating’s question looked promising, but didn’t work for me. Google searches came up empty for this particular use case. What I am looking for is a clear sequence of commands to accomplish this.

The sequence of commands I started to follow was this:

$ git clone source-repo/ source-repo-copy
$ cd source-repo-copy
$ git filter-branch --tree-filter 'test ! "$@" = "foo.txt" && \
  git rm --cached --ignore-unmatch $@ || true' --prune-empty

The logic of my filter command is to git rm all files that are not foo.txt. I added the || true to the command to force it to have a zero return value to satisfy filter-branch.

My intention was to set source-repo-copy as a remote for my target repository (target-repo), and assuming git filter-branch filtered out everything but foo.txt, fetch and merge source-repo-copy into target-repo. Unfortunately, the git filter-branch command seemed to have no effect. It ran with no errors and appeared to grind through the 600+ commits in source-repo, but when it finished, the git log and files in source-repo-copy looked the same. Shouldn’t all files but foo.txt be missing and all commits that didn’t touch it be gone from the log?

At this point I don’t know how to proceed. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:18:09+00:00Added an answer on June 3, 2026 at 4:18 pm

    This worked for me, but with a whole directory.

    As shown here

    ~$ cd neu
    ~/neu$ git filter-branch --subdirectory-filter FooBar HEAD
    ~/neu$ git reset --hard
    ~/neu$ git remote rm origin
    ~/neu$ rm -r .git/refs/original/
    ~/neu$ git reflog expire --expire=now --all
    ~/neu$ git gc --aggressive
    ~/neu$ git prune
    ~/neu$ git remote add origin git://github.com/FooBar/neu.git
    

    EDIT: For a single file:

    Filter the directory first:

     git filter-branch --prune-empty --subdirectory-filter myDirectory -- --all
    

    Filter the single file:

     git filter-branch -f --prune-empty --index-filter "git rm --cached --ignore-unmatch $(git ls-files | grep -v 'keepthisfile.txt')"
    

    Do some cleanup:

     git reset --hard
     git gc --aggressive
     git prune
    

    This should do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to move the data from one table to another based on names
I'm trying to move a control from one parent to another (if this will
Ok so I'm trying to move items from one listbox to another by using
I'm trying to move a MySQL database from a Linux machine to one running
I want to move every single .pdf file inside a folder an its subfolders
I'm trying to move changes from a couple of changesets into one changeset on
I'm trying to move the menu structure from a JMenu into a JMenuBar. The
I'm trying to move a file but I want to ensure that it exists
I'm trying to move a single marker along a route with a function. I
I'm trying to move from stdio to iostream, which is proving very difficult. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.