将文件提交到另一个没有签出的分支 [英] Commit a file to a Different Branch Without Checkout

查看:144
本文介绍了将文件提交到另一个没有签出的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在git分支中提交文件而不检出该分支?如果是这样怎么做?



基本上我希望能够在我的github页面分支中保存一个文件,而不必一直切换分支。任何想法?

更新:不能做我想做的事情(请参阅下面的用例评论)。我最终做的是以编程方式将当前目录克隆到tmp目录,然后在tmp目录中检出我的分支(不影响我的工作目录)并将我的文件提交到tmp目录clone。完成后,我会推回到我的工作目录并删除tmp目录。吮吸,但它是将文件提交到另一个分支而不更改工作目录的当前工作分支的唯一方法。如果有人有更好的解决方案,请随时在下面添加它。如果它比'不能做'好,我会接受你的。 这是不可能的。



您提交的更改与当前的工作副本有关。如果你想提交到另一个分支,这意味着你可以从你的工作副本提交更改,但是将它们从另一个副本状态作为基础。

这不是您工作版本化的自然方式,这就是为什么您需要进行不同的步骤(存储更改,检出分支,弹出存储和提交)到完成它。



至于你的具体用例,一个简单的方法是保留你的工作的两个副本,一个签出在 master code>分支,另一个在页面分支。
$ b $

页面工作副本中,添加 master 复制为远程回购。




  • 您在 master

  • master 页面复制

  • push to GitHub

  • 重置主分支在其以前的状态。


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 without switching branches all the time. Any thoughts?

Update: It's not possible to do what I want (see comments below for use case). What I ended up doing is programmatically cloning my current directory to a tmp directory, then checking out my branch in that tmp directory (doesn't affect my working directory) and committing my files to the tmp directory clone. When I'm done, I push back to my working directory and delete the tmp directory. Sucks, but it's the only way to commit files to another branch without changing the current working branch of the working directory. If anyone has a better solution, please feel free to add it below. If it's better than 'it cannot be done', I'll accept yours.

解决方案

It's not possible.

The changes you commit are related to the current working copy. If you want to commit to another branch it means that you could commit changes from your working copy, but base them from another copy state.

This is not a natural way of versioning your work, and this is why you need to make different steps (stash changes, checkout the branch, pop stash and commit) to accomplish it.

As for your specific use case, a simple way is to keep two copies of your work, one checked out at master branch, and the other at pages branch.

In the pages working copy, add the master copy as a remote repo.

  • You commit pages on master
  • Pull from master on the pages copy
  • push to GitHub
  • reset the master branch at its previous state.

这篇关于将文件提交到另一个没有签出的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆