in Technology by
How to Commit only part of a file in Git?

1 Answer

0 votes
by
 
Best answer

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.

Related questions

0 votes
    What is the Git command to blow away all the changes in a file in the working area, since the previous commit? A. ... git log filename C. git checkout filename D. git rm filename...
asked Dec 21, 2022 in Technology by JackTerrance
0 votes
    Point out the correct statement. (a) shiny project is a directory containing at least three parts (b) shiny project ... directory containing only one part (d) none of the mentioned...
asked Oct 9, 2021 in Technology by JackTerrance
0 votes
    How to revert a Git repository to a previous commit?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to modify existing, unpushed commit messages in git?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How do I undo 'git add' before commit?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How will you find out what all files have been changed in a particular Git commit?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    In Git how do you revert a commit that has already been pushed and made public?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    As asked in this question, I also want to know how to resolve a conflicting git stash pop without ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    How do I revert from my current state to a snapshot made on a certain commit? If I do git log, then I get ... , i.e. commit 0d1d7fc? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    What is the Git command to move branch pointer to different commit without checkout? A. Git cherrypick B. Git update-ref C. Git reset head...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    git bisect is used to identify a commit that has introduced a bug. A. True B. False...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    What is the Git command to skip staging and commit the changes directly? A. git skip stage B. git commit C. git commit -a -m “message” D. git add changes...
asked Dec 21, 2022 in Technology by JackTerrance
0 votes
    What is the Git command to view all the changes since the last commit ? A. git clean B. git status C. git changes D. git commit...
asked Dec 21, 2022 in Technology by JackTerrance
0 votes
    What is mean by cherry-picking a commit in Git ?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    Move the most recent commit(s) to a new branch with Git?...
asked Jan 7, 2021 in Technology by JackTerrance
...