[英]How to programmatically edit files in another branch even when there are uncommitted changes in the current branch?
As a personal exercise, I am writing a command line and git-based issue tracker where the issues are saved in another git branch (say, project-issues
).作为个人练习,我正在编写一个命令行和基于 git 的问题跟踪器,其中问题保存在另一个 git 分支中(例如, project-issues
)。 Let's say I'm currently working on the master
branch.假设我目前正在master
分支上工作。 With my issue tracker, running issue-tracker --add-issue "Fix all bugs"
will need to modify the contents of the project-issues
branch.使用我的问题跟踪器,运行issue-tracker --add-issue "Fix all bugs"
将需要修改project-issues
分支的内容。 So the question is: what is a recommended way to programmatically modify the contents of another branch (even if there are uncommited changes in the current branch)?所以问题是:以编程方式修改另一个分支的内容的推荐方法是什么(即使当前分支中有未提交的更改)?
Possible methods I've thought of:我想到的可能方法:
git stash
on the branch that the user is working on, switches to the project-issues
branch, makes the required commits to the project-issues
branch, switches back to the branch that the user was originally on, then runs git stash apply
.该程序在用户正在处理的分支上运行git stash
,切换到project-issues
分支,将所需的提交提交到project-issues
分支,切换回用户原来所在的分支,然后运行git stash apply
。project-issues
branch using git-workdir, makes the required commits in the temporary working directory, then deletes the temporary working directory.该程序使用 git-workdir 为project-issues
分支创建一个临时工作目录,在临时工作目录中进行所需的提交,然后删除临时工作目录。Is there a simpler solution to this problem?这个问题有更简单的解决方案吗?
PS The program is written in Python. PS 该程序是用 Python 编写的。
You could use low-level plumbing commands to create blobs, trees and commits.您可以使用低级管道命令来创建 blob、树和提交。 Eg:例如:
hash-object
would create a blob (from a file or stdin) hash-object
将创建一个 blob(来自文件或标准输入)mktree
to create a tree with the blob(s) mktree
使用 blob 创建一棵树commit-tree
to create a commit with the tree commit-tree
创建提交update-ref
to move a ref to the new commit update-ref
将update-ref
移动到新提交There is a small basic example here .有一个小的基本的例子在这里。
Another idea is to use git notes
or at least just a ref (or orphaned branch) instead of regular branch to keep the issue information separate and independent from project files and branches.另一个想法是使用git notes
或至少只是一个 ref(或孤立分支)而不是常规分支,以保持问题信息与项目文件和分支分开并独立。
I'd suggest avoiding git stash
.我建议避免git stash
。 It's full of sharp corner cases.它充满了尖锐的角落案例。
If you have git worktree add
, use that.如果您有git worktree add
,请使用它。 It has a nasty bug from the point it first appeared (Git 2.5) until Git 2.15, which means you should complete any work you do in an added work-tree within two weeks and then delete the added work-tree, but if you're doing this in a program, you'll finish in minutes or at worst hours, not weeks.从它第一次出现(Git 2.5)到 Git 2.15,它有一个令人讨厌的错误,这意味着你应该在两周内完成你在添加的工作树中所做的任何工作,然后删除添加的工作树,但如果你在程序中重新执行此操作,您将在几分钟内或最坏的几个小时内完成,而不是几周。
The low-level method in kan's answer will be the most efficient (by far) but requires understanding the internal details of Git, at least to some extent. kan 的答案中的低级方法将是最有效的(到目前为止),但至少在某种程度上需要了解 Git 的内部细节。
如果您使用带有 webhooks 的 Jenkinsfile 管道,您可以更好地控制要执行的自定义任务,无论如何,如果您在不提交的情况下切换到另一个分支,则必须将其隐藏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.