gpt4 book ai didi

git:如何在 merge 时覆盖所有本地更改?

转载 作者:IT王子 更新时间:2023-10-29 00:34:08 25 4
gpt4 key购买 nike

情况是这样的。我在 master 分支上工作。我创建 file1 并提交。我创建 file2 并提交。哎呀。有一天我可能想使用 file2,但它绝对不应该放在 master 分支中。这样我就不会丢失我使用的 file2

git checkout head~1
git branch new-branch
git checkout new-branch

这样我才能继续发展。我将 file3 添加到 new-branch。如果您一直在注意,我有两个分支,master 包含“file1”和“file2”,new-branch 包含“file1”和“文件 3”。

现在是将我所做的更改返回到 master 分支的时候了。最好的方法是什么?我绝对希望 master 分支的负责人指向出现在 new-branch 中的文件,但我也不想丢失我已经完成的工作通过重置在 file2 中完成,以防我想使用它。

请记住,这是一种简化。除了三个文件,我还有十几个文件,其中数十行代码在整个地方都被多次提交更改。我当然希望解决方案不是逐个文件地 merge/ check out ,因为那将是一个巨大的痛苦。

有什么想法吗?

最佳答案

I'm working on the master branch. I create file1 and commit.

date >file1
git add file1
git commit -m 'added file1'

I create file2 and commit.

date >file2
git add file2
git commit -m 'added file2'

Whoops. I may want to use file2, someday, but it's definitely not something that should be put in the master branch.

糟糕。很简单。从你所在的地方创建一个新分支:

git checkout -b savingfile2

这将使 file2 更改 savingfile2 的提交。现在回去放松 master 上的一步

git checkout master
git reset --hard HEAD~1

此时,导致 master 的提交将反射(reflect) file1 的添加,并且 master 和 savingfile2 之间的额外提交将向其添加 file2。

如果您对 master 进行了更多更改,然后想要最终恢复 file2,您将需要将该侧分支 rebase 到新的 master 上:

date >file3
git add file3
git commit -m 'adding file3'
date >file4
git add file4
git commit -m 'adding file4'

现在我们终于要 file2 了:

git checkout savingfile2
git rebase master # might need to fix conflicts here
git checkout master
git merge savingfile2 # will be a fast-forward
git branch -d savingfile2 # no need any more

应该这样做。

关于git:如何在 merge 时覆盖所有本地更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2025085/

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