0

I have a situation where I committed to a branch B1. In the commit I have several files. Now I want those files to be copied to a Branch B2.

Actually I am using the following command for each file in the commit on my current branch B2:

 $ git checkout B1 path/to/file1
 $ git checkout B1 path/to/file2
 ......
 $ git checkout B1 path/to/fileN

I guess that there should be a syntax that will allow me to directly checkout the specific commit from branch B1 to branch B2. I tried several solutions but they are rewriting the whole branch which I do not want, nor history of the all files. I only want the state of the file in the particular commit. Overwriting is not a problem.

How to achieve this ?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
vanessen
  • 1,125
  • 1
  • 12
  • 19
  • 1
    Do you want to get "the complete repo as it is in B1" ? or "only the files modified in the last commit on B1" ? – LeGEC May 19 '21 at 20:29

2 Answers2

1
git checkout B1 .

You have to specify a path to get what you want, so . is a path that specifies the current directory.

user1686
  • 13,155
  • 2
  • 35
  • 54
  • I forget to mention that all the files are not in the same directory, and do not necessarily have same extensions – vanessen May 19 '21 at 20:13
1

To get the list of files modified in commit xyz, run :

# get only the names for the diff between xyz's parent and xyz :
git diff --name-only xyz^ xyz

If you want to checkout these files only :

git checkout B1 -- $(git diff --name-only xyz^ xyz)

# for example : the files modified in the last commit on B2 :
git checkout B1 -- $(git diff --name-only B2^ B2)
LeGEC
  • 46,477
  • 5
  • 57
  • 104