我有一种情况,我想撤消分支上的第一次提交。 一个简单的解决方案是创建一个新分支,然后将除第一个提交以外的所有提交从我的第一个分支移至新分支,然后删除旧分支。
我想这可以通过简单地弯曲分支的底部来更好地完成。这样的事情可能吗?我尝试勾勒出来:
当前:
final TableCell<ModelTab4, Void> cell = new TableCell<ModelTab4, Void>() {
private final Button btn1 = new Button();
{
btn1.setOnAction(click -> {
try {
ModelTab4 analyse = getTableView().getItems().get(getIndex());
idAnalyse = analyse.getNum1();
if (getItem()!=null){
mainApp.goConsultResult(idPerso, idPatient, idAnalyse);
}
else {
mainApp.goAjoutResult(idPerso, idPatient, idAnalyse);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
public void updateItem(String dateR, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
}
else {
if (dateR!=null){
setGraphic(btn1);
ImageView icone1 = new ImageView("consult.png");
icone1.setFitHeight(20);
icone1.setFitWidth(20);
btn1.setGraphic(icone1);
setStyle("-fx-alignment : CENTER;");
}
else if (dateR==null){
setGraphic(btn1);
ImageView icone2 = new ImageView("ajout.png");
icone2.setFitHeight(20);
icone2.setFitWidth(20);
btn1.setGraphic(icone2);
setStyle("-fx-alignment : CENTER;");
}
}
}
};
想要
:master a - b
branch1 \ revertB - c - d
可能吗?:
master a - b
branch1 \ - c - d
我不需要revertB提交。 我怎样才能做到这一点,或者这是不好的做法? 在此先感谢:)
答案 0 :(得分:2)
git rebase --onto b c branch1
这将从branch1
开始对c
进行所有提交,并将它们重新设置到b
上。
想起来就像在树上剪下树枝然后重新移植 --onto
树的其他部分(b
)。
Git rebase功能强大。学习使用它!尤其是交互模式(git rebase -i
)。有关其功能的示例以及对git rebase
的工作方式的了解,请参见https://stackoverflow.com/a/61411955/8910547的the todo file explained
部分
答案 1 :(得分:1)
“可能吗?”
绝对。
实现它的一种方法:
# create your "skip" branch
git checkout -b skip <commitHash of "revertB">
# repair branch1 by resetting to master then recreating commits c and d
git checkout -B branch1 master
git cherry-pick c d
答案 2 :(得分:1)
如果尚未推送任何内容,则可以在multiple ways中修改历史记录。如果您推动了自己的状态,则需要考虑它会带来的影响!
对于在branch1
上创建了提交但尚未推送提交的任何协作者,您都会造成麻烦。如果您以前从未使用过branch1
,那么您应该没事。
我找到了最直观的使用方式
git checkout branch1
git rebase -i master
这将打开一个这样的文本文件。如下所述,将您要摆脱的提交更改为drop
。然后保存文件并退出。
drop 1a432d7 revertB
pick 2657446 c
pick 8d15847 d
# Rebase 1219262..8d15847 onto 1219262 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
由于我在测试存储库中始终针对此答案修改了同一行,因此发生了合并冲突。在提交b中,我写了b
。在提交c中,我写了c
。在commit revertB中,我写了revertB
。
<<<<<<< HEAD
b
=======
c
>>>>>>> 2657446... c
如果遇到任何合并冲突,则必须修复。就我而言,我将文件内容替换为c
。
在暂存固定文件后继续进行变基:
git add myfile
git rebase --continue
您可以选择修改提交c的提交消息。确认后,revertB
就从branch1
消失了。