alias ll="ls -lah"
alias branch="git branch"
alias checkout="git checkout"
add() {
# Colors
GREEN="\033[32m"
RED="\033[31m"
YELLOW="\033[33m"
BLUE="\033[34m"
RESET="\033[0m"
# Arrays for summary
added_files=()
skipped_files=()
# Get changed files (including renamed, deleted, etc.)
mapfile -t files < <(git status --porcelain | sed 's/^...//' )
if [ ${#files[@]} -eq 0 ]; then
echo -e "${GREEN}No changed files to add.${RESET}"
return
fi
echo -e "${YELLOW}Interactive add: y = add, n = skip, d = show diff${RESET}"
for file in "${files[@]}"; do
while true; do
printf "${BLUE}Add %s?${RESET} [y/n/d]: " "$file"
read -r answer
case "$answer" in
y|Y )
git add -- "$file"
added_files+=("$file")
break
;;
n|N )
skipped_files+=("$file")
break
;;
d|D )
echo -e "${YELLOW}--- Diff for $file ---${RESET}"
git diff -- "$file"
echo -e "${YELLOW}----------------------${RESET}"
;;
* )
echo -e "${RED}Invalid input, please choose y, n, or d.${RESET}"
;;
esac
done
done
# Summary section
echo -e "\n${GREEN}Added files:${RESET}"
if [ ${#added_files[@]} -eq 0 ]; then
echo "(None)"
else
printf '%s\n' "${added_files[@]}"
fi
echo -e "\n${RED}Skipped files:${RESET}"
if [ ${#skipped_files[@]} -eq 0 ]; then
echo "(None)"
else
printf '%s\n' "${skipped_files[@]}"
fi
echo -e "\n${YELLOW}Done.${RESET}"
}
commit() {
echo "Enter commit message (Ctrl+D to finish):"
msg=$(cat)
git commit -m "$msg"
}
git_push() {
# Colors
RED="\033[0;31m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
git fetch origin "$CURRENT_BRANCH" >/dev/null 2>&1
echo -e "${CYAN}Commits to push on $CURRENT_BRANCH:${NC}"
git log origin/$CURRENT_BRANCH..$CURRENT_BRANCH --oneline || echo "No upstream set yet."
echo
BEHIND_COUNT=$(git rev-list --count $CURRENT_BRANCH..origin/$CURRENT_BRANCH 2>/dev/null || echo 0)
if [ "$BEHIND_COUNT" -gt 0 ]; then
echo -e "${YELLOW}⚠ Your branch is behind origin by $BEHIND_COUNT commit(s).${NC}"
read -p "Auto rebase with 'git pull --rebase'? (Y/n): " REBASE_CONF
REBASE_CONF=${REBASE_CONF:-Y}
if [[ "$REBASE_CONF" =~ ^[Yy]$ ]]; then
echo -e "${CYAN}Rebasing...${NC}"
if ! git pull --rebase origin "$CURRENT_BRANCH"; then
echo -e "${RED}❌ Rebase failed. Resolve conflicts and retry.${NC}"
return 1
fi
else
echo -e "${RED}Push cancelled because branch is behind.${NC}"
return 1
fi
fi
if [[ "$CURRENT_BRANCH" == "main" || "$CURRENT_BRANCH" == "master" ]]; then
echo -e "${RED}⚠ You are on ${CURRENT_BRANCH}. Be careful!${NC}"
read -p "Type YES to confirm push: " CONFIRM
if [[ "$CONFIRM" != "YES" ]]; then
echo -e "${YELLOW}Push cancelled.${NC}"
return 1
fi
fi
printf "Enter remote branch name (or press Enter to use %s): " "$CURRENT_BRANCH"
read USER_BRANCH
if [ -z "$USER_BRANCH" ]; then USER_BRANCH="$CURRENT_BRANCH"; fi
echo -e "${GREEN}Pushing to origin/$USER_BRANCH ...${NC}"
if git push -u origin "$CURRENT_BRANCH":"$USER_BRANCH"; then
echo
echo -e "${GREEN}✔ Push successful!${NC}"
echo -e "${CYAN}📝 Summary of pushed commits:${NC}"
# Show commits that just got pushed
git log origin/$USER_BRANCH --oneline --no-decorate | head -n 10
echo -e "${YELLOW}(showing latest up to 10 commits)${NC}"
echo
echo -e "${GREEN}Remote branch: origin/$USER_BRANCH${NC}"
else
echo -e "${RED}❌ Push failed.${NC}"
return 1
fi
}
alias push="git_push"
Comments
Post a Comment