我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
master A - B - C - D - E
为此:
newbranch C - D - E
/
master A - B
我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
master A - B - C - D - E
为此:
newbranch C - D - E
/
master A - B
当前回答
这不是在技术上“移动”它们,但它有相同的效果:
A--B--C (branch-foo)
\ ^-- I wanted them here!
\
D--E--F--G (branch-bar)
^--^--^-- Opps wrong branch!
While on branch-bar:
$ git reset --hard D # remember the SHAs for E, F, G (or E and G for a range)
A--B--C (branch-foo)
\
\
D-(E--F--G) detached
^-- (branch-bar)
Switch to branch-foo
$ git cherry-pick E..G
A--B--C--E'--F'--G' (branch-foo)
\ E--F--G detached (This can be ignored)
\ /
D--H--I (branch-bar)
Now you won't need to worry about the detached branch because it is basically
like they are in the trash can waiting for the day it gets garbage collected.
Eventually some time in the far future it will look like:
A--B--C--E'--F'--G'--L--M--N--... (branch-foo)
\
\
D--H--I--J--K--.... (branch-bar)
其他回答
如果你是像我这样的人,你正在使用Visual Studio,那么你可以这样做:在我的情况下,我想把最新的承诺交给另一个分支。
右键单击前一个(委托)。
此分類上一篇
因此,所有承诺变更将出现在 Git 变更板上。
此分類上一篇
到您的目标分支或从右下角创建一个新的分支。
此分類上一篇
在“Git Changes”中,双击您的最新 Stash. “Stash details” 面板将被打开. 点击“Pop”,然后解决冲突(如果存在)。
此分類上一篇
最后,做你的改变。
最简单的方式就是这样做:
任命主分支到您的新分支(假设您在主分支):
git branch -m newbranch
2、从你想要的承诺中创建主分支:
git checkout -b master <seven_char_commit_id>
例如 git checkout -b master a34bc22
注意:新行业的上流将是起源/主流。
只有在这种情况下:
Branch one: A B C D E F J L M
\ (Merge)
Branch two: G I K N
我表演了:
git branch newbranch
git reset --hard HEAD~8
git checkout newbranch
我希望承诺我会成为头,但承诺是现在的。
要确保在历史上登上正确的位置,就更容易与承诺的哈希工作。
git branch newbranch
git reset --hard #########
git checkout newbranch
如果你想将你的承诺转移到一个现有分支,它会看起来如下:
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 选项,以避免失去所承担的承诺。
1) 创建一个新的分支,将所有更改转移到 new_branch。
git checkout -b new_branch
(二)然后回到旧的分支。
git checkout master
3、做吉·雷巴斯
git rebase -i <short-hash-of-B-commit>
4) 然后打开的编辑器包含最后3个承诺信息。
...
pick <C's hash> C
pick <D's hash> D
pick <E's hash> E
...
5) 更改选项下载所有这些 3 任务. 然后保存并关闭编辑器。
...
drop <C's hash> C
drop <D's hash> D
drop <E's hash> E
...
6) 现在最后3个命令从当前分支(主)中移除。 现在强烈推分支,在分支名称之前 + 标志。
git push origin +master