Commit only part of a file in Git

后端 未结 23 2311
一整个雨季
一整个雨季 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:27

    If it's on Windows platform, in my opinion git gui is very good tool to stage/commit few lines from unstaged file

    1. Hunk wise:

    • Select the file from unstagged Changes section
    • Right click chunk of code which needs to be staged
    • Select Stage Hunk for commit

    2. Line wise:

    • Select the file from unstagged Changes section
    • Select the line/lines to be staged
    • Right click and select Stage Lines for commit

    3. If you want to stage the complete file except couple of lines:

    • Select the file from unstagged Changes section
    • Press Ctrl+T (Stage file to commit)
    • Selected file now moves to Staged Changes Section
    • Select the line/lines be staged
    • Right click and select UnStage Lines for commit

提交回复
热议问题