Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

git - How to get remote repo branch added to local repo

newly created - by creating a folder & running the command git init in short creating a local git repo from the top.

I have here a local git repository (newly created) with 2 branches. Now these branches are just dummy ones which I created, nothing much important to it.

$ git branch
* repo2-branch1
  repo2-branch2

I also have here a remote repository (private) from Github with a branch "TLA1", now remember the newly created local repository I mentioned above with those 2 branches? What I wanted to do is to ADD this "TLA1" branch as one of the branches with repo2-branch1 & repo2-branch2 in my newly created local repository as I mentioned.

enter image description here

Let's say the "TLA1" branch has been added. So when I type git branch I would like to have it like this.

$ git branch
* repo2-branch1
  repo2-branch2
  TLA1

Of course when I type git log when I switch to "TLA1" I would also have the commits which is in the remote repository as you can see in the image, cause for me those commits are very important.

Solutions I have tried:

I have done many research and found this, I thought this was already it as it was similar to my goal. But when I tried it I get an error.

$ git checkout -b TLA1 origin/TLA1
fatal: 'origin/TLA1' is not a commit and a branch 'TLA1' cannot be created from it

I also have not tried this as this thing might do something to my remote repo git reset --hard <remote>/<branch_name> & it seems to be not the solution I am finding.

Any solutions to this? I'd really like to have this branch on my newly created repository.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'd need to tell Git to track the remote repository first. So you'll have to do

git remote add origin <url>

Next you can tell git to only fetch that one branch from remote

git fetch origin TLA1

Then you can switch to the branch

git checkout TLA1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...