Gitで発生したエラーと解決方法

2023年8月5日システムGit

Gitで発生したエラーと解決方法

gitを使っていて発生したエラーと解決方法を備忘録として残します

Gitで発生したエラーと解決方法

error: failed to push some refs to 'git.example.jp:hoge/work.git’

git push origin develop しようとしたらエラーが発生

エラーメッセージ

To git.example.jp:hoge/work.git
 ! [rejected]            develop -> develop (fetch first)
error: failed to push some refs to 'git.example.jp:hoge/work.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.
ヒントの翻訳

ローカルにない作業がリモートに含まれているため、更新は拒否されました。これは通常、別のリポジトリが同じ参照にプッシュすることによって発生します。再度プッシュする前に、最初にリモートの変更 (例: 「git pull …」) を統合することをお勧めします。

詳細については、「git Push –help」の「早送りに関する注意」を参照してください。

原因

このエラーは一般的によく見られるエラーで、一般的に発生する原因は

  • リモートリポジトリに変更を push しようとしたときに、他の作業者がすでに同じブランチに変更を push している場合
  • リモートリポジトリのブランチの更新や変更がローカルリポジトリに反映されていない場合

などがある

解決方法

リモートリポジトリから git pull origin develop でファイルの変更を取得して最新の状態にする(ローカルリポジトリとリモートリポジトリを同期する)

$ git pull origin develop

Please enter a commit message to explain why this merge is necessary

git で merge した(された)際に表示されるメッセージ

これはエラーメッセージではない

メッセージ

Merge branch 'work/hoge' into develop
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
翻訳

このマージが必要な理由を説明するコミット メッセージを入力してください。
特に、更新されたアップストリームをトピック ブランチにマージする場合はそうです。

「#」で始まる行は無視され、空のメッセージが表示されるとコミットが中止されます。

原因

表示されるメッセージを読むと「マージするコミットのメッセージを入力して」ということ

解決方法

おそらく Linux 上で git を利用しているので、viエディタが起動していると思う

  1. [ESC]キーを押下
  2. [:] + [w] + [q] とキーを押下
  3. [Enter]キーを押下

これでOK

ちなみに [:] + [q] + [!] と入力するとマージが中断されるので注意

The branch 'ブランチ名’ is not fully merged.

Gitでブランチを削除しようとしたときに発生

エラーメッセージ

$ git branch -d ブランチ名
error: The branch 'ブランチ名' is not fully merged.
If you are sure you want to delete it, run 'git branch -D ブランチ名'.

原因

マージされていないブランチを削除しようとしている

解決方法

解決する方法は「マージしてからブランチを削除する」もしくは「強制的にブランチを削除する」の2通り

強制的にブランチを削除する場合
$ git branch -D ブランチ名
Deleted branch ブランチ名 (was 403e52a17).

warning: the following paths have collided (e.g. case-sensitive paths on a case-insensitive filesystem) and only one from the same colliding group is in the working tree:

GitでcloneしたときにWarningが発生

エラーメッセージ

warning: the following paths have collided (e.g. case-sensitive paths on a case-insensitive filesystem) and only one from the same colliding group is in the working tree:

  'example/test/index.html'
  'example/Test/index.html'

翻訳:警告: 次のパスが衝突しました (例: 大文字と小文字を区別しないファイルシステム上で大文字と小文字を区別するパス)。同じ衝突グループからの 1 つのみが作業ツリー内にあります。

原因

大文字・小文字違いのファイル(パス)が存在している
(※MacやWindowsなどのOSによって振る舞いが変わる)

解決方法

どちらかのファイル(パス)を削除する or ファイル(パス)を変更する

Filename too long

WindowsのSourceTreeでcloneしたときに発生

エラーメッセージ

Filename too long

翻訳:ファイル名が長すぎます

原因

ファイルの名前(パス)が長すぎる
(※MacやWindowsなどのOSによって振る舞いが変わる)

解決方法

以下のgitコマンドを実行すると解決

git config --system core.longpaths true
SourceTreeで解決する場合
SourceTree画面上部にある「ターミナル」よりターミナルを起動してgitコマンドを実行します

SourceTree画面上部にある「ターミナル」よりターミナルを起動してgitコマンドを実行します

補足

Got for Windowsでは260文字以上のファイル(パス)は許容されない設定となっている
参考:[Git] Windowsで “Filename too long” エラーが出た時の解決方法

You have divergent branches and need to specify how to reconcile them

git pull したら警告が表示

エラーメッセージ

 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 (the default strategy)
 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.

翻訳:分岐したブランチがあり、それらを調整する方法を指定する必要があります。

原因

2020年6月1日リリースの「Git 2.27.0」から表示されるようになった警告

補足

pullしたときに意図しないマージが発生するのを防ぐために変更
参考:Git 2.27.0 から git pull をすると表示されるようになった “Pulling without specifying how to reconcile divergent branches is discouraged." について

解決方法

pullするときに毎回オプションでしている。もしくはpullしたときにリモートブランチと差があったときのふるまいを以下の3つどれかで設定

–rebase したいとき:デフォルトの pull の挙動のままにする場合
git config pull.rebase false
–rebase なしで pull –rebase したいとき
git config pull.rebase true
差分があったら fast forward してほしい
git config pull.ff only

error: unable to create file /var/www/html/index.html: Permission denied

git pull したら警告が表示

エラーメッセージ

$ git pull origin develop
From ******
 * branch                develop    -> FETCH_HEAD
Updating *************
error: unable to create file /var/www/html/index.html: Permission denied

解決方法

pullするときに毎回オプションでしている。もしくはpullしたときにリモートブランチと差があったときのふるまいを以下の3つどれかで設定

/var/www/html への書き込み権限がない場合
$ ls -la /var/www/html
drwxrwsr-x * developer ec2-user   * Oct 26 13:33 .
drwxr-sr-x * root      ec2-user   * Oct 30 18:26 ..

/var/www/htmlの書き込み権限を変更する

$ sudo chown -R developer:ec2-user /var/www/html
$ sudo chmod -R 775 /var/www/html
$ ls -la /var/www/html
drwxrwsr-x * developer ec2-user * Oct 26 13:33 .
drwxrwsr-x * developer ec2-user * Oct 30 18:26 ..

Gitで操作ミスした時の解決対応

誤って git add した時【git add の取り消し方法】

誤って git commit した時【git commit の取り消し方法】

ユート
ユート

Webプログラマ兼ブロガー | プログラミング基礎知識を発信|Webプログラマ歴は10年以上|JavaScript,PHP.HTML | 「人生の時間は有限。悪いことをしている時間はない」
プロフィール

2023年8月5日システムGit

Posted by ユート