简体   繁体   English

即使当前分支中有未提交的更改,如何以编程方式编辑另一个分支中的文件?

[英]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:我想到的可能方法:

  • The program runs 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
  • The program creates a temporary working directory for the 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:例如:

  1. hash-object would create a blob (from a file or stdin) hash-object将创建一个 blob(来自文件或标准输入)
  2. mktree to create a tree with the blob(s) mktree使用 blob 创建一棵树
  3. commit-tree to create a commit with the tree commit-tree创建提交
  4. update-ref to move a ref to the new commit update-refupdate-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.

相关问题 当前分支上有未提交的更改时签出另一个分支 - Checkout another branch when there are uncommitted changes on the current branch 将未提交的更改从当前分支移动到与那些更改冲突的另一个分支 - Move uncommitted changes from current branch to another branch that conflicts with those changes 如果当前分支有未提交的更改,如何防止git合并 - How to prevent git merge if current branch has uncommitted changes 如何列出仅在git中当前分支中进行的所有未提交的更改 - how to list all uncommitted changes made only in current branch in git 如何在Git上将未提交的更改从一个分支移动到另一个分支 - How to move changes as uncommitted from one branch to another on Git Git(无分支)具有未提交的更改 - Git (no branch) with uncommitted changes 用当前分支子目录的未提交内容破坏另一个git分支 - Clobber another git branch with uncommitted contents of current branch subdirectory 使用git,如何将一些未提交的更改从一个分支移动到另一个文件夹中的另一个分支? - Using git, how do you move some uncommitted changes from one branch to another branch in a different folder? 如何将未提交的更改放在SourceTree中的新分支上? - How to put uncommitted changes on a new branch in SourceTree? 如何将未提交的更改放入主分支 - how to put uncommitted changes into master branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM