gpt4 book ai didi

git - 如何在git中的两个分支之间同步一些文件?

转载 作者:太空狗 更新时间:2023-10-29 13:03:46 24 4
gpt4 key购买 nike

我对 git 和我的存储库有疑问。

先写例子

git checkout Proj1
# modify index.html
git commit -am "Modify Index.html"
git checkout Proj2
git show Proj1:index.html > index.html
git commit -am "Modify Index.html"
git checkout Proj1

这是我在两个分支中保留相同文件内容的方法。

当然,如果提交仅包含我要同步的文件,我可以使用 cherry-pick。 How can we sync files between two branches in git?

对于这个问题,你有什么简短的解决方案吗?
也许要写一些别名?
我可以在哪里保存包含要同步的文件列表的文件?

最佳答案

如果您不关心历史记录,那么您可以在分支 2 上进行单独的提交,其中包含的文件与 Proj1 中提交提示中的文件完全相同:

git checkout Proj2
git checkout Proj1 -- index.html

git commit -am "Modify Index.html"

本质上,这意味着完全按照它在 Proj1 分支中的样子获取文件。

git checkout --表示在破折号之后你提供了一个文件名(或路径)。如果您想更改存储库中的某些文件并保留其他文件,这也很有用。

然后,如果需要,您还可以像这样重用您在 Proj1 中所做的提交中的提交消息:

git checkout Proj2
git checkout Proj1 -- index.html

git commit -a -C Proj1

Git commit -C意味着重用您已经拥有的提交评论。

来自git documentation "git checkout" :

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>

When or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named (most often a commit).

[...]

git checkout with or --patch is used to restore modified or deleted paths to their original contents from the index or replace paths with the contents from a named (most often a commit-ish).

并且来自 git documentation "git commit"

git commit -C <commit>

--reuse-message=<commit>

Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

关于git - 如何在git中的两个分支之间同步一些文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36419916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com