更改 git 中分支的根目录

发布于 2024-11-02 05:13:39 字数 498 浏览 3 评论 0原文

我正在使用 git 并且想要更改现有分支的基础。这是由部署系统引起的,该系统将该显式分支拉入我的生产环境。在规划我的发布时,我每次想要上线时都会创建一个标签。但我的分支也有特殊的更改,因此 git reset --hard v1.0 不起作用。

这是一个小例子。我希望它

      C---D---E deploy
     /
A---B---F---G master
     \
      v1.0

变成这样

                          C---D---E deploy
                         /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

也许 git rebase 是我正在寻找的,但手册页对我没有帮助。感谢您的回复!

I'm using git and want to change the base of an exiting branch. This is caused by a deployment system, which pulls this explicit branch into my production environment. When planning my releases, I create a tag every time I want to go live. But my branch has special changes too, so git reset --hard v1.0 won't work.

Here a small example. I want this

      C---D---E deploy
     /
A---B---F---G master
     \
      v1.0

to become this

                          C---D---E deploy
                         /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

Maybe git rebase is what I am looking for, but the man pages don't help me. Thanks for your replies!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄与心 2024-11-09 05:13:39

git rebase 应该像你说的那样,允许你更改部署的基础:

git checkout deploy
git rebase v1.1 # using the tag
(or:
 git rebase J # SHA1 of J
 or
 git rebase master~1
)

但你最终会得到

C'---D'---E' deploy

也就是说,deploy分支的提交部分的SHA1被重写,如果没有人克隆说deploy,这也不算太糟糕> 分支并正在研究它。
由于它是用于部署的分支,因此很可能是这种情况(即没有人在该分支的克隆上工作)。

git rebase should, like you say, allow you to change the base of deploy:

git checkout deploy
git rebase v1.1 # using the tag
(or:
 git rebase J # SHA1 of J
 or
 git rebase master~1
)

But you will end up with

C'---D'---E' deploy

That is, the SHA1 of the commits part of deploy branch are rewritten, which isn't too bad if nobody cloned said deploy branch and was working on it.
Since it is a branch for deployment, that is most likely the case (i.e. nobody was working on a clone of said branch).

南薇 2024-11-09 05:13:39

是的,可以使用rebase来达到想要的效果。以下命令将签出 deploy 分支并重播其所有提交,这些提交无法通过 v1.1 访问,位于 v1.1 之上:(

git rebase v1.1 deploy

详细的方法是:git rebase --onto v1.1 v1.0 deploy

但是为什么要重新设置基础并更改历史记录呢?您可以简单地将开发主线更改为您的部署分支:

git checkout deploy
git merge v1.1

这将使您的所有提交哈希保持不变,您的历史记录将如下所示(M 是合并提交):

      C---D---E-----------M deploy
     /                   /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

因为在变基以及合并期间,在使用基于合并的方法时,您将有合并冲突的历史记录。使用变基,您就不会在变基操作期间发生冲突的历史记录。使用基于合并的工作流程,您稍后可以在合并提交的(组合)差异中看到冲突。

yes, you can use rebase to achieve the desired effect. the following command will checkout the deploy branch and replay all its commits, which are not reachable through v1.1, on top of v1.1:

git rebase v1.1 deploy

(the verbose way would be: git rebase --onto v1.1 v1.0 deploy)

but why rebasing and altering history? you can simply change the mainline of development into your deployment-branch:

git checkout deploy
git merge v1.1

this will leave all your commit hashes intact, your history will then look like this (M being the merge commit):

      C---D---E-----------M deploy
     /                   /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

since conflicts might arise during rebase as well as during merge, you will have a history of merge conflicts when using the merge based approach. with rebase you don't have a history of conflicts which happened during rebase operation. using a merge based workflow, you can later see your conflicts in the (combined) diff of the merge commits.

还给你自由 2024-11-09 05:13:39

我不明白你为什么想失去原来的分支。在这种情况下我会做什么:

 # create a new branch from your 1.1 tag
 git checkout -b deploy1.1 v1.1 
 # merge your existing branch into this one
 git merge deploy

编辑:添加架构

你最终会得到类似的结果

       C---D---E deploy
       /        \_______ 
      /                  F deploy1.1
     /                  /
A---B---F---G--H--I--J--K--L
     \                   \
    v1.0                 V1.1

I don't understand why you'd want to lose your original branch. What I would do in such a case:

 # create a new branch from your 1.1 tag
 git checkout -b deploy1.1 v1.1 
 # merge your existing branch into this one
 git merge deploy

EDIT: added schema

You'll end up with something like that

       C---D---E deploy
       /        \_______ 
      /                  F deploy1.1
     /                  /
A---B---F---G--H--I--J--K--L
     \                   \
    v1.0                 V1.1
一紙繁鸢 2024-11-09 05:13:39

git rebase 应该适合你:

git checkout deploy
git rebase master~1

或者

git rebase v1.1

看看 http://progit.org/book/ch3 -6.html - 我认为应该可以帮助你更好地理解 rebase

git rebase should work for you:

git checkout deploy
git rebase master~1

or

git rebase v1.1

Have a look at http://progit.org/book/ch3-6.html - should help you understand rebase better I think

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文