DEV Community

Keyur Ramoliya
Keyur Ramoliya

Posted on

Git - Interactive Add for Precise Staging

When preparing your changes for a commit in Git, the git add command is used to stage files. However, there might be scenarios where you want to stage only specific lines or chunks of code within a file rather than the entire file. Git provides a feature called "interactive add" that allows you to stage specific changes interactively.

Here's how to use Git's interactive add:

  • Make Changes: First, make your changes to the file as you normally would.

  • Check the Changes: To see a summary of your changes, you can use the git diff command:

   git diff
Enter fullscreen mode Exit fullscreen mode

This command shows the modifications you've made since the last commit.

  • Start Interactive Add: To stage-specific changes interactively, use the following command:
   git add -i
Enter fullscreen mode Exit fullscreen mode

This command starts an interactive add session.

  • Select Changes to Stage: In the interactive add session, you'll be presented with a menu that allows you to choose which changes to stage. You can use options like 1: patch, 2: update, and more to interactively select the changes you want to stage.

  • Stage Selected Changes: Once you've chosen the changes to stage, Git will add them to the staging area.

  • Commit: After staging the changes you want, you can proceed to commit them as usual with git commit.

Interactive add gives you fine-grained control over which changes to include in your commit. It's particularly useful when you've made multiple unrelated changes within the same file or when you want to split a file's changes into multiple commits. This level of control helps you keep your commits more focused and logically organized.

Top comments (0)