# git checkout effect on ls-files --others, how to merge partially?

4 messages from 2009-02-06 to 2009-02-07. Participants: Bisani, Alok, Sitaram Chamarty, bisani.
Thread: https://gitlist.dev/t/17608

## Bisani, Alok, 2009-02-06 12:33

Subject: git checkout effect on ls-files --others, how to merge partially?
Message-ID: <C5E2CAEE4A87D24DAB5334F62A72D1F43ADCBD@ELON17P32001A.csfb.cs-group.com>
URL: https://gitlist.dev/e/C5E2CAEE4A87D24DAB5334F62A72D1F43ADCBD%40ELON17P32001A.csfb.cs-group.com

```
Hi,

	I found a feature that was a bit surprising. Basically, a file
exist on master as untracked (git-ls-files --others). It is git-add'ed
on a branch. Now switching back to master, removes the file from
master's working directory as well.

	Whereas, I was expecting it to simply be present as an untracked
file on master as before. This maybe be unreasonable expectation, but
still. To bring the file back on the master branch I had to $ git merge
<branch>. This, however, would pull in all the changes from the branch.
Whereas, I want to limit the merge only to the file.

	Is there a way to pull in changes from a branch but only limited
to a file/files? I ended up doing a manual restore using git-cat-file
<branch>@{0}:/file. And checked in.

    $ git init

    $ echo first > first

    $ echo second > second

    $ git add second

    $ git commit

    $ git ls-files
    first

    $ git ls-files --all

    $ git ls-files --others
    second

    $ git ls-files --others --cached
    first
    second

    $ git checkout -b test_branch HEAD

    $ git branch
      master
    * test_branch

    $ git ls-files --cached
    second

    $ git ls-files --others
    first

    $ git add first

    $ git commit -m "first"

    $ git ls-files --cached
    first
    second

    $ git ls-files --others

    $ git checkout master

    $ git ls-files --others
    # the file "first" which was --others before, has now been removed
upon checking out master

    $ git cat-file blob test_branch@{0}:first > first
    # To bring the file first back on the master branch as an untracked
file

Best regards,
Alok

==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================

```

## Sitaram Chamarty, 2009-02-06 13:28

Subject: Re: git checkout effect on ls-files --others, how to merge partially?
Message-ID: <slrngooent.om3.sitaramc@sitaramc.homelinux.net>
URL: https://gitlist.dev/e/slrngooent.om3.sitaramc%40sitaramc.homelinux.net
In-Reply-To: <C5E2CAEE4A87D24DAB5334F62A72D1F43ADCBD@ELON17P32001A.csfb.cs-group.com>

```
On 2009-02-06, Bisani, Alok <alok.bisani@credit-suisse.com> wrote:

> 	I found a feature that was a bit surprising. Basically, a file
> exist on master as untracked (git-ls-files --others). It is git-add'ed
> on a branch. Now switching back to master, removes the file from
> master's working directory as well.

How would git know that you had such an untracked file in
master?  As far as git knows, at the point you switch back
from branch to master, the file (a) is clean,
exists/committed on branch and (b) does not exist on master.
So it needs to be deleted.

If you really want to do this, you can "git stash" before
switching to the branch and "git stash pop" when you come
back to master.

> 	Is there a way to pull in changes from a branch but only limited
> to a file/files? I ended up doing a manual restore using git-cat-file
><branch>@{0}:/file. And checked in.

git checkout branch -- filename

```

## bisani, 2009-02-06 16:37

Subject: Re: git checkout effect on ls-files --others, how to merge partially?
Message-ID: <loom.20090206T163626-457@post.gmane.org>
URL: https://gitlist.dev/e/loom.20090206T163626-457%40post.gmane.org
In-Reply-To: <slrngooent.om3.sitaramc@sitaramc.homelinux.net>

```
Sitaram Chamarty <sitaramc <at> gmail.com> writes:

> 
> On 2009-02-06, Bisani, Alok <alok.bisani <at> credit-suisse.com> wrote:
>
> > 	Is there a way to pull in changes from a branch but only limited
> > to a file/files? I ended up doing a manual restore using git-cat-file
> ><branch>@{0}:/file. And checked in.
> 
> git checkout branch -- filename
> 
> 

This is exactly what I was looking for, thank you! I wonder why it is the
checkout command which does this, rather than the pull/merge. What is the reason
that we cannot pull/merge a specific file from a branch?

```

## Sitaram Chamarty, 2009-02-07 01:28

Subject: Re: git checkout effect on ls-files --others, how to merge partially?
Message-ID: <slrngopotq.his.sitaramc@sitaramc.homelinux.net>
URL: https://gitlist.dev/e/slrngopotq.his.sitaramc%40sitaramc.homelinux.net
In-Reply-To: <loom.20090206T163626-457@post.gmane.org>

```
On 2009-02-06, Bisani  Alok <alok.bisani@credit-suisse.com> wrote:
>> git checkout branch -- filename

> This is exactly what I was looking for, thank you! I
> wonder why it is the checkout command which does this,
> rather than the pull/merge. What is the reason that we
> cannot pull/merge a specific file from a branch?

Pull is to pull commits between repos, not files between
commits.  (The special case of using "." as the repo is just
that -- a special case; ignore it for now).

Merge is to merge commits within a repo.

Both operate on entire commits, and both *create* a new
commit to record their action (ignore the --no-commit option
in merge for now; that too is a special case IMO, and its
purpose does not detract from the overall sense of what I am
saying anyway)

What you want is an individual file in a commit.  'git show'
is a good way of getting that, as is 'git cat-file blob'
(by the way I prefer 'git cat-file -p').  For blob objects,
all of them seem to do the same thing.

But all of them are geared to putting the file onto stdout,
and you deal with it how you want.  They're all basically
just 'cat', with a couple of extra whiskers perhaps :-)

'git checkout' is meant to 'checkout a branch or paths to
the working tree' -- as it says on the tin, which is what
you want.

Sita

```
