Commit only part of a file in Git

后端 未结 23 2382
一整个雨季
一整个雨季 2020-11-22 05:50

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

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

23条回答
  •  暖寄归人
    2020-11-22 06:25

    I believe that git add -e myfile is the easiest way (my preference at least) since it simply opens a text editor and lets you choose which line you want to stage and which line you don't. Regarding editing commands:

    added content:

    Added content is represented by lines beginning with "+". You can prevent staging any addition lines by deleting them.

    removed content:

    Removed content is represented by lines beginning with "-". You can prevent staging their removal by converting the "-" to a " " (space).

    modified content:

    Modified content is represented by "-" lines (removing the old content) followed by "+" lines (adding the replacement content). You can prevent staging the modification by converting "-" lines to " ", and removing "+" lines. Beware that modifying only half of the pair is likely to introduce confusing changes to the index.

    Every details about git add are available on git --help add

提交回复
热议问题