{"thread":{"id":"31558","subject":"Remove all files except a few files, using filter-branch","startedAt":"2012-09-16T16:28:31Z","lastAt":"2012-09-18T15:25:23Z","messageCount":5,"participants":["Yi, EungJun","Andreas Schwab","Johannes Sixt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"199171","messageId":"CAFT+Tg_ipyPgNQxx8XsXySzmA3YM3mKe1gB5GyTNqN++gdphnQ@mail.gmail.com","threadId":"31558","inReplyTo":null,"subject":"Remove all files except a few files, using filter-branch","fromName":"Yi, EungJun","fromEmail":"semtlenori@gmail.com","sentAt":"2012-09-16T16:28:31Z","receivedAt":"2012-09-16T16:28:31Z","isPatch":false,"sender":{"key":"semtlenori@gmail.com","avatar":"https://gravatar.com/avatar/8363435d2badb3450df0dd7c4ec2113f6e1d62c44dd3cf7dfe83c5a389b8a9bf?d=mp&s=160"},"body":"Hi, all.\n\nI want to remove all files except a few files, in the history of my\ngit repository.\n\nI tried to do that as follows:\n\n    git filter-branch --index-filter \"git rm --cached --ignore-unmatch\n$(git ls-files | grep -v '^filename$' | tr '\\n' ' ')\"\n\nBut this does not work well if there is a file whose name is not\nencoded in us-ascii or includes parenthesis. git-filter-branch is\ngreat to remove some files in my repository, but not good enough to\nremove all except only a few.\n\nDoes anyone know the better way?\n"},{"id":"199173","messageId":"m24nmxkibw.fsf@igel.home","threadId":"31558","inReplyTo":"CAFT+Tg_ipyPgNQxx8XsXySzmA3YM3mKe1gB5GyTNqN++gdphnQ@mail.gmail.com","subject":"Re: Remove all files except a few files, using filter-branch","fromName":"Andreas Schwab","fromEmail":"schwab@linux-m68k.org","sentAt":"2012-09-16T17:06:59Z","receivedAt":"2012-09-16T17:06:59Z","isPatch":false,"sender":{"key":"schwab@linux-m68k.org","avatar":"https://avatars.githubusercontent.com/u/2175493?v=4"},"body":"\"Yi, EungJun\" <semtlenori@gmail.com> writes:\n\n> Hi, all.\n>\n> I want to remove all files except a few files, in the history of my\n> git repository.\n>\n> I tried to do that as follows:\n>\n>     git filter-branch --index-filter \"git rm --cached --ignore-unmatch\n> $(git ls-files | grep -v '^filename$' | tr '\\n' ' ')\"\n\nTry instead first removing all files, then restoring the files you want\nto keep.\n\n--index-filter \"git rm --cached -qr -- . && git reset -q -- filename\"\n\nAndreas.\n\n-- \nAndreas Schwab, schwab@linux-m68k.org\nGPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5\n\"And now for something completely different.\"\n"},{"id":"199372","messageId":"CAFT+Tg-4q0mzD6rdY74WBP-0OM_SaZ_wkCAn+Gz2_eDte2a4Nw@mail.gmail.com","threadId":"31558","inReplyTo":"m24nmxkibw.fsf@igel.home","subject":"Re: Remove all files except a few files, using filter-branch","fromName":"Yi, EungJun","fromEmail":"semtlenori@gmail.com","sentAt":"2012-09-18T15:01:27Z","receivedAt":"2012-09-18T15:01:27Z","isPatch":false,"sender":{"key":"semtlenori@gmail.com","avatar":"https://gravatar.com/avatar/8363435d2badb3450df0dd7c4ec2113f6e1d62c44dd3cf7dfe83c5a389b8a9bf?d=mp&s=160"},"body":"> --index-filter \"git rm --cached -qr -- . && git reset -q -- filename\"\n\nHmm... I tried as you said, but it seems to lose history.\n\nIn the below example, after rewriting there must be two commits for\n'b' file but only one exists.\n\n~$ git init filter-branch2\nInitialized empty Git repository in\n/home/nori/mysrc/test/git/filter-branch2/.git/\n~/filter-branch2$ cd filter-branch2\n~/filter-branch2$ echo 1 > a\n~/filter-branch2$ echo 1 > b\n~/filter-branch2$ git add a b\n~/filter-branch2$ git commit -m 'first'\n[master (root-commit) ae2b5fd] first\n 2 files changed, 2 insertions(+)\n create mode 100644 a\n create mode 100644 b\n~/filter-branch2$ echo 2 >> b\n~/filter-branch2$ git add b\n~/filter-branch2$ git commit -m 'second'\n[master a32b84e] second\n 1 file changed, 1 insertion(+)\n~/filter-branch2$ git filter-branch --index-filter \"git rm --cached\n-qr -- . && git reset -q -- b\"\nRewrite a32b84ed7cec5686e43a47195dfa8114f83619f3 (2/2)\nRef 'refs/heads/master' was rewritten\n~/filter-branch2$ git log -- b\ncommit 19611f9eaf412232e237afcc059d0324a862062f\nAuthor: Yi EungJun <semtlenori@gmail.com>\nDate:   Tue Sep 18 23:51:53 2012 +0900\n\n    first\n\nAm I doing something wrong?\n\nOn Mon, Sep 17, 2012 at 2:06 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:\n> \"Yi, EungJun\" <semtlenori@gmail.com> writes:\n>\n>> Hi, all.\n>>\n>> I want to remove all files except a few files, in the history of my\n>> git repository.\n>>\n>> I tried to do that as follows:\n>>\n>>     git filter-branch --index-filter \"git rm --cached --ignore-unmatch\n>> $(git ls-files | grep -v '^filename$' | tr '\\n' ' ')\"\n>\n> Try instead first removing all files, then restoring the files you want\n> to keep.\n>\n> --index-filter \"git rm --cached -qr -- . && git reset -q -- filename\"\n>\n> Andreas.\n>\n> --\n> Andreas Schwab, schwab@linux-m68k.org\n> GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5\n> \"And now for something completely different.\"\n"},{"id":"199373","messageId":"50588ED7.4090501@viscovery.net","threadId":"31558","inReplyTo":"CAFT+Tg-4q0mzD6rdY74WBP-0OM_SaZ_wkCAn+Gz2_eDte2a4Nw@mail.gmail.com","subject":"Re: Remove all files except a few files, using filter-branch","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2012-09-18T15:10:15Z","receivedAt":"2012-09-18T15:10:15Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 9/18/2012 17:01, schrieb Yi, EungJun:\n>> --index-filter \"git rm --cached -qr -- . && git reset -q -- filename\"\n> \n> Hmm... I tried as you said, but it seems to lose history.\n\nI think it should be '... && git reset -q $GIT_COMMIT -- filename'\n\n-- Hannes\n"},{"id":"199374","messageId":"CAFT+Tg_UbG8C9o2E5P1E2a9GXgLhn2fmE8sMA4un0Kai2HrOeg@mail.gmail.com","threadId":"31558","inReplyTo":"50588ED7.4090501@viscovery.net","subject":"Re: Remove all files except a few files, using filter-branch","fromName":"Yi, EungJun","fromEmail":"semtlenori@gmail.com","sentAt":"2012-09-18T15:25:23Z","receivedAt":"2012-09-18T15:25:23Z","isPatch":false,"sender":{"key":"semtlenori@gmail.com","avatar":"https://gravatar.com/avatar/8363435d2badb3450df0dd7c4ec2113f6e1d62c44dd3cf7dfe83c5a389b8a9bf?d=mp&s=160"},"body":"> I think it should be '... && git reset -q $GIT_COMMIT -- filename'\n\nIt works! Thanks to Hannes and Andreas!\n\nOn Wed, Sep 19, 2012 at 12:10 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:\n> Am 9/18/2012 17:01, schrieb Yi, EungJun:\n>>> --index-filter \"git rm --cached -qr -- . && git reset -q -- filename\"\n>>\n>> Hmm... I tried as you said, but it seems to lose history.\n>\n> I think it should be '... && git reset -q $GIT_COMMIT -- filename'\n>\n> -- Hannes\n"}]}