长沙做企业网站,襄阳seo推广,尚层装饰,网站建设资讯站问题
当我们在使用 Git 向远程仓库提交代码时#xff0c;可能会遇到如下所述的错误提示#xff1a;
To https://github.com/xxxxx/gitmerge.git! [rejected] master - master (fetch first)
error: failed to push some refs to https://github.com/xxxxx/gitme…问题
当我们在使用 Git 向远程仓库提交代码时可能会遇到如下所述的错误提示
To https://github.com/xxxxx/gitmerge.git! [rejected] master - master (fetch first)
error: failed to push some refs to https://github.com/xxxxx/gitmerge.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., git pull ...) before pushing again.
hint: See the Note about fast-forwards in git push --help for details.这种情况出现的原因是当前本地分支的一些提交被其它开发者推送到了远程仓库导致远程仓库的最新代码已经超过了当前本地分支的代码状态因此在将本地代码推送到远程仓库时出现了冲突。此时需要手动合并远程仓库的最新代码并提交代码。
场景一
$ git pull然后再 push 到远程仓库即可。
适用场景冲突比较简单、容易解决可以尝试使用 git pull 命令来合并最新代码并进行必要的手动部分解决和合并操作。
举个例子
我在 github 的远程仓库上创建了一个 README 文件而我本地仓库中没有这个 README 文件所以当我在本地没有这个 README 文件的基础之上在 push 的话就会出现冲突。恰好我们又想在本地编辑一下 README 文件这样其实可以直接使用git pull因为冲突比较简单。
首先在 github 上创建一个远程仓库并且在初始化仓库的时候选择不添加 README.md 文件如果选择添加的话github 会自动帮你创建一个分支。然后在本地初始化一个仓库
$ mkdir test_git_merge
$ cd test_git_merge
$ git init
$ echo 这是本地仓库 hello.txt
$ git add .
$ git commit -m test
$ git remote add origin https://github.com/your_username/your_repo_name.git
$ git push -u origin your_branch在 github 上的远程仓库新建一个 README.md 文件修改或添加一定的内容到 README.md 文件
此时我们在本地修改hello.txt, 然后尝试将其推送到远程
$ echo 修改本地仓库 hello.txt
$ git add .
$ git commit -m test2
$ git push此时会出现文章开头时的错误, 我们直接使用以下命令
$ git pull我们会看到如下
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 715 bytes | 102.00 KiB/s, done.
From https://github.com/xxxxx/testgitmerge2c6c00c..466227d master - origin/master
Merge made by the ort strategy.README.md | 3 1 file changed, 3 insertions()create mode 100644 README.md此时我们会发现本地仓库中多了远程仓库中的 README.md 文件然后我们执行
$ git add .
$ git commit -m test3
$ git push这次我们顺利的 push 了
场景二
假设你在本地更新 hello.txt 文件并将其推送到远程分支并且在此期间远程分支也发生了变化如在 GitHub 上通过在线编辑在 hello.txt 文件中添加了一些内容。此时你从远程分支 pull 代码将会产生冲突需要手动解决冲突并进行代码合并。
以下是一个手动解决代码合并冲突的示例
在远程分支上编辑了 hello.txt 文件增加一个新行 “修改远程仓库”
这是本地仓库
修改本地仓库
修改远程仓库推送远程修改 在本地编辑 hello.txt 文件增加一个新行 “helloworld”
这是本地仓库
修改本地仓库
helloworld提交并推送修改将其应用到远程仓库
$ git add hello.txt
$ git commit -m add helloworld to hello.txt
$ git push出现错误
To https://github.com/xxxxx/testgitmerge.git! [rejected] master - master (fetch first)
error: failed to push some refs to https://github.com/Xxxxxx/testgitmerge.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., git pull ...) before pushing again.
hint: See the Note about fast-forwards in git push --help for details.然后本地分支尝试 pull 远程仓库的修改这时候 Git 将会自动合并尝试。发现 hello.txt 文件存在冲突如下所示(原因是同一行中的内容不同)
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.打开 hello.txt. 文件可以看到冲突的内容
这是本地仓库
修改本地仓库HEAD
helloworld修改远程仓库6963a862d66fed43ccd87dad21b0dae8b7105c0b在这个示例中Git 将本地和远程更改放在两个不同的区块中中间分隔符 HEAD 和 标记着来自两个源的冲突本地更改在上方远程更改在下方之后远程更改的 ID 用 标识出来。
然后可以手动审查并解决冲突如删除一个 区块来保留所需代码或使用编辑器等工具进行更高级的合并操作。在这种情况下我们可以手动将两个内容合并到一起(删除对应的分隔符即可)
这是本地仓库
修改本地仓库
helloworld
修改远程仓库手动解决后使用 git add 命令添加文件。
$ git add hello.txt最后使用 git commit 命令提交更改git push 推送到远程仓库即可。
$ git commit -m Merge the hello.txt from remote and local
$ git push origin master通过以上步骤你已经手动解决了冲突并合并了代码。需要注意的是在手动解决代码冲突时要进行谨慎操作确保代码的质量和准确性。
场景三
多个开发者分别在不同的分支上修改同一个文件然后尝试合并分支由于分支之间的代码修改冲突导致合并失败。 在 GitHub 上创建一个新的空仓库。 将该仓库克隆到本地并在本地仓库上创建一个新分支。
$ git clone https://github.com/your_github_username/your_repo.git
$ cd your_repo
$ git checkout -b test修改并提交文件将本地分支 push 到远程仓库。
# 编辑文件
$ echo line1 test.md
echo line2 test.md# 添加修改后的文件
$ git add test.md# 提交修改
$ git commit -m Add line1 and line2# 向远程仓库推送本地分支
$ git push origin test切换到另一个本地分支并修改相同文件中的相同行代码。
# 切换到另一个分支
$ git checkout -b another_branch# 编辑 test.md
$ sed -i s/line1/line11/ test.md# 添加修改后的文件
$ git add test.md# 提交修改
$ git commit -m Change line1 to line11此时尝试使用 git pull 命令将远程分支的修改拉取到本地分支进行合并即可触发合并冲突。例如执行以下命令
# 从远程仓库拉取最新的代码
$ git fetch origin# 在当前分支上将远程分支合并到本地分支
$ git merge origin/test此时由于两个分支都修改了 test.md 文件的同一行代码Git 将无法自动合并这两个本地分支会提示冲突并让你解决这些冲突。你需要手动解决这些冲突合并文件中的代码然后提交更改并完成该操作。