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 初めてのpush備忘録@20250118 ~ set-urlと--allow-unrelated-histories ~

Last updated at Posted at 2025-01-17

初めてgitを触った。

パーミッションで行き詰まったり、いろんなエラーに遭遇したので残しておく。

ローカルでgit

まず初期化。すべてはここから始まる。

git init

次に、ローカルのgitにカレントディレクトリのファイルを加える。

git add .

GitHub側で行うこと

次にGithubへアクセスして、適当にリポジトリを作る。

以前作ったスマートスピーカーのリポジトリなので、名前はRaspGPTとした。

ここでLICENCEとReadme.mdを設定したために後でエラーが出るのだが、一旦割愛。

次に、リモートリポジトリを設定する。
2025年1月18日現在、GitHubではトークンによる認証を行っている。

そのため、適宜トークンを取得する必要がある。

手順

  1. Githubの右上の自分のアイコンをクリック。歯車マーク「settings」から設定を開く
  2. 設定の左側のサイドバー、その一番下「Deveroper settings」を開く
  3. 左側のサイドバー「Personal access tokens」から「Fine-grained tokens」を選択、緑色のボタンを押す
  4. トークンの名前などを設定
  5. 「Repository permissions」は「Content」を「Read/Write」に設定
  6. 生成したトークンをコピーして以下のコマンドを実行
git remote add origin https://自分で取得したトークン@github.com/ユーザー名/リポジトリ名.git

ちゃんとユーザー名とリポジトリ名は自分で入力をおねがいします。
あと、トークンはこの画面から切り替わると二度と表示できないので気をつけて。

一度add originしてしまって変更できないよ!

もしもすでにoriginを設定してしまっている場合、add originではうまくいきません。

その場合、

git remote set-url https://自分で取得したトークン@github.com/ユーザー名/リポジトリ名.git

としてやればoriginの内容を書き換えられます。

参考

ローカルから、いざpush!の前に拒否られる。

早速pushと思ったが、comitの時点で拒否られた。

Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'ユーザ名@ユーザ名.(none)')

まず、メールアドレスとかも設定しないといけないらしい。

ので一応設定する。

GitHubと通信するわけだから、

git config --global user.name "GitHubのユーザ名"
git config --global user.email "GitHubで設定したメールアドレス"

とした。

これでコミット通った。

git commit -m "Initial commit"

gitのbranchの名前をmainに変更

git branch -M main

履歴が違うぞ!

さあ、pushだ!

と思いきやまたしてもエラー。

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/narikyow/RaspGPT.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

どうやら履歴が食い違っているらしい。

ひとまずpullするとこれ。

 * branch            main       -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

ここで行き詰まった。

原因は明らかで、リポジトリを作ったときに、LICENSEとReadme.mdを作ったから。

初期状態が違うためにバッティングしたと考えられる。

色々調べた結果、次のコマンドで解決した。

# 履歴をリモートとローカルの両方からすり合わせる。
git config pull.rebase false
# --allow-unrelated-historiesを追加
git pull origin main --allow-unrelated-histories
# pushに成功
git push origin main

類似事例も発見。

やはり、最初にLICENSEとReadme.mdをリポジトリに作成したのが問題だったらしい。

共通の祖先がないとマージするのがうまくいかないからエラーが発生していた。

おまけ

.gitignoreで無視していたファイルをpushする

git rm -r --cached .
git add .
git commit -m "Apply .gitignore changes"
git push

リポジトリの履歴を削除し、現在の状態のみをリポジトリに保存する

# 新しいブランチを作成
git checkout --orphan latest_branch
git add .
git commit -m "Clean slate"
# mainブランチを削除
git branch -D main
# 現在のブランチの名前をmainとする
git branch -m main
# リモートリポジトリを強制的に上書き
git push -f origin main
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?