Search code examples
gitlabrepositorybranchclone

How do I clone a repository from a branch and not the main one to my pc?


I'm a beginner in using vscode. I'm also not a programmer. I was tasked to commit some art asset files to GitLab and I found that vscode can help me with that. I successfully cloned the repo to my pc and committed and pushed the files. BUT, they told me afterwards that I shouldn't put the files on the main branch but in another branch.

I was able to see the 2nd branch in vscode using the checkout to and I can see that 2nd branch on the lowest left corner of the window. But I can only see in my explorer the files that is currently existing on the main branch which is also what I have in my pc. I don't know how to move the files to the 2nd branch.

So my question is How do I clone the 2nd branch repo in my pc and not the main branch repo? So that I can put the misplaced files I did from the main branch to the 2nd branch locally and then commit and push.

Also is there a way that I won't have a copy of the main repo locally and just the 2nd branch repo only. So that I can avoid making any changes in the main branch.

If anyone could help me, I would be thankful!

I tried cloning the 2nd branch repo, clone with http. I noticed that the link was the same as the main branch but tried it anyway. And it did clone the main repo still because I could see the file I placed still there and now I have a duplicate of the same folder in my pc.

What I want to happen is to have only the 2nd branch repo cloned in my pc. So that I can close the main branch repo and delete that folder in my pc.


Solution

  • After cloning a repo, the default branch is checked out (usually master, main or develop), but you get access to checkout any existing branch you want. No need to do additional cloning.

    When checking out a branch in VS Code, you should see the corresponding changes in your file explorer (unless the branch points to the same commit as the default branch).

    What I can suggest to make things easier is to start from scratch:

    1. Clone the repo again to make sure it is clean but don't modify anything yet
    2. Open the repo's folder in VS Code
    3. Use the git client within VS Code to checkout the "2nd branch"
    4. Add your files, commit and push (just as you did before when you were on the default branch)