我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

这里的大多数解决方案计算了你想回去的承诺数量,我认为这是一个错误的方法,计算需要计算。

你可以简单地通过你想要的承诺的承诺,或者换句话说,你想要的承诺是最后的承诺:

(重定向自Commit Hash)

为了避免这一点:

1) git checkout master

2) git branch <feature branch> master

3) git reset --hard <commit hash>

4) git push -f origin master

其他回答

还有另一种方法来做到这一点,只使用2个命令,同时保持您的当前工作树无缝。

git checkout -b newbranch # switch to a new branch
git branch -f master HEAD~3 # make master point to some older commit

旧版本 - 在我学到 git branch -f 之前

git checkout -b newbranch # switch to a new branch
git push . +HEAD~3:master # make master point to some older commit 

能夠推到. 是一個好技巧知道。

我不得不将7个承诺从一个旧行业转移到一个新行业。

git checkout old-branch     # in the example, master
git reset --hard h4sh       # h4sh is the hash for the commit 
git checkout -b new-branch  
git push origin new-branch

在 git checkout new-branch 之后,我得到了好 git log 和 git 状态,但是,当我访问旧行(git checkout old-branch)时,我得到了“git 落后于 7 行,可以快速推进”。

git checkout old-branch
git status 
> git is behind by 7 commits and can be fast-forwarded
git push origin old-branch -f

在这一步之后,最后7个承诺仅适用于新分支,而以前的承诺则适用于Bitbucket树的旧分支和新分支。

如果你想将你的承诺转移到一个现有分支,它会看起来如下:

git checkout existingbranch
git merge branchToMoveCommitFrom
git checkout branchToMoveCommitFrom
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

警告: 这个方法工作,因为你正在创建一个新的分支与第一个命令: git 分支新分支. 如果你想将承诺转移到一个现有分支,你需要将你的变化融入现有分支之前执行 git 重新设置 --hard HEAD~3 (见移动到一个现有分支上)。

除非有其他情况涉及,否则可以轻松地通过连接和旋转。

# Note: Any changes not committed will be lost.
git branch newbranch      # Create a new branch, saving the desired commits
git checkout master       # checkout master, this is the place you want to go back
git reset --hard HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

git reset --hard a1b2c3d4

最后,您可能需要强迫将最新的更改推到主重复:

git push origin master --force

警告: 使用 Git 版本 2.0 及以后,如果您稍后 git 将新分支转移到原始(主)分支上,您可能需要在转移期间明确的 --no-fork-point 选项,以避免失去所承担的承诺。

如果你只需要将所有的无缝承诺转移到一个新的分支,那么你只需要,

创建一个新的分支从当前一个 :git 分支新分支名 推你的新分支: git 推起源新分支名 转换你的旧(当前)分支到最后一个推 / 稳定状态: git 重新设置 - 硬起源 / 旧分支名

有些人也有其他上流而不是起源,他们应该使用适当的上流。

git reset HEAD~3
git stash
git checkout newbranch
git stash pop

如果你的主要目标是旋转回主 你想保持文件变更 你不关心错误的命令上的消息 你没有推,但 你想这很容易记住 你不想要如临时/新分支, 查找和复制命令, 和其他头痛等并发症

这是什么不做

目标

OP表示,目标是“在这些承诺完成之前重新掌握”而不失去变化,而这个解决方案就是这样。

不要这样做,如果你推了大师的变化

如果你只是重新编写你的本地大师没有影响,当它被推向上流,但推一个重新编写的历史给合作伙伴可能会导致头痛。