Commit a file to a Different Branch Without Checkout

前端 未结 10 2099
南方客
南方客 2020-12-05 09:55

Is it possible to commit a file in a git branch with out checking out that branch? If so how?

Essentially I want to be able to save a file in my github pages branch

10条回答
  •  感动是毒
    2020-12-05 10:11

    As several others have said, it is literally possible, but impractical.

    However, as of Git 2.5 (with some important fixes in 2.6 and minor ones since then), there is a practical method for doing this using git worktree add.

    Let's say, for instance, that you want to work on branches main and doc "at the same time", or branches develop and test "at the same time", but the two branches in question deliberately contain different things. (For instance, the doc branch has documentation that exists outside or alongside the code, or the test branch has tests that will be run against the code, but not distributed, or which are expected to have failures for which tests are deliberately skipped on the develop side, or whatever.)

    Instead of just:

    git clone -b develop  theclone
    

    followed by working in theclone with constant switching back and forth between the two branches, you would:

    git clone -b develop  theclone
    

    but then:

    cd theclone
    git worktree add ../ct test  # check out branch test in ../ct
    

    or just:

    git worktree add ../test     # check out branch test in ../test
    

    Now you can run your tests in ../test while developing in theclone. You can merge and/or rebase changes from one branch to the other in the usual way: the underlying repository is already shared, so no git push or git fetch is required. You simply have both branches checked out, into two separate work-trees, named theclone and test from the top level.

提交回复
热议问题