{"thread":{"id":"17608","subject":"git checkout effect on ls-files --others, how to merge partially?","startedAt":"2009-02-06T12:33:07Z","lastAt":"2009-02-07T01:28:26Z","messageCount":4,"participants":["Bisani, Alok","Sitaram Chamarty","bisani"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"103464","messageId":"C5E2CAEE4A87D24DAB5334F62A72D1F43ADCBD@ELON17P32001A.csfb.cs-group.com","threadId":"17608","inReplyTo":null,"subject":"git checkout effect on ls-files --others, how to merge partially?","fromName":"Bisani, Alok","fromEmail":"alok.bisani@credit-suisse.com","sentAt":null,"receivedAt":"2009-02-06T12:33:07Z","isPatch":false,"sender":{"key":"alok.bisani@credit-suisse.com","avatar":null},"body":"Hi,\n\n\tI found a feature that was a bit surprising. Basically, a file\nexist on master as untracked (git-ls-files --others). It is git-add'ed\non a branch. Now switching back to master, removes the file from\nmaster's working directory as well.\n\n\tWhereas, I was expecting it to simply be present as an untracked\nfile on master as before. This maybe be unreasonable expectation, but\nstill. To bring the file back on the master branch I had to $ git merge\n<branch>. This, however, would pull in all the changes from the branch.\nWhereas, I want to limit the merge only to the file.\n\n\tIs there a way to pull in changes from a branch but only limited\nto a file/files? I ended up doing a manual restore using git-cat-file\n<branch>@{0}:/file. And checked in.\n\n    $ git init\n\n    $ echo first > first\n\n    $ echo second > second\n\n    $ git add second\n\n    $ git commit\n\n    $ git ls-files\n    first\n\n    $ git ls-files --all\n\n    $ git ls-files --others\n    second\n\n    $ git ls-files --others --cached\n    first\n    second\n\n    $ git checkout -b test_branch HEAD\n\n    $ git branch\n      master\n    * test_branch\n\n    $ git ls-files --cached\n    second\n\n    $ git ls-files --others\n    first\n\n    $ git add first\n\n    $ git commit -m \"first\"\n\n    $ git ls-files --cached\n    first\n    second\n\n    $ git ls-files --others\n\n    $ git checkout master\n\n    $ git ls-files --others\n    # the file \"first\" which was --others before, has now been removed\nupon checking out master\n\n    $ git cat-file blob test_branch@{0}:first > first\n    # To bring the file first back on the master branch as an untracked\nfile\n\nBest regards,\nAlok\n\n==============================================================================\nPlease access the attached hyperlink for an important electronic communications disclaimer: \n\nhttp://www.credit-suisse.com/legal/en/disclaimer_email_ib.html\n==============================================================================\n"},{"id":"103473","messageId":"slrngooent.om3.sitaramc@sitaramc.homelinux.net","threadId":"17608","inReplyTo":"C5E2CAEE4A87D24DAB5334F62A72D1F43ADCBD@ELON17P32001A.csfb.cs-group.com","subject":"Re: git checkout effect on ls-files --others, how to merge partially?","fromName":"Sitaram Chamarty","fromEmail":"sitaramc@gmail.com","sentAt":"2009-02-06T13:28:29Z","receivedAt":"2009-02-06T13:28:29Z","isPatch":false,"sender":{"key":"sitaramc@gmail.com","avatar":"https://avatars.githubusercontent.com/u/43316?v=4"},"body":"On 2009-02-06, Bisani, Alok <alok.bisani@credit-suisse.com> wrote:\n\n> \tI found a feature that was a bit surprising. Basically, a file\n> exist on master as untracked (git-ls-files --others). It is git-add'ed\n> on a branch. Now switching back to master, removes the file from\n> master's working directory as well.\n\nHow would git know that you had such an untracked file in\nmaster?  As far as git knows, at the point you switch back\nfrom branch to master, the file (a) is clean,\nexists/committed on branch and (b) does not exist on master.\nSo it needs to be deleted.\n\nIf you really want to do this, you can \"git stash\" before\nswitching to the branch and \"git stash pop\" when you come\nback to master.\n\n> \tIs there a way to pull in changes from a branch but only limited\n> to a file/files? I ended up doing a manual restore using git-cat-file\n><branch>@{0}:/file. And checked in.\n\ngit checkout branch -- filename\n"},{"id":"103531","messageId":"loom.20090206T163626-457@post.gmane.org","threadId":"17608","inReplyTo":"slrngooent.om3.sitaramc@sitaramc.homelinux.net","subject":"Re: git checkout effect on ls-files --others, how to merge partially?","fromName":"","fromEmail":"bisani","sentAt":"2009-02-06T16:37:38Z","receivedAt":"2009-02-06T16:37:38Z","isPatch":false,"sender":{"key":"bisani","avatar":null},"body":"Sitaram Chamarty <sitaramc <at> gmail.com> writes:\n\n> \n> On 2009-02-06, Bisani, Alok <alok.bisani <at> credit-suisse.com> wrote:\n>\n> > \tIs there a way to pull in changes from a branch but only limited\n> > to a file/files? I ended up doing a manual restore using git-cat-file\n> ><branch>@{0}:/file. And checked in.\n> \n> git checkout branch -- filename\n> \n> \n\nThis is exactly what I was looking for, thank you! I wonder why it is the\ncheckout command which does this, rather than the pull/merge. What is the reason\nthat we cannot pull/merge a specific file from a branch?\n"},{"id":"103575","messageId":"slrngopotq.his.sitaramc@sitaramc.homelinux.net","threadId":"17608","inReplyTo":"loom.20090206T163626-457@post.gmane.org","subject":"Re: git checkout effect on ls-files --others, how to merge partially?","fromName":"Sitaram Chamarty","fromEmail":"sitaramc@gmail.com","sentAt":"2009-02-07T01:28:26Z","receivedAt":"2009-02-07T01:28:26Z","isPatch":false,"sender":{"key":"sitaramc@gmail.com","avatar":"https://avatars.githubusercontent.com/u/43316?v=4"},"body":"On 2009-02-06, Bisani  Alok <alok.bisani@credit-suisse.com> wrote:\n>> git checkout branch -- filename\n\n> This is exactly what I was looking for, thank you! I\n> wonder why it is the checkout command which does this,\n> rather than the pull/merge. What is the reason that we\n> cannot pull/merge a specific file from a branch?\n\nPull is to pull commits between repos, not files between\ncommits.  (The special case of using \".\" as the repo is just\nthat -- a special case; ignore it for now).\n\nMerge is to merge commits within a repo.\n\nBoth operate on entire commits, and both *create* a new\ncommit to record their action (ignore the --no-commit option\nin merge for now; that too is a special case IMO, and its\npurpose does not detract from the overall sense of what I am\nsaying anyway)\n\nWhat you want is an individual file in a commit.  'git show'\nis a good way of getting that, as is 'git cat-file blob'\n(by the way I prefer 'git cat-file -p').  For blob objects,\nall of them seem to do the same thing.\n\nBut all of them are geared to putting the file onto stdout,\nand you deal with it how you want.  They're all basically\njust 'cat', with a couple of extra whiskers perhaps :-)\n\n'git checkout' is meant to 'checkout a branch or paths to\nthe working tree' -- as it says on the tin, which is what\nyou want.\n\nSita\n"}]}