cre*_*ips 13
Git方法是使用自定义Git钩子.
在您的情况下,您将需要转到.git/hooks存储库中的目录,并添加一个名为的文件pre-push,这是您选择的脚本,例如(在此示例中为bash)
#!/bin/bash
echo "pulling ..."
git pull
Run Code Online (Sandbox Code Playgroud)
在执行此操作时git push以及在实际推送之前将调用此脚本.
显然,这只是一个非常天真的样本,但我希望你能得到这个想法.此目录中已有样本.评论是否还有什么不清楚.
根据OP的评论,他们似乎试图避免他们最近的提交和远程提交之间的合并提交,这通常是由git pull本地和远程历史分歧时产生的.
这样做的明确方法是首先获取和rebase,最后推送.
git fetch
git rebase origin/master
git push
Run Code Online (Sandbox Code Playgroud)
第二种方法是--rebase在调用时传递git pull.
git pull --rebase
Run Code Online (Sandbox Code Playgroud)
最后,可以git pull通过设置配置使其成为默认行为.
git config --global pull.rebase true
Run Code Online (Sandbox Code Playgroud)
在推送之前你实际上不需要拉:如果远程端有你没有本地提交,git push将失败并显示消息:
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.
Run Code Online (Sandbox Code Playgroud)
如果您可以控制远程服务器,则可以在此处进行设置:
git config --system receive.denyNonFastForwards true
Run Code Online (Sandbox Code Playgroud)
更一般地,您可以轻松定义组合两个命令的别名:
git config alias.pullpush '!git pull && git push'
Run Code Online (Sandbox Code Playgroud)
如果拉动失败,则不会执行推送.
最后,您可以在命名的bash脚本中组合任何命令序列
git-pullpush
Run Code Online (Sandbox Code Playgroud)
(没有扩展名,可执行文件,存储在由引用的文件夹中$PATH)
它将是一个常规的bash脚本(甚至可以在Windows上运行,因为它将由msys bash解释)
#!/bin/bash
# you can add any command you want
git pull && git push
Run Code Online (Sandbox Code Playgroud)
而你会称之为git pullpush(像别名)