LoginSignup
0
0

GitHubへの簡単なやり取りを自動化する

Posted at

GitHubへの簡単なやり取りを自動化する

概要

GitHubへのやり取りが面倒だったのでChat-gptに自動化してもらった

※gitのメール・名前・リモートリポジトリの情報が追加されていて、当然権限もある前提
※最初のコミットは手動じゃないとだめかも...

尚、up主のGitHubはこちら

git pushの自動化

オプションを設定しなければ以下のように自動で日付が追加される
「AutomaticPush_2023-11-29_13-03-30」
-mを付けることでコミット時のコメントを追記できる

bash
#!/bin/bash

current_date=$(date +%Y-%m-%d)
current_time=$(date +%H-%M-%S)

commit_message="AutomaticPush_${current_date}_${current_time}"

git status
git add .

while getopts ":m" opt; do
  case $opt in
    m)
      # Prompt the user for a commit message
      read -p "Enter commit message: " user_message
      commit_message="$user_message"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

git commit -m "$commit_message"
sleep 2
git push origin master

※Masterに直接アップされるので注意

git pullの自動化

bash
#!/bin/bash

# ユーザーに確認メッセージを表示し、ユーザーの入力が'y'かどうかを確認する関数
function confirm() {
  read -r -p "$1 (y/n): " response
  case "$response" in
    [yY][eE][sS]|[yY])
      true
      ;;
    *)
      false
      ;;
  esac
}

# リモートから最新の変更を取得
if confirm "リモートから最新の変更を取得しますか?"; then
  git fetch --all
else
  echo "処理を中止します。"
  exit 1
fi

# ローカルのブランチをリモートの対応するブランチと同期
if confirm "ローカルのブランチをリモートの対応するブランチと同期しますか?"; then
  git reset --hard origin/master
else
  echo "処理を中止します。"
  exit 1
fi

# ローカルの変更を取り消して、指定されたブランチの状態と一致させる
if confirm "ローカルの変更を取り消して、指定されたブランチの状態と一致させますか?"; then
  git pull origin master
else
  echo "処理を中止します。"
  exit 1
fi

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0