The most common workflow is to commit changes to entire files. But it is not uncommon, in development, for a working copy of a file to end up with two or more sets of unrelated changes. Development can get messy. Even when this happens, we want the eventual commit history to represent a sequence of discrete logical changes.
Both VSCode and LazyGit support this.
The magic is in the --patch argument to git-add. This instructs Git to display all changes to the files specified on a hunk-by-hunk basis and lets you choose one of the following options for each hunk:
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
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
git add --patch is actually a shortcut to features in git add --interactive, a powerful front-end for managing all aspects of the staging area. The git-add manual page is a treasure trove of worthwhile information that’s often passed over due to the traditional semantics of VCS “add” commands. Remember that git-add does a lot more than just add stuff - it’s your interface for modifying the staging area.
The most common workflow is to commit changes to entire files. But it is not uncommon, in development, for a working copy of a file to end up with two or more sets of unrelated changes. Development can get messy. Even when this happens, we want the eventual commit history to represent a sequence of discrete logical changes.
Both VSCode and LazyGit support this.
The magic is in the
--patch
argument to git-add. This instructs Git to display all changes to the files specified on a hunk-by-hunk basis and lets you choose one of the following options for each hunk:git add --patch
is actually a shortcut to features ingit add --interactive
, a powerful front-end for managing all aspects of the staging area. The git-add manual page is a treasure trove of worthwhile information that’s often passed over due to the traditional semantics of VCS “add” commands. Remember thatgit-add
does a lot more than just add stuff - it’s your interface for modifying the staging area.