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 2024-10-27

初めに

開発中に間違えて違うブランチでプッシュしてしまってその修正を行うのに手間取ったのでその解決方法を紹介します

エラー詳細

git push origin main

を行った後のエラー

To https://github.com/opatu07/todo_maneger.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/opatu07/todo_maneger.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

deeplでの翻訳結果

https://github.com/opatu07/todo_maneger.git へ
 ! [拒否] main -> main (非ファストフォワード)
error: 'https://github.com/opatu07/todo_maneger.git' への参照プッシュに失敗しました。
ヒント: 更新が却下されたのは、現在のブランチの先端が
ヒント: 現在のブランチの先端がリモートのブランチより遅れているため、更新が却下されました。リモートの変更を統合したい場合は
hint: 再度プッシュする前に 'git pull' を使ってください。
hint: 詳細については 'git push --help' の 'ファストフォワードに関する注意' を参照してください。

リモートブランチよりローカルブランチが遅れていることが原因らしいです
なので

git pull origin main

を行いその後でたエラーがこちら

From https://github.com/opatu07/todo_maneger
 * 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.
https://github.com/opatu07/todo_maneger より
* ブランチメイン -> FETCH_HEAD
hint:分岐したブランチがあり、それらを調整する方法を指定する必要があります。
hint: 次のコマンドのいずれかを、次のプルの前に実行してください。
hint: 次のプルの前に、以下のコマンドを実行します:
を実行します:
ヒント: git config pull.rebase false # マージ
ヒント: git config pull.rebase true # rebase
hint: git config pull.ff only # 早送りのみ
ヒント
hint: 「git config」 を 「git config --global」 に置き換えると、デフォルトを設定できます。
hint: すべてのリポジトリに対する優先順位を設定します。rebase、--no-rebase を渡すこともできます、
を渡すこともできます。
を渡すこともできます。
致命的: 分岐したブランチを調整する方法を指定する必要があります。

DeepL.com(無料版)で翻訳しました。

分岐したブランチのために何かの処理を実行しないといけないらしいです
そこで思い出しました
僕はmainブランチを作成後git flowの導入を忘れていたので

developブランチを作成したのでした
その処理の調整をエラーが教えてくれているのだと

解決方法

git reset

を行いました理由は
履歴を残したくなかったからです

mainブランチは本番環境なのでdevelopブランチで通っていないファイルをmainブランチにあげることはチーム開発ではないと考えたからです
そもそもdevelopブランチ作らないとかありえないとは思いますが
履歴を残すrevertをやってもよかったかなとかんがえたのですが最初の段階だったので履歴を残さないresetを選択しました
開発が進んでいたならrevertで履歴を残しました

追記

git pull後に同じエラーが出ました。今度はホーム画面の修正中にログイン画面用のブランチを作成したためブランチが分岐しました。
その状態でホーム画面の修正をdevelopブランチに反映しました
その後git pullしたので前回と話が変わってきます。
今回重要なのはホーム画面の修正をログイン画面に反映することだと考えたので
デフォルトのgit config pull.rebase falseを選択しました。
ちょっと個々の理解が曖昧だからもう少し勉強したいと思います

最後に

チーム開発なら絶対やってはいけないことなので次からgit flowを意識してやっていきたいです

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?