Git Add with a Prompt: Selectively Stage Files in Terminal

Git Add with a Prompt: Selectively Stage Files in Terminal

Hello everyone,

Have you ever needed to stage only specific files in Git but found it tedious to manually copy and paste file paths, especially when they are in different folders? If so, I have a simple solution for you!

I’ve created a Bash script that streamlines this process by prompting you, file by file, and letting you decide whether to stage each one by simply entering 'y' (yes) or 'n' (no).

This eliminates the need to repeatedly run git status and git add with manually copied file paths.

Let's dive in!

Note: While many IDEs offer selective staging, this script is a great alternative for those who prefer working in the terminal. It will save you time and effort.

The Script

Here’s a Bash script that asks you whether to stage each modified file:

#!/bin/bash

# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    echo "Not a Git repository. Please navigate to a Git repository."
    exit 1
fi

# Get the list of modified files
modified_files=$(git diff --name-only)

# Check if there are any modified files
if [ -z "$modified_files" ]; then
    echo "No modified files to stage."
    exit 0
fi

# Loop through each modified file
for file in $modified_files; do
    read -p "Do you want to stage the file '$file'? (y/n): " answer
    if [[ $answer == "y" ]]; then
        git add "$file"
        echo "'$file' has been staged."
    else
        echo "'$file' has not been staged."
    fi
done

Adding the Script to System Variables

To make this script accessible from anywhere in the terminal, add its path to your system variables.

Steps for Windows Users

  1. Right-click "This PC" or "Computer" in File Explorer.

  2. Click "Properties""Advanced system settings""Environment Variables".

  3. You can also search for "Edit environment variables for your account" in Windows.

  4. Under System Variables, find and double-click "Path".

  5. Click "New", then add the folder path where your script is saved (e.g., C:\customScripts).

  6. Click OK to save the changes.

Creating a Global Alias in Git Bash

To make running the script even easier, you can create a global alias in Git Bash.

Step 1: Open Git Bash and Edit .bashrc File

Run the following command

nano ~/.bashrc

(If nano is not available, use another text editor like vim.)

Step 2: Add Your Alias

At the end of the .bashrc file, add this line:

alias stage-files='bash /c/customScripts/git_stage_files.sh'

Note: In Git Bash, Windows paths use forward slashes (/), and C: drive paths start with /c/.

Step 3: Save and Exit

  • If using nano, press CTRL + X and then Yes to save

  • If using vim, press Esc, type :wq, and hit Enter.

Step 4: Reload the .bashrc File

Run the following command to apply the changes:

source ~/.bashrc

How to Use the Script

Now, whenever you want to selectively stage files, just run:

stage-files

This will list all modified files and ask you whether to stage each one.

Conclusion

This simple script makes it easy to stage specific files in Git without manually copying file paths. If you're a terminal user, this method will help you work more efficiently.

If you encounter any issues, feel free to ask in the comments section.

Happy coding! 🚀

Did you find this article valuable?

Support Siddhant Bytes by becoming a sponsor. Any amount is appreciated!