LoginSignup
3
0

More than 1 year has passed since last update.

failed to push some refs to 'URL' について

Posted at

概要

railsアプリの開発中にgit pushしようとしたところ、以下のエラーが発生しました。

% git push origin feature/signup
To https://github.com/***/*****.git
 ! [rejected]        feature/signup -> feature/signup (non-fast-forward)
error: failed to push some refs to 'https://github.com/***/*****.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因

そういえば思い当たる節が...
以前、pushした後にローカルでgit resetなどで最新のコミットを消した覚えがあり、git pullし忘れた。
それによって、まだローカルにpullされていない最新版がリモートリポジトリにあることが原因で表示されるエラーが発生している。
https://omkz.net/error-git-push/

なので、まずはリモートの最新の変更をpullすることに...

対処法

 % git pull origin feature/signup
From https://github.com/***/*****
 * branch            feature/signup -> 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.

hint:1~3行目を翻訳してみると、

ヒント:分岐があり、それらを調整する方法を指定する必要があります。
ヒント:次のコマンドのいずれかを少し前に実行することで、これを行うことができます。

じゃあ、pullする前に上のコマンドを実行すればいいのか。

また以下の記事によると、

メッセージにあるように、以下の 3 つの設定のうち、いずれかを実施すれば、 warning は出なくなります。
git config pull.rebase false
git config pull.rebase true
git config pull.ff only

ということで、以下のコマンドを実行。
git config pull~でうまくいかなかったので、 --globalを付与してみました
(「git config」を「git config --global」に置き換えることで、全リポジトリにデフォルトを設定できます。)

% git config --global pull.rebase false

からの、git pull。

 % git pull origin feature/signup 
From https://github.com/***/*****
 * branch            feature/signup -> FETCH_HEAD
Merge made by the 'ort' strategy.

満を持して、git push。

% git push origin feature/signup 
Enumerating objects: 128, done.
Counting objects: 100% (97/97), done.
Delta compression using up to 8 threads
Compressing objects: 100% (59/59), done.
Writing objects: 100% (64/64), 11.30 KiB | 3.77 MiB/s, done.
Total 64 (delta 32), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (32/32), completed with 20 local objects.
remote: This repository moved. Please use the new location:
remote:   https://github.com/***/*****.git
To https://github.com/***/*****.git
   274360d..e010a8d  feature/signup -> feature/signup

pushできましたー!🙌

結論

git pushした後にコミットの取り消しなどしたら、リモートリポジトリからgit pullしてくることを癖づける。
初学者なので至らぬ点もありますが、皆さんのお役に立てれば嬉しいです。

参考記事

3
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
3
0