Git - separate folder for each branch. Setting it up

前端 未结 4 1738
鱼传尺愫
鱼传尺愫 2021-02-01 10:08

I have a need to keep 3 branches in 3 separate folders. (I know this is not a git way of doing things. But I need to do this for a reason).

Lets say the repo name is

相关标签:
4条回答
  • 2021-02-01 10:18

    What you want to achieve has become simpler (or even trivial) since git 2.5 introduced the git worktree command.

    Basically, your your git repo has now completely free number of checked out branches, called work tree:

    • 0 (a bare repo)
    • 1 (a normal repo)
    • n > 1, a normal repo where you added n-1 work trees

    If your repo already contains your branch, you can do:

    git worktree add <path to branch> <branch name>
    

    Or, if that branch isn't created yet and you want to branch off master:

    git worktree add -b <new branch name> <path to branch> master
    

    Note that you can't checkout a branch in several directories simultaneously.

    0 讨论(0)
  • 2021-02-01 10:19

    The above scenario as put up in the question, is pretty much valid in following scenarios.

    1. Micro-services: AWS Lambda development for example one or two develop work on an lambda, there are say 70 lambdas total. Thus, 70 folders including test and code_folder. If it was a monolithic system, you have to make sure all parts of system work together by having zero merge issue. But here each of 70 folder is independent except few global configuration and few shared folders.

    2. Monorepo: Large companies use monorepo to manage all sub-projects of a larger project including mobile app, websites, backend and other related stuff.

    Master branch may or may not have all the code, if each branch track only changes in its folder.

    One way to do it, first create a shared branch for shared modules. Then, create multiple branches where each branch starts with empty folder. Never merge your branch with master, only pull shared branch merge your branch locally and push to remote branch.

    Thus, every branch on remote has shared modules and your code.

    0 讨论(0)
  • 2021-02-01 10:34

    I think that you might have to create all the branch in your repo

    $ git branch <name_of_your_new_branch>
    

    Then create all your folders and in each of your folder Clone the repo but Checkout the appropriate branches.

    Each folder should / may track only the appropriate branch

    Step 1 $ git branch [name_branch#1]
    
    Step 2 $ git branch [name_branch#2]
    
    Step 3 $ git branch [name_branch#3]
    

    ...

    Step 4 $ git push --all
    Step 5 md Folder #2
    Step 6 $ git clone [URL]
    Step 7 $ git checkout [name_branch]
    
    0 讨论(0)
  • 2021-02-01 10:37

    Here's a bash script to check out all remote branches into separate worktrees and then keep them in sync. Every time it runs it will throw away any local changes. It just tries to make the checked out code mirror the repo.

    git fetch --all --prune
    
    # reset branch only if it has deviated from the remote
    function update_branch {
            if git diff --exit-code --quiet $1; then
                    echo "No changes on branch $1"
            else
                    echo "Resetting branch $1"
                    git reset --hard $1
            fi
    }
    
    # master branch is checked out in the root
    update_branch origin/master
    
    # check out each remote branch into a subfolder
    branches=$(git for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v /HEAD | grep -v /master)
    for branch in $branches; do
            if [ -d $branch ]; then
                    cd $branch
                    update_branch $branch
                    cd $OLDPWD
            else
                    echo "Checking out new branch $branch"
                    git worktree add $branch $branch
            fi
    done
    
    # check for branches that have been deleted on the remote
    for branch in origin/*; do
            if ! git show-ref --verify refs/remotes/$branch --quiet; then
                    echo "Removing worktree $branch"
                    git worktree remove $branch
            fi
    done
    
    git worktree prune
    
    
    0 讨论(0)
提交回复
热议问题