Git:GitHubレポジトリのPublic/Private変更方法

公開:2026.05.10(日) 17:19

GitHubレポジトリのPublic/Private変更方法

レポジトリの "Settings" - "General" ページの末尾に "Danger Zone" がある。
そこの "Change repository visibility" という項目の [Change visibility] ボタンをクリックする。
これでPrivateに変更できる。


"Git 学習"

Git:GitHubへのアップロード方法

公開:2026.05.10(日) 14:48

GitHubへのアップロード方法

JavaScriptの学習(JavaScript #9:電卓に1文字消すボタンを追加する)のソースコードをローカルのGitで管理していたが、これをGitHubへアップロードしてみようと思う。

1. GitHubアカウントの登録
登録済みなのでスキップ

2. 新しいレポジトリの作成
◆ GitHubの自分のDashboardページ右上の [+]ボタンより "New repository" をクリック

◆ Repository nameの入力と 公開設定(Public/Private)を選択し [Create repository] ボタンをクリック

◆ レポジトリを作成するとアップロード方法が表示される
新規の場合は "…or create a new repository on the command line" に書かれたコマンドw実行
既にGitで管理している場合は "…or push an existing repository from the command line" に書かれたコマンドを実行

◆ git push時にエラーが出る場合
"git push -u origin" を実行したところ以下のメッセージが表示された
xxx@xxx:~/docker/node.js/calc-vite$ git push -u origin
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

これはローカルの main と GitHubの main が紐付いていない、というメッセージらしい。
メッセージに表示されたコマンド "git push --set-upstream origin main" を実行することで対応
xxx@xxx:~/docker/node.js/calc-vite$ git push --set-upstream origin main
Username for 'https://github.com': xxx
Password for 'https://xxx@github.com':
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/xxx/CALC-VITE.git/'

またまたエラー。
GitHubではセキュリティ向上のため、2021年から通常のパスワードでの認証が廃止された?
PAT(Personal Access Token)という「Git操作専用の合言葉」を発行して入力することにする。

1. Githubでトークン(PAT)を発行
1. https://github.com/settings/tokens を開く

2. [Generate new toke] → [Generate new token (classic)

3. Noteの入力、"repo"へチェックを付け [Generate token] をクリック

4. "ghp_"で始まるトークンをメモしておく

2. "git push -u origin main" コマンドを実行
ユーザー名を入力し、パスワードには先程メモしたトークンを入力
xxx@xxx:~/docker/node.js/calc-vite$ git push -u origin main
Username for 'https://github.com': xxx
Password for 'https://xxx@github.com':
Enumerating objects: 36, done.
Counting objects: 100% (36/36), done.
Delta compression using up to 24 threads
Compressing objects: 100% (36/36), done.
Writing objects: 100% (36/36), 29.86 KiB | 5.97 MiB/s, done.
Total 36 (delta 13), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (13/13), done.
To https://github.com/xxx/CALC-VITE.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

これでGitHubにアップされた。


"Git 学習"

JavaScript #9:電卓に1文字消すボタンを追加する

公開:2026.05.10(日) 12:42

電卓に1文字消すボタンを追加する

打ち間違えた際に1文字削除するための1文字削除(バックスペース)キーを追加してみる。
この機能を実装するには、JavaScriptの文字列操作(Slice)を使う。
VB.NETでは String.Substring や、1文字削る String.Remove に相当。

1. index.html修正
"C"(クリア)ボタンの隣あたりに、バックスペースボタンを追加する。
  <button>C</button>
+ <button id="backspace">⌫</button>
  <button>=</button>

2. calculatorLogic.js修正 1文字削除する deleteLastChar メソッドを追加する。
export function deleteLastChar(currentValue) {
    // 1文字削った結果を返す
    return currentValue.slice(0, -1);
}

3. main.js修正
deleteLastCharメソッドをインポート、button.id=backspace クリック時にdeleteLastCharメソッドを呼び出すように修正する。

  import './style.css'
- import { calculate, clearDisplay } from './calculatorLogic.js'
+ import { calculate, clearDisplay, deleteLastChar } from './calculatorLogic.js'
 :
  buttons.forEach(button => {
      button.addEventListener('click', () => {
          const value = button.textContent;
  
+         if (button.id === 'backspace') {
+            display.value = deleteLastChar(display.value);
+             return;
+         }
 ;

参考(全体ソース)

index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <title>Vite 電卓</title>
  </head>
  <body>
    <div id="app">
      <!-- ここに電卓のHTMLを貼り付けます -->
      <div id="calculator">
          <input type="text" id="display" readonly>
          <div class="buttons">
              <button>7</button><button>8</button><button>9</button><button>/</button>
              <button>4</button><button>5</button><button>6</button><button>*</button>
              <button>1</button><button>2</button><button>3</button><button>-</button>
              <button>0</button>
              <button>C</button>
              <button id="backspace">⌫</button>
              <button>=</button>
              <button>+</button>
          </div>
      </div>
      <div id="history-container">
        <h3>計算機能</h3>
        <ul id="history-list"></ul>
        <button id="clear-history">履歴をクリア</button>
      </div>
    </div>
    <script type="module" src="./src/main.js"></script>
  </body>
</html>

calculatorLogic.js
// calculatorLogic.js の先頭に必要です
import { evaluate } from 'mathjs';

export function calculate(expression) {
    try {
        return evaluate(expression); // window.math ではなく直接 evaluate を使う
    } catch (error) {
        console.error("計算エラー:", error); 
        return 'Error';
    }
}

export function clearDisplay() {
    return '';
}

export function deleteLastChar(currentValue) {
    // 1文字削った結果を返す
    return currentValue.slice(0, -1);
}

main.js
import './style.css'
import { calculate, clearDisplay, deleteLastChar } from './calculatorLogic.js'

const display = document.getElementById('display');
const buttons = document.querySelectorAll('#calculator .buttons button');
// 履歴を表示するulノード
const historyList = document.getElementById('history-list');
// 履歴をクリアするボタン
const clearHistoryBtn = document.getElementById('clear-history');

// 履歴を格納するための historyData配列 を追加
let historyData = [];

buttons.forEach(button => {
    button.addEventListener('click', () => {
        const value = button.textContent;
 
        if (button.id === 'backspace') {
            display.value = deleteLastChar(display.value);
            return;
        }

        if (value === 'C') {
            display.value = clearDisplay();
        } else if (value === '=') {
            // "="の処理で historyData配列 へ値の追加と liノードの追加
            const expression = display.value;
            const result = calculate(display.value);

            if (result !== 'Error') {
                historyData.unshift(`${expression} = ${result}`);
                renderHistory();
                saveHistory();
            }
            display.value = result;
        } else {
            display.value += value;
        }
    });
});

// 履歴クリア処理
function renderHistory() {
    historyList.innerHTML = '';

    historyData.forEach(item => {
        const li = document.createElement('li');
        li.textContent = item;
        historyList.appendChild(li);
    });
}

//  履歴をクリアボタンにイベント登録
clearHistoryBtn.addEventListener('click', () => {
    historyData = [];
    renderHistory();
});

// ローカルストレージへの保存
function saveHistory() {
    // 配列をJSON文字列に変換して保存
    localStorage.setItem('calculator-history', JSON.stringify(historyData));
}

// ローカルストレージからの復元
function loadHistory() {
    const savedData = localStorage.getItem('calculator-history');
    if (savedData) {
        // JSON文字列を配列に戻す
        historyData = JSON.parse(savedData);
        renderHistory();
    }
}

// 起動時に実行
loadHistory();


"JavaScript 学習"

その他の記事