Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

git - 在Git中只提交文件的一部分(Commit only part of a file in Git)

When I make changes to a file in Git, how can I commit only some of the changes?

(当我在Git中对文件进行更改时,如何只提交一些更改?)

For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?

(例如,我怎样才能在文件中更改的30行中只提交15行?)

  ask by freddiefujiwara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use git add --patch <filename> (or -p for short), and git will begin to break down your file into what it thinks are sensible "hunks" (portions of the file).

(您可以使用git add --patch <filename> (或简称-p ),git将开始将您的文件分解为它认为合理的“帅哥”(文件的一部分)。)

It will then prompt you with this question:

(然后它会提示您这个问题:)

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

Here is a description of each option:

(以下是每个选项的说明:)

  • y stage this hunk for the next commit

    (y为下一次提交提供此块)

  • n do not stage this hunk for the next commit

    (n不要为下次提交暂存此块)

  • q quit;

    (Q退出;)

    do not stage this hunk or any of the remaining hunks

    (不要把这个大块头或任何剩下的帅哥放在一边)

  • a stage this hunk and all later hunks in the file

    (这个大块头和后来所有人都在文件中的一个阶段)

  • d do not stage this hunk or any of the later hunks in the file

    (d不要在文件中放置这个大块或任何后来的黑客)

  • g select a hunk to go to

    (g选择一个大块去)

  • / search for a hunk matching the given regex

    (/搜索与给定正则表达式匹配的块)

  • j leave this hunk undecided, see next undecided hunk

    (j离开这个大块未定,看下一个未定的大块头)

  • J leave this hunk undecided, see next hunk

    (J离开这个大块未定,看下一个大块头)

  • k leave this hunk undecided, see previous undecided hunk

    (k离开这个大块未定,看到先前未定的大块头)

  • K leave this hunk undecided, see previous hunk

    (K离开这个大块未定,看看上一个大块头)

  • s split the current hunk into smaller hunks

    (分裂当前大块成更小的厚片)

  • e manually edit the current hunk

    (e手动编辑当前的大块头)

  • ?

    ()

    print hunk help

    (打印大块帮助)

If the file is not in the repository yet, you can first do git add -N <filename> .

(如果文件尚未存储在存储库中,则可以先执行git add -N <filename> 。)

Afterwards you can go on with git add -p <filename> .

(之后你可以继续使用git add -p <filename> 。)

Afterwards, you can use:

(之后,您可以使用:)

  • git diff --staged to check that you staged the correct changes

    (git diff --staged以检查您是否已暂存正确的更改)

  • git reset -p to unstage mistakenly added hunks

    (git reset -p to unstage错误地添加了帅哥)

  • git commit -v to view your commit while you edit the commit message.

    (git commit -v在编辑提交消息时查看您的提交。)

Note this is far different than the git format-patch command, whose purpose is to parse commit data into a .patch files.

(请注意,这与git format-patch命令有很大不同,后者的目的是将提交数据解析为.patch文件。)

Reference for future: Git Tools - Interactive Staging

(未来参考: Git工具 - 交互式分段)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...