Skip to content

Instantly share code, notes, and snippets.

@roger-mo-gusto
Last active April 19, 2020 18:28
Show Gist options
  • Save roger-mo-gusto/d2b89f4acfa0585661ee2c9e36edb453 to your computer and use it in GitHub Desktop.
Save roger-mo-gusto/d2b89f4acfa0585661ee2c9e36edb453 to your computer and use it in GitHub Desktop.
Git Add, Reset, and Checkout Shortcuts
RED='\e[31m'
GREEN='\e[32m'
CYAN='\e[36m'
BLUE='\e[34m'
LIGHTBLUE="\e[94m"
PURPLE='\e[1;35m'
NC='\e[0m'
Sample usage:
###################### ADD ######################
~/workspace/zenpayroll development ⚡ add
Which files would you like to add?
[0]: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
[1]: engines/consumer_banking/app/services/consumer_banking/core_pro_service/transaction.rb
[2]: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
[3]: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
>> 0 2 3
Performing git add on file engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
Performing git add on file frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
Performing git add on file frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
On branch development
Your branch is up to date with 'origin/development'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
modified: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
modified: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: engines/consumer_banking/app/services/consumer_banking/core_pro_service/transaction.rb
~/workspace/zenpayroll development ⚡
###################### Checkout ######################
~/workspace/zenpayroll development ⚡ gof
Which files would you like to checkout?
[0]: engines/consumer_banking/app/services/consumer_banking/core_pro_service/transaction.rb
[1]: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
>> 0
Performing git checkout on file engines/consumer_banking/app/services/consumer_banking/core_pro_service/transaction.rb
Updated 1 path from the index
On branch development
Your branch is up to date with 'origin/development'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
modified: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
~/workspace/zenpayroll development ⚡
###################### Reset ######################
~/workspace/zenpayroll development ⚡ greset
Which files would you like to reset -q?
[0]: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
[1]: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
[2]: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
>> 2
Performing git reset -q on file frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
On branch development
Your branch is up to date with 'origin/development'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
modified: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: engines/consumer_banking/app/services/consumer_banking/core_pro_service/transaction.rb
modified: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
~/workspace/zenpayroll development ⚡
###################### Usage of . ######################
~/workspace/zenpayroll development ⚡ greset
Which files would you like to reset -q?
[0]: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
[1]: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
>> .
Performing git reset -q on file engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
Performing git reset -q on file frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
On branch development
Your branch is up to date with 'origin/development'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: engines/banking_bridge/app/graphql/banking_bridge/objects/savings.rb
modified: frontend/javascripts/components/cash_reserve/routers/cash_reserve_withdraw_router.jsx
modified: frontend/javascripts/spec/components/cash_reserve/shared_components/transactions_table_spec.jsx
no changes added to commit (use "git add" and/or "git commit -a")
~/workspace/zenpayroll development ⚡
gof() {
possible_files="$(git ls-files -m --exclude-standard)"
command_name='checkout -q'
run_git_command_on_files
unset possible_files
}
greset() {
possible_files="$(git diff --cached --name-only)"
command_name='reset -q'
run_git_command_on_files
unset possible_files
}
add() {
possible_files="$(git ls-files -m -o --exclude-standard)"
command_name='add'
run_git_command_on_files
unset possible_files
}
# RED='\033[1;31m'
# GREEN='\033[0;32m'
# BLUE='\033[1;34m'
# LIGHTBLUE="\033[1;36m"
# PURPLE='\033[1;35m'
# NC='\033[0m'
RED='\e[31m'
GREEN='\e[32m'
CYAN='\e[36m'
BLUE='\e[34m'
LIGHTBLUE="\e[94m"
PURPLE='\e[1;35m'
NC='\e[0m'
run_git_command_on_files() {
#!/bin/bash
regex="(.*)"
filenames=()
file_count=0
files_to_operate_on=()
if [[ "$possible_files" =~ $regex ]]
then
for MATCHED in $BASH_REMATCH
do
if [[ "$MATCHED" =~ (.*\..*) ]]
then
filenames+=("${MATCHED}")
file_count=$((file_count+1))
fi
done
if [[ file_count -eq 0 ]]
then
printf "${RED}No files to ${command_name}! \n${NC}"
else
if [[ file_count -eq 1 ]]
then
files_to_operate_on="$filenames"
else
echo "Which files would you like to ${command_name}?"
echo ""
file_count=0
local filename_regex="[^/]+$"
local filepath_regex="(.*)\/"
for FILE_WITH_PATH in "${filenames[@]}"
do
local filename=$(echo "${FILE_WITH_PATH}" | grep -E -o "${filename_regex}")
local filepath=$(echo "${FILE_WITH_PATH}" | grep -E -o "${filepath_regex}")
printf "[${GREEN}${file_count}${NC}]: ${BLUE}${filename}${NC} - ${filepath}\n"
file_count=$((file_count+1))
done
echo ""
while true; do
read -p ">> " file_indeces
num_files_to_operate_on=0
if [ "$file_indeces" = "q" ]
then
exit 0
elif [ "$file_indeces" = '.' ]
then
for FNAME in "${filenames[@]}"
do
files_to_operate_on+=("${FNAME}")
num_files_to_operate_on=$((num_files_to_operate_on+1))
done
else
for file_index in $file_indeces
do
if [ $file_index -eq $file_index 2>/dev/null -o $file_index -eq 0 2>/dev/null ]
then
if [[ "$file_index" -le "$file_count" ]]
then
files_to_operate_on+=("${filenames[$file_index]}")
num_files_to_operate_on=$((num_files_to_operate_on+1))
fi
fi
done
fi
if [[ num_files_to_operate_on -gt 0 ]]
then
break
fi
done
fi
for filename in "${files_to_operate_on[@]}"
do
printf "${PURPLE}Performing git ${command_name} on file ${filename}${NC}\n"
eval "git ${command_name} $filename"
done
eval "git status"
fi
fi
unset regex
unset filenames
unset file_count
unset file_indeces
unset file_index
unset file_to_add
unset files_to_operate_on
unset num_files_to_operate_on
unset FNAME
}
gof() {
possible_files="$(git ls-files -m --exclude-standard)"
command_name='checkout -q'
run_git_command_on_files
unset possible_files
}
gset() {
possible_files="$(git diff --cached --name-only --relative)"
command_name='reset -q'
run_git_command_on_files
unset possible_files
}
add() {
possible_files="$(git ls-files -m -o --exclude-standard)"
command_name='add'
run_git_command_on_files
unset possible_files
}
dif() {
possible_files="$(git ls-files -m -o --exclude-standard)"
command_name='diff'
run_git_command_on_files
unset possible_files
}
difc() {
possible_files="$(git diff --cached --name-only --relative)"
command_name='diff --cached'
run_git_command_on_files
unset possible_files
}
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment