暂时从 git master 分支中删除代码

发布于 2024-12-11 14:03:28 字数 267 浏览 0 评论 0原文

我在 git 中跟踪了一个项目,它有一个主分支,该分支成为我的代码的发布版本。我在该分支中有一些代码导致了问题,因为它依赖的外部库当前已损坏。然而,我预计外部库将在未来的某个时候(几周,所以隐藏并不是我真正想要的)被“修复”,并且我希望恢复有问题的代码。

那么问题来了:

如何保存分支中的代码以供将来使用?如果我只是创建一个名为 saveme 的分支,在 master 分支上进行更改,然后尝试将 saveme 合并回 master 以获取“已保存”的代码,这似乎会导致“已经是最新的”。

I have a project tracked in git that has a master branch that becomes the release for my code. I have some code in that branch that is causing problems because an external library that it relies on is currently broken. However, I expect that the external library will be "fixed" at some point in the future (weeks, so stash is not really what I want here) and that I will want to resurrect the problematic code.

So, the question:

How can I save code from a branch for future use? If I simply create a branch called saveme, make changes on the master branch, and then try to merge saveme back into master to get back the "saved" code, this seems to result in "already up-to-date".

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

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

发布评论

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

评论(1

温折酒 2024-12-18 14:03:28

当您创建 saveme 分支时,您的历史记录如下所示:

 A -- B -- C (saveme,master)

.... 然后,当您在 master 上进行更多工作时,为了应对损坏的库,您的历史记录如下这样:

 A -- B -- C (saveme) -- D -- E (master)

因此,如果您尝试将 saveme 合并到 master 中,它会显示“已经是最新的”,因为 saveme 的完整历史记录已包含在 master 中。听起来好像您想要的是创建一个与 saveme 完全相同的树的提交,但只是在 master 之上的另一个提交。您可以通过使用 git revert 提交 E 然后使用 D 来完成此操作,但如果您想一次性完成,您可以这样做以下:(

# Check that "git status" is clean:
git status

# Set the index (staging area) to be as it was at the tip of the branch saveme:
git read-tree saveme

# Create a commit based on that index:
git commit -m "Going back to the state of the branch 'saveme'"

# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
git reset --hard

这是我在 这个答案。)然后你的历史记录将看起来像:

 A -- B -- C (saveme) -- D -- E -- F (master)

...但是提交 F 的树将与提交 C 的树相同。

When you create the saveme branch, your history looks like this:

 A -- B -- C (saveme,master)

.... then when you work more on master, to cope with the broken library, your history looks like this:

 A -- B -- C (saveme) -- D -- E (master)

So if you try to then merge saveme into master, it says "Already up-to-date" because the complete history of saveme is already contained in master. It sounds as if what you want is to then create a commit which has exactly the same tree as saveme, but is just another commit on top of master. You could do this by using git revert for commit E and then D, but if you want to do it in one go, you can do the following:

# Check that "git status" is clean:
git status

# Set the index (staging area) to be as it was at the tip of the branch saveme:
git read-tree saveme

# Create a commit based on that index:
git commit -m "Going back to the state of the branch 'saveme'"

# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
git reset --hard

(This is a trivial variation of the recipe I suggested in this answer.) Then your history will look like:

 A -- B -- C (saveme) -- D -- E -- F (master)

... but the tree of commit F will be the same as that of commit C.

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