Commit only part of a file in Git

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

    You can use git add --interactive or git add -p <file>, and then git commit (not git commit -a); see Interactive mode in git-add manpage, or simply follow instructions.

    Modern Git has also git commit --interactive (and git commit --patch, which is shortcut to patch option in interactive commit).

    If you prefer doing it from GUI, you can use git-gui. You can simply mark chunks which you want to have included in commit. I personally find it easier than using git add -i. Other git GUIs, like QGit or GitX, might also have this functionality as well.

提交回复
热议问题