Перейти к содержанию

Проблемы

При первом push в репозиторий с коммитом ошибка

git push --set-upstream origin main
To gitlab.com:ansible4990534/ansible-role-gitlab-runner.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'gitlab.com:ansible4990534/ansible-role-gitlab-runner.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.

Решение:

Берем существующий коммит из удаленного репозитория и объединяем истории

git pull origin main --allow-unrelated-histories

Если похожая ошибка:

From gitlab.com:ansible4990534/ansible-role-gitlab-runner
 * 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.

Тогда пробуем:

git pull origin main --no-rebase --allow-unrelated-histories

Если похожая ошибка:

From gitlab.com:ansible4990534/ansible-role-gitlab-runner
 * branch            main       -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

Нужно решить конфликт (либо оставить наш файл или из удаленной репы) командой:

git restore --ours README.md  #оставляем наш локальный файл 
git restore --theirs README.md  #оставляем файл из репозитория