LoginSignup
4
2

More than 3 years have passed since last update.

【Git】non-fast-forwardエラーが出た時の対処法

Last updated at Posted at 2021-04-02

はじめに

こちらは、私が毎度どうしようもないところで躓いて解決できず、時間がただ過ぎていく状況を改善するための救いようのない備忘録です。

ネット上の他の解決法を試してもダメだった場合、役に立てるかもしれません!

環境

・Git - 2.31.1

git pushでエラー!

git pushを実行すると、

 ! [rejected]        main -> main (non-fast-forward)

というようにnon-fast-forwardとエラーが出てきます。

とりあえず、git pull origin mainを叩いてみます。すると今度はこんなメッセージが。

 * branch            main       -> FETCH_HEAD
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before 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.
fatal: refusing to merge unrelated histories

色々解決方法が載っていますね。そして、その中でも気になるのが、一番下の

fatal: refusing to merge unrelated histories

です。どうやら無関係はヒストリーをもつものをマージしようとしていたみたいです。

解決!

マージがうまくいかない場合は、オプションに--allow-unrelated-historiesを追加することで無関係でもマージできるようになります。

$ git merge --allow-unrelated-histories origin/main

これでOK!あとは、get statusで変更状況を確認して、変更があれば、git add <file>、そしてgit commit -m <comment>、最後にgit push origin mainでフィニッシュです。

おわりに

今自分がどこまでやったか、頭の片隅に入れておきたいですね。

4
2
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
4
2