将文件提交到不同的分支而无需签出 [英] Commit a file to a Different Branch Without Checkout

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

问题描述

是否可以在 git 分支中提交文件而不检查该分支?如果是这样怎么办?

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

本质上,我希望能够在我的 github pages 分支中保存文件,而无需一直切换分支.有什么想法吗?

Essentially I want to be able to save a file in my github pages branch without switching branches all the time. Any thoughts?

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

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.

推荐答案

这不可能.

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

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.

对于您的特定用例,一个简单的方法是保留两份工作副本,一份在 master 分支检出,另一个在 pages 分支检出.

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.

pages 工作副本中,添加 master 副本作为远程仓库.

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

  • 您在 master
  • 上提交页面
  • pages 副本上的 master 拉取
  • 推送到 GitHub
  • 将 master 分支重置为其先前的状态.

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

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