【git】error: failed to push some refs to ‘https://github.com/biluko/ZJGSU-Exams-in-master-two.git‘

When I submitted the code, I encountered the following error:

To https://github.com/biluko/ZJGSU-Exams-in-master-two.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/biluko/ZJGSU-Exams-in-master-two.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.

Looks like I've hit a Git related bug.

The error message said that my latest commit on the current branch was behind the remote branch, so Git rejected my push because it would result in non-fast-forward changes.

1. Question 1

To fix this, you need to merge the changes from the remote branch into your local branch before pushing. You can follow the steps below:

First, make sure I am currently on the local branch, you can use the following command to view the current branch:

  1. First, make sure you are currently on the local branch, you can use the following command to view the current branch:
git branch

If you are not on the branch you want to push, you can switch to the target branch with the following command:

git checkout <branch-name>
  1. Next, pull the latest changes from the remote branch with the following command:
git pull origin <branch-name>

This will automatically merge changes from the remote branch into your local branch.

  1. If there are conflicts, Git will prompt you to resolve the conflicts. You need to manually edit the file to resolve the conflict and mark the conflict resolved with:
git add <conflicted-file>

Repeat this process until all conflicts are resolved.

  1. When all conflicts are resolved, commit the merged changes with:
git commit -m "Merge remote changes"
  1. Finally, push the changes from the local branch to the remote branch with the following command:
git push origin <branch-name>

The push should complete successfully.

Hope these steps help you resolve the issue and successfully push your changes to the remote branch. If you're still having trouble, please provide more error messages so I can help you better.

2. Question 2

The code reports an error again:

From https://github.com/biluko/ZJGSU-Exams-in-master-two
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

It looks like you've hit a Git-related bug. The error message says that Git refuses to merge unrelated histories.

This error usually occurs between two different Git repositories or branches whose histories do not have a common ancestor, so Git cannot automatically merge them.

If you are sure you want to merge the two unrelated histories, you can use the --allow-unrelated-histories option to force the merge. You can follow these steps:

  1. First, pull the latest changes from the remote branch with the following command:
git pull origin master
  1. When you run the above command, add the --allow-unrelated-histories option:
git pull origin master --allow-unrelated-histories

This will force a merge of the remote branch's history with the local branch's history, even if they are unrelated.

Merge branch 'master' of https://github.com/biluko/ZJGSU-Exams-in-master-two
# 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.
~

When you see this prompt, it means that you need to provide a merge commit message to explain why the merge operation is required. This message can describe the reason for the merge, purpose, or other relevant information.

You can submit merged messages by following these steps:

  1. Press the lowercase "i" on your keyboard to enter edit mode.
  2. At the bottom of the editor, you'll see the cursor on a blank line waiting for you to type your message.
  3. Enter your merge message explaining why this merge operation is needed. May describe the purpose of the merger, what is being merged, or other relevant information. Make sure your message is concise and to the point.
  4. Press the "Esc" key on your keyboard to exit edit mode.
  5. Type a colon ":", then "wq" (for "write" and "quit"), and finally press Enter to save and close the editor.

In this way, you have successfully committed the merged message. If you don't want to provide a merge message, you can directly press the "Esc" key, then enter a colon ":", then enter "q!", and finally press the Enter key to abandon the commit merge message.

insert image description here
Successful upload!

Guess you like

Origin blog.csdn.net/wzk4869/article/details/131131198