当我在用git项目提交的时候,经常会遇到这个问题:
! [rejected] development -> development (non-fast-forward)
在踩过好几次坑和浏览了很多网上解析后,终于找到一个觉得比较好用的方法。
在push 远程的时候出现冲突
在开发中,在我们向远程Git 仓库push 代码的时候,经常会因为本地的版本落后于远程的,报出类似下面的错误提示。
! [rejected] development -> development (non-fast-forward)
error: failed to push some refs to 'https://e.coding.net/xiniaogongkao/tudoqingke.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.
我们来看一下我的一个完整提交过程,看看这个错误是怎么出来的。
D:\myproject\tudoqingke>git status
On branch development
Your branch and 'origin/development' have diverged,
and have 1 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
D:\myproject\tudoqingke>git add .
D:\myproject\tudoqingke>git commit -m '兑换修改'
On branch development
Your branch and 'origin/development' have diverged,
and have 1 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
D:\myproject\tudoqingke>git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
D:\myproject\tudoqingke>git pull origin master
From https://e.coding.net/xiniaogongkao/tudoqingke
* branch master -> FETCH_HEAD
Already up to date.
D:\myproject\tudoqingke>git checkout development
Switched to branch 'development'
Your branch and 'origin/development' have diverged,
and have 1 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
D:\myproject\tudoqingke>git merge master
Already up to date.
D:\myproject\tudoqingke>git push origin development
To https://e.coding.net/xiniaogongkao/tudoqingke.git
! [rejected] development -> development (non-fast-forward)
error: failed to push some refs to 'https://e.coding.net/xiniaogongkao/tudoqingke.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.
上面的这些操作我是想把本地修改的文件commit 后提交到远程仓库中,但是,在我提交之前有一个人也修改了此文件并且提交到了远程仓库中,在我提交时,本得的代码就不是最新的了,落后于远程仓库的代码,所以这也是为什么经常会有人告诉我们,在提交代码前要先同步一下代码的原因。在Git 中同步代码常用的有两种方式 ,一种是使用pull 命令,一种是使用fetch + rebase ,会有专门的文章介绍这两种的用法。下面我们使用pull 方法进行同步一下代码。
操作如下:
D:\myproject\tudoqingke>git pull
Auto-merging subpkg-story/pages/story-play/story-play.wxml
CONFLICT (content): Merge conflict in subpkg-story/pages/story-play/story-play.wxml
Auto-merging subpkg-story/pages/story-play/story-play.js
CONFLICT (content): Merge conflict in subpkg-story/pages/story-play/story-play.js
Automatic merge failed; fix conflicts and then commit the result.
上面的信息的意思是说有文件发生冲突,我们需要解决这个冲突,然后重新提交。
查看发生冲突的文件,并进行更改
<<<<<<<和 >>>>>>>之间的是发生冲突的部分,中间的=======是将冲突的部分进行分离,<<<<<<< HEAD到 =======是本地的代码,=======到>>>>>>> 1bd642b86f99072e0930e72e84168ab3faf43d88是远程仓库的代码。如下:
本地代码:
<<<<<<< HEAD
<color name="colorAccent">#FF4081</color>
=======
远程仓库代码:
=======
<color name="colorAccentsever">#FF4081</color>
>>>>>>> 1bd642b86f99072e0930e72e84168ab3faf43d88
我们将冲突的部分合并后重新提交就可以push 了。
下面是实际操作:
D:\myproject\tudoqingke>git status
On branch development
Your branch and 'origin/development' have diverged,
and have 1 and 2 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)
Changes to be committed:
modified: subpkg-story/pages/story-detail/story-detail.js
modified: subpkg-story/pages/story-detail/story-detail.wxml
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: subpkg-story/pages/story-play/story-play.js
both modified: subpkg-story/pages/story-play/story-play.wxml
D:\myproject\tudoqingke>git add subpkg-story/pages/story-play/story-play.js
D:\myproject\tudoqingke>git add subpkg-story/pages/story-play/story-play.wxml
D:\myproject\tudoqingke>git commit -m '兑换及故事详情冲突更新'
[development 130a273] '兑换及故事详情冲突更新'
D:\myproject\tudoqingke>git pull
Already up to date.
D:\myproject\tudoqingke>git push origin development
Enumerating objects: 85, done.
Counting objects: 100% (65/65), done.
Delta compression using up to 4 threads
Compressing objects: 100% (29/29), done.
Writing objects: 100% (33/33), 4.73 KiB | 323.00 KiB/s, done.
Total 33 (delta 20), reused 0 (delta 0)
To https://e.coding.net/xiniaogongkao/tudoqingke.git
a3d2f23..130a273 development -> development
D:\myproject\tudoqingke>