处于分离头状态时如何保存更改?
How to save changes when in detached-head state?
使用 Git,我不得不返回到特定的提交。我做了一些更改,现在我想提交它们。这样做的正确方法是什么?
我的项目现在处于分离头状态。如果我使用
进行提交,我的更改会被保存吗
git commit
?否则,我应该怎么做才能不丢失我的更改?
从此提交创建一个新分支,然后执行 commit
:
git checkout -b <branchname>
git commit
假设您已经上演(即 git add myfile1 myfile2
)您的文件。
免责声明:git并不复杂,只是用途广泛。不要因为我长篇大论就被吓跑了:)
你有:
master: a-b-c-d-e-f
并想改变 c
。你做到了:
* git checkout c
(避免将来检查提交。改为移动分支头)
* 更改了一些文件
您在:
master: a-b-c-d-e-f
\uncommitted-work,detached
如果您想在更改的基础上重新应用 d-e-f "c"
(如果你推送了,下游的人会有to recover from upstream rebase)
git stash .
git checkout master
git stash pop
(解决冲突)
git stage .
git commit -m "temporary name for g"
- (
master: a-b-c-d-e-f-g
)
git rebase c -i
("re-apply my current branch on to point c, and let me manipulate the commits interactively",即重新父级(变基)d-e-f
到新的 c
)
- 关注guide to interactive rebase。你想重新排序
g
所以它在 c 之后,然后将 rebase 命令从 pick
更改为 fixup
。 dd
删除一行,P
放置它,i
进入插入模式键入 "fixup" 然后 :wq
保存并退出 vim .
- (
master: a-b-c'-d'-e'-f'
,其中 c'
是您在变基期间合并 g
和 c
的结果。d-e-f
已变为 d'-e'-f'
因为他们的祖先已经改变,所以就 git 而言,他们不是 "same" 提交,但他们的内容保持不变)
如果您想撤消 d-e-f
(并重写历史,就好像您从未创造过一样)
(如果你推送了,下游的人会有to recover from upstream rebase) :
git stash .
git checkout master
- (
master: a-b-c-d-e-f
,隐藏文件最初基于 c)
git reset --hard c
(丢弃自 c 以来在 master 上的所有文件和提交)
- (
master: a-b-c
,有隐藏文件)
git stash pop
(解决冲突)
- (
master: a-b-c-*
)
git stage .
git commit -m "description of g"
- (
master: a-b-c-g
)
如果你想撤消 d-e-f(但将它们保留在历史记录中)
git stash
git revert --no-commit d
git revert --no-commit e
git revert --no-commit f
git push
git stash pop
(不会有冲突)
git stage .
git commit -m "Undo d-e-f in order to fix..."
git push
如果您有 git push
d-e-f,并且您希望将它们分开:
听起来您的新更改是针对 master 的新分支。 git branch <foo>
。
将您的工作保存在独立的头文件中:
git checkout -b <new-branch-name-for-detached-head> <sha-of-the-detached-head>
如果你想将它合并到 master:
git checkout master
git merge <new-branch-name-you-have-just-created>
如果您因为检出标签 (git checkout tags/<tag-name>
) 而处于 'detached HEAD' 状态,那么您想创建一个新分支以保留您创建的提交,方法是使用 -c 和开关命令。
git switch -c <new-branch-name>
使用 Git,我不得不返回到特定的提交。我做了一些更改,现在我想提交它们。这样做的正确方法是什么?
我的项目现在处于分离头状态。如果我使用
进行提交,我的更改会被保存吗git commit
?否则,我应该怎么做才能不丢失我的更改?
从此提交创建一个新分支,然后执行 commit
:
git checkout -b <branchname>
git commit
假设您已经上演(即 git add myfile1 myfile2
)您的文件。
免责声明:git并不复杂,只是用途广泛。不要因为我长篇大论就被吓跑了:)
你有:
master: a-b-c-d-e-f
并想改变 c
。你做到了:
* git checkout c
(避免将来检查提交。改为移动分支头)
* 更改了一些文件
您在:
master: a-b-c-d-e-f \uncommitted-work,detached
如果您想在更改的基础上重新应用 d-e-f "c"
(如果你推送了,下游的人会有to recover from upstream rebase)
git stash .
git checkout master
git stash pop
(解决冲突)git stage .
git commit -m "temporary name for g"
- (
master: a-b-c-d-e-f-g
) git rebase c -i
("re-apply my current branch on to point c, and let me manipulate the commits interactively",即重新父级(变基)d-e-f
到新的c
)- 关注guide to interactive rebase。你想重新排序
g
所以它在 c 之后,然后将 rebase 命令从pick
更改为fixup
。dd
删除一行,P
放置它,i
进入插入模式键入 "fixup" 然后:wq
保存并退出 vim . - (
master: a-b-c'-d'-e'-f'
,其中c'
是您在变基期间合并g
和c
的结果。d-e-f
已变为d'-e'-f'
因为他们的祖先已经改变,所以就 git 而言,他们不是 "same" 提交,但他们的内容保持不变)
如果您想撤消 d-e-f
(并重写历史,就好像您从未创造过一样)
(如果你推送了,下游的人会有to recover from upstream rebase) :
git stash .
git checkout master
- (
master: a-b-c-d-e-f
,隐藏文件最初基于 c) git reset --hard c
(丢弃自 c 以来在 master 上的所有文件和提交)- (
master: a-b-c
,有隐藏文件) git stash pop
(解决冲突)- (
master: a-b-c-*
) git stage .
git commit -m "description of g"
- (
master: a-b-c-g
)
如果你想撤消 d-e-f(但将它们保留在历史记录中)
git stash
git revert --no-commit d
git revert --no-commit e
git revert --no-commit f
git push
git stash pop
(不会有冲突)git stage .
git commit -m "Undo d-e-f in order to fix..."
git push
如果您有 git push
d-e-f,并且您希望将它们分开:
听起来您的新更改是针对 master 的新分支。 git branch <foo>
。
将您的工作保存在独立的头文件中:
git checkout -b <new-branch-name-for-detached-head> <sha-of-the-detached-head>
如果你想将它合并到 master:
git checkout master
git merge <new-branch-name-you-have-just-created>
如果您因为检出标签 (git checkout tags/<tag-name>
) 而处于 'detached HEAD' 状态,那么您想创建一个新分支以保留您创建的提交,方法是使用 -c 和开关命令。
git switch -c <new-branch-name>