翻译结果:选择所有更改特定文件的提交(转移更改和其历史记录)

huangapple go评论47阅读模式
英文:

cherrypick all commits that change specific files(transfer changes & its history)

问题

抱歉,无法提供代码翻译,只能翻译文本部分:

"Unfortunately dev & master are developed separately from a few weeks ago. many files are created or updated on branch dev and i want have this changes and its history on master branch for just some files.

how cherrypick(or any transfer way) all commits that change specific files from dev branch to master branch?

I think it should be get log of commits for all those files and sort them by date then cherrypick but it takes a lot of time."

抱歉,不提供其他类型的回复。

英文:

Unfortunately dev & master are developed separately from a few weeks ago.many files are created or updated on branch dev and i want have this changes and its history on master branch for just some files.

how cherrypick(or any transfer way) all commits that change specific files from dev branch to master branch?

I think it should be get log of commits for all those files and sort them by date then cherrypick but it takes a lot of time.

答案1

得分: 1

我不知道直接在git中做这个的方法,但你可以使用git log列出所有相关的提交,然后使用一些Shell脚本将它们应用到主分支上。

首要任务是找出masterdev分支分叉的地方。如果dev基于master进行变基,那很容易(那个点就是master),但如果没有,你可以使用git merge-base来找到它。
一旦找到了,你可以使用git log按相反顺序列出所有相关的提交哈希并将它们传递给git cherry-pick,使用xargs

master分支上运行:

git log `git merge-base master dev`..dev --reverse --format='%H' -- x.txt | xargs git cherry-pick
英文:

I'm not aware of a way of doing this in git directly, but you could use git log to list all the relevant commits, and use some shell scripting to apply them on master.

The first order of business is to find where the master and dev branches diverge. If dev is rebased on top of master it's easy (that point is simply master), but if not, you can use git merge-base to find it.
Once you've found it, you can use git log to list all the relevant commit hashes in reverse order and pipe them to git cherry-pick using xargs.

Running on the master branch:

git log `git merge-base master dev`..dev --reverse --format='%H' -- x.txt | xargs git cherry-pick

答案2

得分: 0

以下是已经翻译好的部分:

这里有一个备选方案,但它有一些缺点,所以请仔细考虑是否满足您的需求:

从“特定文件”的限制来看,我认为您只关心文件的状态,而不一定关心逻辑变化是什么(即这个文件因为 bug 修复 #149 而改变)。也就是说,我只想要这些特定文件与dev分支中的内容匹配。

请记住,一个 Git 提交并不是一组更改,而是在该提交创建时文件的时间点快照。在maindev93ab34de7752fbe之间的所有 diff 输出都没有保存在提交对象中,所有这些 diff 在运行git diff命令时动态计算。重新播放这些提交(cherry-picking)不是为了使文件达到相同的状态,而是为了将那些历史记录——这些时间点与为何进行这些更改的原因相结合——带入主分支。

在 cherry-pick 之后,main 如何达到当前状态的叙述包括讨论 bug 修复 #149、特性 Y 和代码重构以提高清晰度以及每个这些逻辑提交后文件的状态的提交消息。如果您需要还原main,提交消息将帮助您快速了解为何更改了这些文件的上下文。例如,可以针对特性 Y 存在性能回退的原因选择提交7752fbe

但在这个问题中,请求似乎更担心特定文件的状态,而不是将特定的历史片段带入前景。历史是关于一组文件的。请求似乎是“我需要这个最终状态”,而不是更改(毕竟,更改可能涉及比我们列出的文件更多的文件)。

解决方案如下:

  1. main创建并进入一个新分支(出于卫生原因);将其命名为surgery
  2. git checkout dev -- [file1, file2, file3]
  3. 测试以确保您的代码现在符合要求
  4. 创建一个新的提交;这将捕获main的状态+您从dev检出的那些文件。
  5. 返回到main并合并您的surgery提交。

从概念上讲,surgery类似于所有这些 cherry-pick 提交的压缩合并。

这样做的缺点,以及任何压缩合并的缺点,是您会失去围绕代码开发的元数据。Git 不再跟踪中间更改的各个提交。在surgery提交消息中包含git log main..dev可以是一个应急措施。

另一个缺点是确认更改是否完整。当提交feature Y时,它是一个 Git 作者制定的逻辑更改集,并且可以描述这些更改的原因。假设他/她/他们已经测试了更改的质量。也就是说,在每个提交的每个时间点,他们验证了每个更改的功能。如果文件1的更改意味着文件2的更新,那么他们的提交包括了这一点。在我们的git checkout方法中,我们可能会忽略这样的影响。因此,在进行surgery提交时,在验证整个代码库的功能时要非常小心。

英文:

Here's an alternate approach that has some down-sides, so consider carefully if it meets your needs:

From the restriction of 'specific files', I believe that you care only about the state of files, not necessarily what the logical change is (i.e. this file change because of bug-fix #149). That is, I only want these specific files to match the contents in the dev branch.

Remember that a git commit isn't a set of changes, it's a point-in-time snapshot of the files when that commit was made. All the diff output between main, dev, 93ab34de and 7752fbe isn't saved in the commit object, all those diffs are computed dynamically when you run the git diff command. Replaying those commits (cherry-picking) isn't about getting the file to the same point, it's about bringing that history -- those points in time combined with the rationale of why these changes were made -- into the main branch.

After the cherry-pick, the narrative of how main got to its current state includes commit messages that discuss bug-fix #149, feature Y, and code-refactor-for-clarity as well as the state of the files after each of these logical commits was made. If you need to revert main, the commit messages help you quickly understand the context of why these files were changed. For example, commit 7752fbe can be targeted because feature Y has a performance regression.

But in this question, the request seems to worry more about the state of specific files, rather than bringing specific bits of history forward. The history is about a set of files. The request seems to be "I need this end state", rather than the change (after all, the change may involve more files than the ones we've named).

A solution, then:

  1. from main, create and enter a new branch (for hygiene); call it surgery
  2. git checkout dev -- [file1, file2, file3]
  3. test to make sure your code is now as desired
  4. create a new commit; this captures the state of main + those files you've checked out from dev.
  5. go back to main and merge your surgery commit.

Conceptually, surgery is similar to a squash-merge of all those cherry-picked commits.

The downside of this, and any squash merge, is that you lose the metadata around the code development. Git is no longer tracking the individual commits around the intermediate changes. A kludge here would be to include git log main..dev as part of the surgery commit message.

A separate downside is confirming that the changes are complete. When commit 'feature Y' was made, it was a logical set of changes that git-author made, and could describe the rationale for those changes. Presumably he/she/they had tested the quality of the change. That is, they verified that everything worked at each point-in-time of each commit. If a change of file1 implied an update of file2, then their commit includes that. In our git checkout approach, we could miss such repercussions. As such, be very careful in validating functionality of the entire code-base when making your surgery commit.

huangapple
  • 本文由 发表于 2023年5月20日 23:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定