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
==============================================================================
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.
On 2009-02-06, Bisani, Alok <alok.bisani <at> credit-suisse.com> wrote:
quoted
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?
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