Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git create new branch in new folder

Tags:

git

I know how to create branches in git bash with

git branch develop

then to switch to that branch

git checkout develop

I want this specific branch to have it's own folder and anytime i commit to this branch the folder is updated.

How do i accomplish this in Git bash?

like image 760
user892134 Avatar asked Oct 21 '25 08:10

user892134


1 Answers

The closest thing you can get is called "worktrees", you can read more about it here.

A short description of it is that you will have 1 main repository with a working folder, and you may for instance check out the master branch in this working folder.

Then additionally you would check out another branch, such as develop, into a separate working folder backed by the same repository.

This would allow you to have more than one branch "live" in a working folder on disk at the same time.

However, there is no "when I commit to this branch the folder is updated", instead you have to work in that folder.

It will basically look like two distinct clones, except that you don't have to push and pull between them since there is only one repository backing the two folders.

Here's a short example of how to set it up (using Windows command syntax):

C:\> cd \dev\projects
C:\dev\projects> md MyAwesomeProject
C:\dev\projects> cd MyAwesomeProject
C:\dev\projects\MyAwesomeProject> git clone https://github.com/user/awesome.git master
.... snip

C:\dev\projects\MyAwesomeProject> cd master
C:\dev\projects\MyAwesomeProject\master> git worktree add ..\develop develop
Preparing worktree (checking out 'develop')
HEAD is now at fca4b6c My most recent awesome commit

C:\dev\projects\MyAwesomeProject\master> cd ..
C:\dev\projects\MyAwesomeProject> dir

 Volume in drive C is System
 Volume Serial Number is 1234-5678

 Directory of C:\dev\projects\MyAwesomeProject

02.10.2018  13.43    <DIR>          .
02.10.2018  13.43    <DIR>          ..
09.11.2018  10.26    <DIR>          develop
08.11.2018  09.48    <DIR>          master
               0 File(s)              0 bytes
               4 Dir(s)  229 636 960 256 bytes free

If you want to work in the develop branch, do it in the develop folder, and if you want to work on master, do it in that folder.

After you have committed in one folder you can switch to the other and merge the other branch in, because it is just 1 repository backing both worktrees.

like image 158
Lasse V. Karlsen Avatar answered Oct 23 '25 22:10

Lasse V. Karlsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!