简体   繁体   中英

Gitlab repository with multiple branches to local

This is more of a clarification question. In Gitlab, you can create multiple branches from the same repository, so the repository myrepository.git can have a master branch and then a development branch for example.

When I clone the repository to a local repository, I often do a single branch and not the whole repository with all of the branches like

git clone <url> --branch development --single-branch

If I am working on multiple branches, I then create a separate directory for each of the branches. My reasoning is that this seems cleaner since I will know what branches I am working in and there's no easy way to switch between the branches and sync up the files. Am I doing this incorrectly? Is there a reason why you would want to have multiple branches in the same directory?

Thanks.

In some version control systems (and some workflows) what you are saying makes perfect sense, because you only have a small number of long-lived branches.

An often repeated advantage of git over earlier versioning systems is that "branches are cheap", leading to workflows where it is common to create and delete multiple branches, every single day . It commonly looks like this:

  • You start working on a task, and you create a new branch for that specific task.
  • You make some changes, and commit them to that branch.
  • Maybe you get called away to something more urgent before you're done - that's fine, you can check out the "main"/"master"/"develop" branch and work on a different branch, then come back to where you were.
  • You finish the task to your satisfaction, and push it to a central location like Github.
  • Your colleagues (or even just yourself) reviews the changes in a web interface and makes sure they're OK to merge.
  • Maybe someone checks them out and does some testing; maybe an automated script checks them out and runs some tests.
  • Finally, you merge the changes to the "main"/"master"/"develop" branch and delete the task branch.

All of this relies on branches being easy to create, and easy to switch between, which is no longer true if you tie each directory to a named branch.

Thank you everyone for your help. After some experimentation, I believe I was initially mistaken when I tried out Gitlab, so my procedure was based on a wrong assumption. I did something like this:

  1. Use git clone to clone a local repository of the remote one.
  2. Change to the clone local repository in the command line.
  3. Use git checkout to try to switch from the master to the development branch. What happen was that I notice the files were not updating to the development branch, I still got the master branch files, so I was under the mistaken impression that this cannot be done with Gitlab.

Further experimentation was unable to duplicate this behavior. Git checkout worked and if you made changes to the file, it will warn you to commit, just as I expected. I think perhaps the VPN was not running at the time, which is needed to connected to the Gitlab repository, though I would think I should have gotten a timeout when I run git checkout, so I am not still sure what happened.

Just to let everyone know that what I asked was not necessary. There is no need to setup separate directory for each branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM