Commit a file to a Different Branch Without Checkout

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

    It can be done by reimplementing git commit.

    This can be done with various call to git hash-object

    But this is hard to achieve.

    Please read progit chapter 9 for more details and a full example of how to simulate a commit.

    0 讨论(0)
  • 2020-12-05 10:25

    While there is currently no single command to do this, there are at least two other options.

    1. You could use the github api to create the commit. This post details creating a commit in a github repo.

    2. Create github pages as a submodule.

    3. Use a series of plumbing commands to create the commit.
      The git book has a description of plumbing commands used to create a commit

    note: the command is now mktree not mk-tree

    0 讨论(0)
  • 2020-12-05 10:27

    I made a little tool that does exactly this: https://github.com/qwertzguy/git-quick

    It let's you edit specific files from another branch without checking out the other branch completely (just the files you want to edit) and commit them. All this without ever affecting your working copy or staging area.

    Behind the scenes it uses a combination of git worktree and sparse checkout. The source is fairly small, so you can read through.

    0 讨论(0)
  • No you cannot, however what you are doing by hard copying files is implemented by git stash using commits. The stash works as a stack of changes that are saved "somewhere outside the branches spaces", hence can be used to move modifications between branches.

    • Make your changes on <file>
    • git add <file>
    • git stash
    • git checkout <branch you want to commit>
    • git stash pop
    • git commit -m "<commit message>"

    you can probably scrip these simple commands down if you do this constantly.

    0 讨论(0)
提交回复
热议问题