How to work simultaneously on a few branches

前端 未结 4 1794
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 06:40

This is a follow-up on this question on creating branches.

It strikes me as odd that I would still work on one repository because the files on my local machine will be a

相关标签:
4条回答
  • 2021-01-30 06:58

    As of Git 2.5, git-worktree directly supports this workflow. See VonC's answer to this question for details.

    My answer below may suffice if you don't like git-worktree for whatever reason.


    Git is designed to allow you to work within a single folder on disk. This is a single repository that contains all the branches you care about. You checkout whichever branch you want to work on at the time.

    Within a Git repository, you can only have a single branch checked out at a time. If you check out a second branch, the files on disk are removed and replaced with those from the second branch.

    If you have the following branches:

    BRANCH-A        BRANCH-B
    alpha.txt       alpha.txt
    bravo.txt
    charlie.txt     charlie.txt
                    delta.txt
    

    When you're on branch-A and you checkout branch-B, then bravo.txt will be removed and delta.txt will be added to your working directory.

    However, git-checkout will not overwrite changes you've made to files unless you supply the -f argument. If you make a change to alpha.txt then try to switch to branch-B, you'll get a message warning you that your changes would be lost and aborts the checkout.

    The exceptions are untracked files. If you have branch-A checked out and you create a new file called echo.txt, Git will not touch this file when you checkout branch-B. This way, you can decide that you want to commit echo.txt against branch-B without having to go through the hassle of (1) move the file outside the repo, (2) checkout the correct branch, and (3) move the file back into the repo.


    Footnote

    Actually, Git doesn't force you to use a single working directory. If you want, nothing is stopping you from creating different paths on disk for each branch you want to work on.

    /home/me/project
     +-- branch-a/
     +-- branch-b/
     +-- ...
    

    Each of these paths is its own Git repository (each one has a .git folder inside), and you can push and pull commits between the repos.

    cd ~/project                     ## Go to my projects directory
    git clone branch-a branch-b      ## Create a new branch-b
    
    cd branch-b
     ... work work work ...
    git commit -a -m "Made some changes on branch-b"
    
    git pull origin                  ## Fetch and merge the changes from branch-a
    git push origin                  ## Push my changes back to branch-a
    

    This is how some people use Mercurial if they aren't using named branches: they clone the repository into a new directory on disk for each branch they want, then push and pull changesets between them.

    0 讨论(0)
  • 2021-01-30 06:59

    I suggest my solution via small script https://web.archive.org/web/20141114201917/http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html

    While git stash tools are nice, I find having multiple clones much better. You can run tests in one branch, while work on other. Or you don't even have to stop debugger, close editor, clean tempfiles or something else.

    0 讨论(0)
  • 2021-01-30 07:01

    With Git 2.5+ (Q2 2015), Git is no longer designed to work within a single folder (ie a single working tree)

    Git will support multiple working trees (for one cloned git repo) with the new command git worktree add <path> [<branch>].

    That replaces an older script contrib/workdir/git-new-workdir, with a more robust mechanism where those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktrees folder (so that work on any OS, including Windows).

    You will be able to checkout different branches in different path while being part of the same main cloned repo.

    See more at "Multiple working directories with Git?"

    0 讨论(0)
  • 2021-01-30 07:02

    Your concern about 'the files on my local machine will be a weird mix of different experiments.' is unfounded - if you have branch 2 checked out, you will not see the files of branch 1 at the same time.

    I would do something like

    # on master branch
    git checkout master
    # Create a branch for feature 1
    git checkout -b feature_1
    # work on feature 1
    
    # Start a new feature branch
    git checkout master
    git checkout -b feature_2
    # work on feature 2
    
    # feature 2 finished and committed, time to merge 
    git checkout master
    git merge feature_2
    
    # update feature_1 branch
    git checkout feature_1
    git merge master
    
    0 讨论(0)
提交回复
热议问题