我在a
分行。我希望在分支b
上提交,以便克隆分支b
的人拥有与我现在相同的工作目录。
保存当前更改不起作用,因为在某些情况下,这会导致冲突。
我正在寻找等效于创建工作目录的临时副本、调用git checkout -f b
、删除所有文件、将临时目录复制到项目目录并进行提交。
发布于 2019-03-29 01:51:30
重启--如果你的朋友。如果希望B之后的修订成为工作树上的方式(目前尚未提交),则可以这样做:
git checkout --detach # disconnect from A
git reset --soft b # set the branch pointer to whatever revision B is pointing to.... Your working tree will be unaffected
git add . # add everything to index
git commit -m "revision after B that made it look the way I had it where I was working"
# if you like everything, move b branch to this revision and push
git branch -f b
git checkout b
git push some-remote b
那应该行。
发布于 2019-03-29 00:39:11
任何提交本质上都是“工作目录的临时副本”。
例如,您所指的“更改”实际上是在git show <commit>
时从快照中为您生成的。
所以很难直接回答你的问题。让我们试试这两种可能的途径:
分支b
的历史重写的
如果您希望在此时在分支b
上提交反映分支a
上的确切状态,那么为什么不直接将分支b指向它应该在的位置。就像这样:
# get the uncommited changes on the branch
git commit -am "Useful message"
# point b where a is now
git branch -f b a
# instead of forcing the branch we could merge with a strategy ours
# but it's hard to tell what you need from your description alone
# reset a to its previous state
git reset --hard HEAD^
初始状态:
C1---C2 <<< a <<< HEAD # with a dirty tree, uncommited changes
? <<< b # (you didn't mention where b was pointing)
事后结果
C1---C2 <<< a <<< HEAD
\
C3 <<< b # where your last (previously uncommited) changes are
当它重写分支的历史时,应该仔细考虑它,并可能排除是否共享该分支。尽管如此,它还是满足了您的要求:“克隆分支b的人拥有与我现在相同的工作目录”。
不重写分支b
历史的
为了避免重写分支b
的历史,另一种方法是将a合并为b,并使用一种策略,该策略将从a
端获取所有内容,而不仅仅是冲突的部分。会是这样的:
# we work on a copy of branch a
git checkout -b a-copy a
# get the uncommited changes on the branch
git commit -am "Useful message"
# proceed with the merge, taking nothing from b
git merge -s ours b
# we now reflect the merge on b, and this is a fast-forward
git checkout b
git merge a-copy
# no further need for the working copy of a
git branch -D a-copy
a
不需要被重置,因为它在任何时候都没有移动。
在第一次提交之后:
C1---C2 <<< a
\
C3 <<< a-copy <<< HEAD
o <<< b (anywhere in the tree)
在第一次合并之后:
C1---C2 <<< a
\
C3 (same state as a, plus the previously uncommited changes)
\
C4 <<< a-copy <<< HEAD (the merge taking only C3 side)
/
o <<< b (anywhere in the tree)
结束状态:
C1---C2 <<< a
\
C3 (same state as a, plus the previously uncommited changes)
\
C4 <<< b <<< HEAD
/
o
https://stackoverflow.com/questions/55408838
复制相似问题