0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git リポジトリ関連コマンド [ 備忘録 ]

Last updated at Posted at 2025-05-03

現在のブランチからアップストリーム(上流)ブランチの関連付けを解除する

git branch --unset-upstream

upstreamとは、あなたのローカルブランチが「どのリモートリポジトリのどのブランチ」と連携しているかを定義する関連付けです。

upstreamの確認方法

git branch -vv
# 出力
  develop 4f7bdf9 [origin/develop] cicd.ymlファイルを更新する。
* main    4f7bdf9 [origin/main] cicd.ymlファイルを更新する。
  sub     4f7bdf9 cicd.ymlファイルを更新する。

各ローカルブランチがどのリモートブランチを追跡しているかを確認できます。

リモートリポジトリの参照を削除する

git remote remove origin

リモートリポジトリを確認

git remote -v

originとは、単にローカルGitリポジトリが参照するリモートリポジトリの「別名」です。特別な機能があるわけではなく、単に慣習的に使われる名前です。

Gitリポジトリ移行のプロセス

既存のローカルリポジトリに新しいリモートリポジトリを接続する

git remote add origin git@github.com2:oyne2561/ecs-github-actions-learning.git

git branch -M main
git push -u origin main

2個目のGitHubアカウントでSSH接続を設定する方法

  1. 新しいSSHキーペアを生成
ssh-keygen -t ed25519 -C "your-second-email@example.com" -f ~/.ssh/github_second_account
  1. 公開キーを2個目のGitHubアカウントに追加
# 公開キーをクリップボードにコピー
cat ~/.ssh/github_second_account.pub | pbcopy  # Macの場合
# Windowsの場合は: cat ~/.ssh/github_second_account.pub | clip
  1. SSH設定ファイルの編集
vim ~/.ssh/config

設定ファイルに以下のように記述します:

# デフォルトのGitHubアカウント (最初のアカウント)
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_ed25519  # または既存のキーファイル

# 2個目のGitHubアカウント
Host github.com2
   HostName github.com
   User git
   IdentityFile ~/.ssh/github_second_account
  1. 設定のテスト
# SSH接続テスト(最初のアカウント)
ssh -T git@github.com

# SSH接続テスト(2個目のアカウント)
ssh -T git@github.com2

成功すると、以下のようなメッセージが表示されます:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
  1. リポジトリのクローンと使用方法
    2個目のアカウントのリポジトリをクローンする場合:
# 通常のURLを変更して使用
git clone git@github.com2:username/repository.git

既存のリポジトリのリモートURLを変更する場合:

git remote set-url origin git@github.com2:username/repository.git

[初回の場合] リモートリポジトリを設定する

git remote add origin git@github.com2:oyne2561/ecs-github-actions-learning.git

github コメント (レビュー)

github_pending.png

直前のコミットのコメントを修正する

git commit --amend -m "コメント"

エラー対処法

1. git remote remove originが落ちる時

「error: could not delete references: cannot lock ref 'refs/remotes/origin/fix/readme'」

大文字と小文字の判別が Git とファイルシステムで差異があることが原因です。

例えば、下記2つのブランチが存在している場合:

  • feature/ticket-123
  • feature/TICKET-123

ファイルシステムでは、大文字小文字の違いを区別せずに同じファイルが存在しているとみなしています。

.git/packed-refs を編集して、重複している ref を削除することで対応できます。

vim .git/packed-refs   

SSH鍵生成とクリップボードコピー

ssh-keygen -t rsa
pbcopy < ~/.ssh/id_rsa.pub

GitHubやサーバーに貼り付け可能

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?