Skip to content

Instantly share code, notes, and snippets.

@gillesB
Last active January 18, 2019 09:37
Bash script to add a file by its index to the git staging area.
#!/usr/bin/bash
git_status=`git status --porcelain`
if [[ $# -ne 1 ]]
then
# Get modified files and their index
# nl: numbers the lines
echo "$git_status" | nl
exit 0
fi
if ! [[ "$1" =~ ^[0-9]+$ ]]
then
echo "Sorry integers only"
exit 1
fi
line=`echo "$git_status" | sed "${1}q;d"`
# remove first 4 chars. E.g. M and some spaces
file=`echo "$line" | cut -c3-`
git add $file
git status -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment