1

I have two branches.

  1. branch-A
  2. branch-B

Now, in my machine, I have only checked out branch-B. I need a folder from origin/branch-A without creating a branch in my current repo (actually I am doing an automation)

I tried these.

Try 1:
branch-B $ git restore --source branch-A folder-x
Problem: It gives error as I haven't created a branch (branch-A) in my local

Try 2:
branch-B $ git fetch `git remote show` && git checkout `git remote show`/branch-B folder-x
This works, but always trying to get all files (from first).

As in my second try, it is always trying to get all files (may be some reset to HEAD of branch-B ).

Is there any way to do update alone for that folder?

smilyface
  • 5,021
  • 8
  • 41
  • 57
  • 1
    Does this answer your question? [Git: copy all files in a directory from another branch](https://stackoverflow.com/questions/2668886/git-copy-all-files-in-a-directory-from-another-branch) – Mike Slinn Apr 01 '21 at 23:22
  • 1
    Note that *branches* do not matter. What matters are *commits*. You can access a commit using a branch name, but you don't need to *have* a branch name to access a commit. Any name will do, such as `origin/branch-A`, and a raw hash ID will work as well (run `git rev-parse origin/branch-A` to turn the name into the hash ID—but note that `git restore` already does that for you internally). – torek Apr 02 '21 at 00:04

1 Answers1

2

You can use the checkout command,

git checkout <remote-name>/<branch-name> -- <pathspec>

Suppose, in branch-A you have folder-x sitting directly under the root and this folder-x hosts file-x.txt, then the command to pull in just the file-x.txt from branch-A of the remote origin would be,

First perform a fetch,

git fetch,

Then checkout the file,

git checkout origin/branch-A -- folder-x/file-x.txt

If you want to checkout the whole directory,

git checkout origin/branch-A -- folder-x

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • It is the same you can find in my second try. It works, but the problem is always it is trying to get all files (I mean, something like reset) without checking files from Branch B already exists or whether it as HEAD version or not. – smilyface Apr 05 '21 at 08:07
  • 1
    I am not sure why its not working for you. It only checks out the file/directory that I specify in my command. It should never pull in everything. – Asif Kamran Malick Apr 05 '21 at 08:44
  • No, sorry for confusing. Say, in Branch-B, there are directories X,Y,Z. I am doing checkout for X alone. That works as expected. But there are N number of files inside X. And it is always trying to get all files under X eventhough those are same in Branch-A and Branch-B. Hope you. got my point. – smilyface Apr 05 '21 at 09:43
  • Simply, it should update if files under directory X is changed. Otherwise do nothing – smilyface Apr 05 '21 at 09:44