1

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.

Gavin Siu
  • 81
  • 1
  • 9
  • 2
    This seems like an unnecessary level of caution that will cause more problems than it prevents. That's my opinion, of course, and it's why this question is off topic. It's a matter of opinion and specific workflow. – isherwood Jun 17 '21 at 15:02
  • You can edit the shell prompt to show the currently checked out branch. See [questions/15883416](https://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt) – Todd Jun 17 '21 at 15:27

2 Answers2

2

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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Perhaps this does stem from an incomplete understanding of Git. I worked most of my life on systems like CVS and Clearcase which uses a different architecture than Git. If I check out a remote repository to a local one. How do I then switch between the branches in the local repository and have the code update? If I Git Switch to a different branch, the code in the directory do not change. I think I am missing something here. – Gavin Siu Jun 17 '21 at 19:56
  • @GavinSiu `git switch` (or `git checkout`) will check out the *committed* code on a particular branch (more specifically, the snapshot of the entire repository in the latest commit on that branch). It won't (by default) overwrite any *uncommitted* changes - it tries not to do anything that you can't undo. So if you're seeing code not changing when you switch branches, my guess is that you haven't committed it (or, it just happens to be the same on both branches). I've never used ClearCase, but you can probably find a git tutorial written by someone who's used both. – IMSoP Jun 17 '21 at 20:40
  • @GavinSiu In case you haven't already read through the [tour] be aware that this site is not designed to act like a forum; the comments box here is designed for short clarifications to the answer (which is probably why your comment there got cut-off mid-sentence). If you need to put a long explanation of the scenario, that should probably be asked as a new question (and do search for existing similar questions first!) – IMSoP Jun 17 '21 at 21:16
  • Hi, I have got through the tutorials but there are often nitty gritty issue not covered. I tried the following 1) Clone the remote repository by git clone url, 2) Switch the dev branch by git checkout dev. 3) examine the file that is different than dev and then thype git checkout master, which switch back and change the file. This is not what I observed before and this mean I probably mistype something and resulted in a confused impression of how Git worked. – Gavin Siu Jun 17 '21 at 21:26
0

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.

Gavin Siu
  • 81
  • 1
  • 9