目录
分叉
当我们在一个分支使用git push
提交内容时,如果在远端有别人已经提交了,这时会报错。
$ git push
To github.com:JiangHuHiKe/JiangHuHiKe.github.io.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'github.com:JiangHuHiKe/JiangHuHiKe.github.io.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.
lixiaoyi@lixiaoyideMacBook-Pro in JiangHuHiKe.github.io on master
$
这个时候我们执行git pull
,仍然会报错
报错的原因是当前分支产生了分叉(divergent),git不知道如何处理
这时的处理方式可以选择merge也可以选择rebase,根据自己的喜好选择
$ git pull
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 6 (delta 5), reused 6 (delta 5), pack-reused 0
Unpacking objects: 100% (6/6), 491 bytes | 54.00 KiB/s, done.
From github.com:JiangHuHiKe/JiangHuHiKe.github.io
d049476..ec5e518 master -> origin/master
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 --no-rebase
,如果没有冲突就会自动进入vim
提交的信息若不需要修改,直接输入 :q
退出vim,然后执行 git push
这时就可以正常提交了
Merge branch 'master' of github.com:JiangHuHiKe/JiangHuHiKe.github.io
# 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.
~
~
"~/LXYFile/ResourceInGithub/JiangHuHiKe.github.io/.git/MERGE_MSG" 6L, 296B
如果有冲突,会列出有冲突的文件,并在文件内标出冲突的内容
$ git pull --no-rebase exit 128
Auto-merging _posts/1.ToolsUsage/1.Git/2019-01-28-Git总结.md
CONFLICT (content): Merge conflict in _posts/1.ToolsUsage/1.Git/2019-01-28-Git总结.md
Automatic merge failed; fix conflicts and then commit the result.
需要先手动解决冲突,我们看下当前的状态
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: "_posts/1.ToolsUsage/1.Git/2019-01-28-Git\346\200\273\347\273\223.md"
no changes added to commit (use "git add" and/or "git commit -a")
lixiaoyi@lixiaoyideMacBook-Pro in JiangHuHiKe.github.io on master
按提示手动解决冲突后执行下面的操作
# 告知冲突已经解决
git add .
# 提交的信息若不需要修改,直接输入 `:q`退出vim,然后执行 `git push` 这时就可以正常提交了
git commit
# 提交
git push
如果是使用 git pull –rebase 来解决的话。流程类似,按提示操作即可
行者常至,为者常成!