How to force pull before push
每次我想要推动我的承诺(
要做到这一点,Git的方法是使用定制的Git钩子。
在您的情况下,您需要转到存储库中的
1 2 3 4  | #!/bin/bash echo"pulling ..." git pull  | 
。
当您执行
显然,这只是一个非常幼稚的样本,但我希望你能理解这个想法。此目录中已存在示例。如果有任何不清楚的地方,请发表评论。
根据OP的评论,他们似乎试图避免最近的提交和远程提交之间的合并提交,这通常是在本地和远程历史发生分歧时由
要做到这一点,显式的方法是首先获取和重新平衡,然后最后推送。
1 2 3  | git fetch git rebase origin/master git push  | 
第二种方法是在调用
1  | git pull --rebase  | 
。
最后,通过设置配置,可以将其设置为
1  | git config --global pull.rebase true  | 
推送之前实际上不需要拉:如果远程端没有提交,则git推送将失败,并显示消息:
1 2 3 4 5 6 7 8 9 10  | Pushing to git@yourserver:<user>/<repo>.git To git@yourserver:<user>/<repo>.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/BigMeanCat/CMDA' 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.  | 
如果您可以控制远程服务器,则可以在其中设置:
1  | git config --system receive.denyNonFastForwards true  | 
。
更一般地说,您可以结合两个命令轻松定义别名:
1  | git config alias.pullpush '!git pull && git push'  | 
如果拉不动,推不动。
最后,可以在bash脚本中组合任何命令序列,命名为
1  | git-pullpush  | 
。
(没有扩展名,可执行,存储在
它将是一个常规的bash脚本(即使在Windows上也可以工作,因为它将由msys bash解释)
1 2 3  | #!/bin/bash # you can add any command you want git pull && git push  | 
。
你可以用
当您希望
1  | command1 && command2  | 
在
1  | command1 ; command2  | 
号
我以前用过一个用于