15.4 合并后推送
理性的工作协同要避免非快进式推送。一旦向服务器推送后,如果发现错误,不要使用会更改历史的操作(变基、修补提交),而是采用不会改变历史提交的反转提交等操作。
如果在向服务器推送过程中,由于他人率先推送了新的提交导致遭遇到非快进式推送的警告,应该进行如下操作才更为理性:执行git pull获取服务器端最新的提交并和本地提交进行合并,合并成功后再向服务器提交。
例如用户user1在推送时遇到了非快进式推送错误,可以通过如下操作将本地版本库的修改和远程版本库的最新提交进行合并。
(1)用户user1发现推送遇到了非快进式推送。
$cd/path/to/user1/workspace/project/
$git push
To file:///path/to/repos/shared.git
![rejected]master->master(non-fast-forward)
error:failed to push some refs to 'file:///path/to/repos/shared.git'
To prevent you from losing history,non-fast-forward updates were rejected
Merge the remote changes(e.g.'git pull')before pushing again.See the
'Note about fast-forwards' section of'git push—help'for details.
(2)用户user1运行git pull命令。
命令git pull实际包含两个动作:获取远程版本库的最新提交,以及将获取到的远程版本库提交与本地提交进行合并。
$git pull
remote:Counting objects:5,done.
remote:Compressing objects:100%(2/2),done.
remote:Total 4(delta 0),reused 0(delta 0)
Unpacking objects:100%(4/4),done.
From file:///path/to/repos/shared
+b4f3ae0…6b1a7a0 master->origin/master(forced update)
Merge made by recursive.
team/user2.txt|1+
1 files changed,1 insertions(+),0 deletions(-)
create mode 100644 team/user2.txt
(3)合并之后,看看版本库的提交关系图。
合并之后远程服务器中的最新提交6b1a7a0成为当前最新提交(合并提交)的父提交之一。如果再推送,则不再是非快进式的了。
$git log—graph—oneline
*bccc620 Merge branch 'master' of file:///path/to/repos/shared
|\
|*6b1a7a0 user2's profile.
*|b4f3ae0 user1's profile.
|/
*5174bf3 initial commit.
(4)执行git push命令,成功完成到远程版本库的推送。
$git push
Counting objects:10,done.
Delta compression using up to 2 threads.
Compressing objects:100%(5/5),done.
Writing objects:100%(7/7),686 bytes,done.
Total 7(delta 0),reused 0(delta 0)
Unpacking objects:100%(7/7),done.
To file:///path/to/repos/shared.git
6b1a7a0..bccc620 master->master