Commit only part of a file in Git

后端 未结 23 1906
一整个雨季
一整个雨季 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-22 06:32

    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). 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
    • n do not stage this hunk for the next commit
    • q quit; 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
    • 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 hunk help

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

    Afterwards, you can use:

    • git diff --staged to check that you staged the correct changes
    • git reset -p to unstage mistakenly added hunks
    • git commit -v to view your commit while you edit the commit message.

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

    Reference for future: Git Tools - Interactive Staging

    0 讨论(0)
  • 2020-11-22 06:32

    Worth noting that to use git add --patch for a new file you need to first add the file to index with git add --intent-to-add:

    git add -N file
    git add -p file
    
    0 讨论(0)
  • 2020-11-22 06:35

    It's been 10 years since this question was asked. And I hope this answer will be useful to someone. As mentioned in the answer here, where GUI is not an option, Andrew Shadura's crecord extension helps bring a ncurses window in which we can select the lines to commit.

    Set up the extension as follows:

    git clone https://github.com/andrewshadura/git-crecord
    cd git-crecord
    ./setup.py install
    ln -s $PWD/git-crecord ~/.local/bin/git-crecord
    

    cd to your git repo and invoke it as follows:

    git crecord
    

    This would bring up the ncurses interface which can be used as shown below. Pressing the following keys in the ncurses window will do certain actions:

    f       hunk toggle fold (arrow keys can also be used)
    space   toggle hunk selection
    a       toggle commit or amend
    c       confirm and open commit window
    

    Screencast showing a sample usageExample

    0 讨论(0)
提交回复
热议问题