首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改当前分支并保持本地更改

更改当前分支并保持本地更改
EN

Stack Overflow用户
提问于 2019-03-29 00:30:54
回答 2查看 413关注 0票数 3

我在a分行。我希望在分支b上提交,以便克隆分支b的人拥有与我现在相同的工作目录。

保存当前更改不起作用,因为在某些情况下,这会导致冲突。

我正在寻找等效于创建工作目录的临时副本、调用git checkout -f b、删除所有文件、将临时目录复制到项目目录并进行提交。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-29 01:51:30

重启--如果你的朋友。如果希望B之后的修订成为工作树上的方式(目前尚未提交),则可以这样做:

代码语言:javascript
运行
复制
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

那应该行。

票数 2
EN

Stack Overflow用户

发布于 2019-03-29 00:39:11

任何提交本质上都是“工作目录的临时副本”。

例如,您所指的“更改”实际上是在git show <commit>时从快照中为您生成的。

所以很难直接回答你的问题。让我们试试这两种可能的途径:

分支b的历史重写的

如果您希望在此时在分支b上提交反映分支a上的确切状态,那么为什么不直接将分支b指向它应该在的位置。就像这样:

代码语言:javascript
运行
复制
# 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^

初始状态:

代码语言:javascript
运行
复制
C1---C2 <<< a <<< HEAD # with a dirty tree, uncommited changes

      ? <<< b # (you didn't mention where b was pointing)

事后结果

代码语言:javascript
运行
复制
C1---C2 <<< a <<< HEAD
     \
      C3 <<< b # where your last (previously uncommited) changes are

当它重写分支的历史时,应该仔细考虑它,并可能排除是否共享该分支。尽管如此,它还是满足了您的要求:“克隆分支b的人拥有与我现在相同的工作目录”。

不重写分支b历史的

为了避免重写分支b的历史,另一种方法是将a合并为b,并使用一种策略,该策略将从a端获取所有内容,而不仅仅是冲突的部分。会是这样的:

代码语言:javascript
运行
复制
# 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不需要被重置,因为它在任何时候都没有移动。

在第一次提交之后:

代码语言:javascript
运行
复制
C1---C2 <<< a
     \
      C3 <<< a-copy <<< HEAD

      o <<< b (anywhere in the tree)

在第一次合并之后:

代码语言:javascript
运行
复制
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)

结束状态:

代码语言:javascript
运行
复制
C1---C2 <<< a
     \
      C3 (same state as a, plus the previously uncommited changes)
       \
        C4 <<< b <<< HEAD
       /
      o
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55408838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档