Commit a file to a Different Branch Without Checkout

前端 未结 10 2097
南方客
南方客 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

    So long as you don't have anything in your current index that differs from your HEAD that you want to keep you can so something like this. (If you do want to keep your index you could temporarily export the GIT_INDEX_FILE environment variable to point at a temporary file for the duration of these commands.)

    # Reset index and HEAD to otherbranch
    git reset otherbranch
    
    # make commit for otherbranch
    git add file-to-commit
    git commit "edited file"
    
    # force recreate otherbranch to here
    git branch -f otherbranch
    
    # Go back to where we were before
    # (two commits ago, the reset and the commit)
    git reset HEAD@{2}
    

    We've never actually checked out otherbranch and our working tree files haven't been touched.

提交回复
热议问题