0

我对存储库的工作副本进行了一些更改。我很久没有承诺了,所有的变化都在跟踪中。

现在,我想先提交一部分更改,然后再提交另一部分更改,以便我可以恢复到每个更改。显然,这些部分的更改是在不同的文件上。我是 git 的新手,因此担心如果我尝试一些其他地方建议的 git rm --cache 之类的东西,我可能会撤消所有更改。请帮忙。

4

6 回答 6

3

你可以 git gui 和 'stage hunk for commit' 或 'stage line for commit'

于 2012-04-23T11:52:20.313 回答
3

在命令行上,您可以使用

$ git add -p

选择提交的帅哥。

于 2012-04-23T11:58:24.987 回答
2

我建议使用-p选项来git add. 这告诉 git 以交互方式询问用户是否提交一个大块。

$ git add -p src_file.c
$ git commit
$ git add -p src_file.c
$ git commit
...

使用此选项,您将收到如下提示:

Stage this hunk [y,n,q,a,d,/,s,e,?]?

?可获得以下帮助信息。

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

像这样添加,您可以轻松地将对同一文件或多个文件的更改拆分为多个提交。有关更详细的描述,请查看man (1) git-add

于 2012-04-23T12:02:46.377 回答
1

也许我不明白这个问题,但只需添加一些文件并进行提交,然后重复该过程。

git add file1 file2
git commit -m "Comment first commit comment"
git add file3 ...
于 2012-04-23T11:55:04.513 回答
1

git add您可以使用命令将文件添加到提交中。例如 type git add file.c,则将file.c与 next 一起提交git commit。任何未添加的文件都不会被提交。提交后只需添加其他文件并再次提交。

您可以通过键入来查看为提交暂存了哪些文件git status。要添加所有文件,请键入git add .

于 2012-04-23T11:53:29.470 回答
0

如果要提交修改文件子集中的所有更改,请使用git add <file1> .... 如果您想选择将文件中的哪些更改添加到索引中,请使用git add -i [<file1> ...]更有用的 IMHO 或 IMHO git add -p [<file1> ...],它甚至允许您修改应用于索引的差异,而无需更改工作副本中的状态。

最后,如果你是Vim用户,你绝对必须知道并使用fugitive

于 2012-04-23T12:00:28.823 回答