How to commit only part of a files changes in git

Posted on

In Git, it’s possible to commit only part of a file’s changes using the git add -p (or git add --patch) command, which allows you to interactively stage portions of a file. This feature is particularly useful when you have multiple changes in a file but only want to commit some of them, helping maintain clean and focused commit histories.

Using git add -p

Interactive Staging: The git add -p command breaks down the changes in your working directory into a series of smaller hunks and presents them to you one by one. You can choose to stage or skip each hunk, giving you precise control over which changes are included in the next commit.

git add -p

Prompt Options: When running git add -p, you will be presented with several options for each hunk:

  • y to stage the hunk.
  • n to skip the hunk.
  • s to split the hunk into smaller pieces.
  • e to manually edit the hunk.
  • q to quit the process.
  • ? to view help for the available options.

Example: Suppose you modified a file example.txt and want to commit only part of those changes. Running git add -p example.txt will start the interactive process.

git add -p example.txt

Interactive Example: The following sequence shows how to stage changes interactively.

diff --git a/example.txt b/example.txt
index 83db48f..f735fef 100644
--- a/example.txt
+++ b/example.txt
@@ -1,5 +1,5 @@
-Hello World
+Hello Universe
 This is a test file.
-Another line of text.
+Modified line of text.

For each hunk, you can decide whether to stage it or not. This process repeats until all changes are reviewed.

Using git add -e

Manual Editing: If the predefined hunks are not suitable, you can use git add -e to manually edit the changes you want to stage. This opens the changes in your default editor, allowing you to select exactly which lines to stage.

git add -e example.txt

Editing Example: In your editor, you will see the diff of changes. You can edit the diff to include only the changes you want to stage.

--- a/example.txt
+++ b/example.txt
@@ -1,5 +1,5 @@
-Hello World
+Hello Universe
 This is a test file.
-Another line of text.
+Modified line of text.

Remove the lines you do not want to stage, save, and close the editor.

Using GUIs and IDEs

GUI Tools: Many Git GUI tools, such as GitKraken, Sourcetree, and GitHub Desktop, provide graphical interfaces to stage partial changes. These tools often offer drag-and-drop or checkbox interfaces to select specific lines or hunks to stage.

IDEs: Integrated Development Environments (IDEs) like Visual Studio Code, IntelliJ IDEA, and Eclipse also have built-in support for staging partial changes. These environments typically highlight modified lines, allowing you to stage them individually.

Visual Studio Code Example: In VS Code, open the Source Control panel, view the changes, and use the Stage Change button next to each modified line or hunk.

Best Practices

Focused Commits: Using git add -p helps create focused commits that address a single concern or issue. This makes your commit history easier to understand and review.

Commit Messages: Write clear and descriptive commit messages that accurately describe the changes included in the commit. This practice complements the focused nature of your commits.

Review Before Committing: Always review the staged changes with git diff --cached before committing. This ensures that only the intended changes are included.

git diff --cached

Avoid Large Commits: Try to avoid large, monolithic commits that include unrelated changes. Break them down into smaller, logical commits using partial staging.

Advanced Techniques

Interactive Rebase: For more advanced use cases, you can use git rebase -i (interactive rebase) to edit commits after they have been created. This allows you to split, reorder, or combine commits, providing another way to manage changes precisely.

git rebase -i HEAD~n  # Replace 'n' with the number of commits to review

Amending Commits: If you need to add changes to the most recent commit, use git commit --amend. Combine this with git add -p to selectively add changes to the latest commit.

git add -p
git commit --amend

Stashing Partial Changes: You can also use git stash -p to stash only part of the changes, allowing you to work on other tasks without losing your current modifications.

git stash -p

Summary

Precision in Version Control: Using git add -p and related techniques helps maintain precision in your version control practices. By committing only the relevant parts of your changes, you keep your commit history clean, logical, and easy to understand.

Flexibility: The flexibility provided by these tools and commands ensures that you can manage your changes effectively, whether working from the command line or using a GUI.

Integration with Workflows: Integrate these practices into your daily workflow to improve code quality, simplify code reviews, and enhance collaboration within your team.

👎 Dislike