1

I am studying a book about TDD, and in an appendix, they ask us to use code which is given in a GitHub branch. I know I can make a new folder, do git init, and use git pull, but it would be easier to have a separate branch and have a branch from GitHub there. Could you advise me on how I can take code and history from a branch in GitHub, and make that the code and history of a new branch in my local repo? I have created the branch using git checkout -b new_branch. Now, if I use git pull, it shows me merge errors, because my branch and the code from GitHub has some substantial differences.

Note: I still haven't added an origin. I just commit to my local repo, and none of my code is on GitHub.

Thanks.

Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25
  • It seems like you’re creating a repository for your various projects etc while studying, right? Why don’t you just make separate folders within the same repo? This would let you copy down the files themselves from the branch, without causing any conflicts. – JBallin Nov 05 '20 at 04:16
  • Good idea, but is there a way to do this without manually downloading? I want to know if it is possible using Git. – Safwan Samsudeen Nov 05 '20 at 04:22
  • This isn’t what git is intended for so there isn’t a clean way to do it. https://stackoverflow.com/questions/2668886/git-copy-all-files-in-a-directory-from-another-branch – JBallin Nov 05 '20 at 04:23

2 Answers2

1

You can try the below command

git checkout -b <new-branch> <commit-id>/<branch>
1

To create a branch without any history, you can use git checkout --orphan YOUR_NEW_BRANCH (git docs).

You'll then have a new branch that contains all the files that were in the initial branch, which you'll now need to delete using git rm -rf . (In git, is there a simple way of introducing an unrelated branch to a repository?).

You can then use git pull URL THEIR_BRANCH to pull down their branch.

JBallin
  • 8,481
  • 4
  • 46
  • 51