[英]Git branch from a prev commit and pull changes from master
Let's say I have made following commits. 假设我已经进行了以下提交。 All commits were in the same branch.
所有提交都在同一分支中。 In Commit 3, I deleted C and now I need to add C back to the current
在提交3中,我删除了C,现在我需要将C添加回当前
Commit 1: A,
Commit 2: A, C
Commit 3: A,
.
.
.
Master (current) : A, B, D
And now, I want to create a branch with A, B, C, and D. This is what I have tried. 现在,我想用A,B,C和D创建一个分支。这是我尝试过的。
create a local and remote branch at commit 2 在提交2创建本地和远程分支
git checkout branch mybranch hashtag-of-commit2 git checkout分支mybranch hashtag-of-commit2
pull changes from origin/master (current) to add B and D 从原点/原点(当前)提取更改以添加B和D
git checkout mybranch git checkout mybranch
git fetch origin git获取来源
git merge origin/master git merge原始/主
Doing this, it deletes C and adds B and D. The final result is A, B, D. What I'd like to have is A, B, C and D. How do I this? 这样做,它将删除C并添加B和D。最终结果是A,B,D。我想要的是A,B,C和D。我该怎么办?
Here's what I think the question means, in more traditional layout, changing letters for numbers and numbers for letters. 我认为这个问题的意思是,在更传统的布局中,将字母更改为数字,将数字更改为字母。
The log (single branch) looks like this 日志(单个分支)如下所示
---A---B---C---D--- .... ---- HEAD master
+x -x
Commit B adds x
and commit C reverts that change. 提交B添加
x
,提交C还原该更改。 OP wants to add x
back in. OP希望重新添加
x
。
Assuming you are on master (ie already have the changes introduced by commits D
.. master
), prima facie git revert sha-1-of-C
should do that, ie you are reverting the commit that reverted the original change. 假设您在master上(即已经具有由commit
D
.. master
引入的更改),则prima facie git revert sha-1-of-C
应该做到这一点,即您正在还原已还原原始更改的提交。
I don't see what this has to do with a remote branch. 我看不到这与远程分支有什么关系。
A whole git revert
would also revert the changes to A
introduced in Commit3
, so that's not what you want. 整整
git revert
也将恢复更改到A
在介绍Commit3
,所以这不是你想要的。
To restore file C
as present in Commit2
, you have not only one option: 要将文件
C
还原为Commit2
文件,您不仅有以下选择:
C
C
的状态 git show
shows the content of a file in a certain revision. git show
显示某个修订版中文件的内容。 So 所以
git show Commit2:C > /path/to/C
will print the content of file C
and will store it in /path/to/C
. 将打印文件
C
的内容,并将其存储在/path/to/C
。 We still need to add it again to the index: 我们仍然需要将其再次添加到索引中:
git add /path/to/C && git commit -m "restoring C"
We know that C
was changed in Commit3
in a way we don't want. 我们知道,
Commit3
中的C
是以我们不希望的方式更改的。 To create a patch from the changes introduced in Commit3
to C
(ie it's deletion), we could use git diff
: 要从
Commit3
对C
Commit3
的更改(即删除)创建补丁,我们可以使用git diff
:
git diff Commit3~..Commit3 -- /original/path/to/C > c-changes.patch
Now, c-changes.diff
holds the changes introduced. 现在,
c-changes.diff
保留了引入的更改。 Now, we apply this patch in reverse order and restore C
: 现在,我们以相反的顺序应用此补丁并还原
C
:
git apply --reverse c-changes.patch
C
is restored, re-add it to the index: C
恢复后,将其重新添加到索引中:
git add /path/to/C && git commit -m "restoring C"
When you use the previous option, Commit3
still removes C
and you have an extra commit restoring C
. 当您使用上一个选项时,
Commit3
仍会删除C
并且您还有一个额外的提交恢复C
If you haven't pushed anywhere since Commit3
, we could edit Commit3
so that it does no longer delete C
. 如果自
Commit3
您再也没有推送任何Commit3
,我们可以编辑Commit3
,使其不再删除C
First, create the patch as described in option 2. Then, invoke an interactive git rebase
: 首先,按照选项2所述创建补丁。然后,调用交互式
git rebase
:
git rebase -i Commit2
This will bring up a dialog in your $EDITOR
. 这将在
$EDITOR
弹出一个对话框。 You'll see Commit3
. 您将看到
Commit3
。 Choose to e
dit the commit, save the file and close the file. 选择
e
DIT提交,保存文件并关闭文件。 Then, the rebasing will start and it will stop right after Commit3
was applied. 然后,在应用
Commit3
之后, Commit3
启动将开始,并且将立即停止。 Now take your patch and apply it in reverse order: 现在获取补丁并以相反的顺序应用它:
git apply --reverse c-changes.patch
The patch was applied, now we amend the last commit and delete the deletion of C
from history: 已应用补丁,现在我们修改上一次提交,并从历史记录中删除对
C
的删除:
git add /path/to/C && git commit --amend -C HEAD && git rebase --continue
Rebasing continues after this and once it's finished, you have a fresh history where C
was never gone. 此后将继续进行重定基础,一旦完成,您将拥有
C
从未消失的全新历史。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.