git 将部分更改从 "to be comitted" 移动到本地更改
git move part of changes from "to be comitted" to local changes
我有一个文件,我修改了 2 组更改:格式化和添加新功能。
我需要将它们变成 2 次提交。
- 格式更改
- 新功能 A.
现在我使用 git add --patch
并准备了所有格式设置。所以我有:
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: FileA <-- formatting changes
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: FileA <-- Feature A
现在在提交之前,我意识到我将一个函数(几行)用于格式更改(它应该位于功能更改中)。
我可以以某种方式编辑现在要提交的更改吗?该文件很重要,我不想用 git add --patch
.
重复一遍
我能想到的通过 git add --patch
省略 运行 的唯一解决方案是提交当前更改,然后从提交本身中删除这些行。
还有其他方法吗?
还有 git reset --patch
,它与 git add --patch
相反。
对于单个小的更改,使用 git gui
会更容易 - 您可以通过右键单击来交互 stage/unstage 单独的代码行。其他一些 Git GUI 可能具有类似的功能。
如果做不到这一点,这里有一个稍微难看的方法,无需再次检查所有内容:
git diff --cached >temp.diff
git reset
- 编辑
temp.diff
并删除包含您不想提交的更改的 hunk
git apply --cached --recount temp.diff
- 提交
我建议如下:
使用 git stash
(https://www.git-scm.com/docs/git-stash):
git stash --keep-index
这会将您当前的工作树推送到存储区并保留您的分阶段更改
注意未跟踪的文件。默认情况下,未跟踪的文件不会被推送到存储区! (还有另一种选择:--include-untracked
)
- 相应地更改
FileA
git add -p -- FileA
暂存您的新更改 - 您现在已经准备好第一次提交
git stash pop
或 git stash apply
(无论您是否要保留更改)
- 您的第二次提交现在几乎已在工作树中准备就绪(进行必要的更改、暂存、提交……)
我有一个文件,我修改了 2 组更改:格式化和添加新功能。
我需要将它们变成 2 次提交。
- 格式更改
- 新功能 A.
现在我使用 git add --patch
并准备了所有格式设置。所以我有:
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: FileA <-- formatting changes
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: FileA <-- Feature A
现在在提交之前,我意识到我将一个函数(几行)用于格式更改(它应该位于功能更改中)。
我可以以某种方式编辑现在要提交的更改吗?该文件很重要,我不想用 git add --patch
.
我能想到的通过 git add --patch
省略 运行 的唯一解决方案是提交当前更改,然后从提交本身中删除这些行。
还有其他方法吗?
还有 git reset --patch
,它与 git add --patch
相反。
对于单个小的更改,使用 git gui
会更容易 - 您可以通过右键单击来交互 stage/unstage 单独的代码行。其他一些 Git GUI 可能具有类似的功能。
如果做不到这一点,这里有一个稍微难看的方法,无需再次检查所有内容:
git diff --cached >temp.diff
git reset
- 编辑
temp.diff
并删除包含您不想提交的更改的 hunk git apply --cached --recount temp.diff
- 提交
我建议如下:
使用 git stash
(https://www.git-scm.com/docs/git-stash):
git stash --keep-index
这会将您当前的工作树推送到存储区并保留您的分阶段更改
注意未跟踪的文件。默认情况下,未跟踪的文件不会被推送到存储区! (还有另一种选择:--include-untracked
)- 相应地更改
FileA
git add -p -- FileA
暂存您的新更改 - 您现在已经准备好第一次提交git stash pop
或git stash apply
(无论您是否要保留更改)- 您的第二次提交现在几乎已在工作树中准备就绪(进行必要的更改、暂存、提交……)