如何在推之前强制拉
How to force pull before push
每次我想推送我的提交 (git commit -m "message"
, git push origin <branch>
),我都会拉 (git pull origin <branch>
)。
有什么方法可以让 git 在执行我的推送之前先进行拉取吗? (在同一个分支)
当您希望 command2
到 运行 当且仅当 command1
成功时(基本上 returns 0),运行 使用双符号 &&
,夹在它们之间:
command1 && command2
并且,运行command2
在command1
成功后或者即使失败,运行使用分号;
:
command1 ; command2
前一个用于 git pull && git push
,后一个用于 git pull ; date
您实际上不需要在推送之前拉取:如果在远程端有您在本地没有的提交,git 推送将失败并显示消息消息:
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.
如果你可以控制远程服务器,你可以在那里设置:
git config --system receive.denyNonFastForwards true
更一般地说,您可以 easily define an alias combining both commands:
git config alias.pullpush '!git pull && git push'
如果拉取失败,则不会执行推送。
最后,您可以在 bash 脚本中组合任何命令序列,命名为
git-pullpush
(无扩展名,可执行文件,存储在 $PATH
引用的文件夹中)
这将是一个常规的 bash 脚本(甚至可以在 Windows 上运行,因为它会被 msys bash 解释)
#!/bin/bash
# you can add any command you want
git pull && git push
你会用 git pullpush
来称呼它(就像一个别名)
根据 OP 的评论,他们似乎试图避免在最近的提交和远程提交之间进行合并提交,这通常由 git pull
在本地和远程历史记录有分歧。
执行此操作的明确方法是先获取和变基,最后推送。
git fetch
git rebase origin/master
git push
第二种方法是在调用 git pull
时传递 --rebase
。
git pull --rebase
最后,可以通过设置配置使其成为 git pull
的默认行为。
git config --global pull.rebase true
Git 方法是 customize Git hooks。
在您的情况下,您需要转到存储库中的 .git/hooks
目录,并添加一个名为 pre-push
的文件,这是您选择的脚本,例如(本例中为bash)
#!/bin/bash
echo "pulling ..."
git pull
此脚本将在您执行 git push
和实际推送之前调用。
显然,这只是一个非常幼稚的示例,但我希望您能理解。此目录中已有示例。有什么不明白的可以留言。
每次我想推送我的提交 (git commit -m "message"
, git push origin <branch>
),我都会拉 (git pull origin <branch>
)。
有什么方法可以让 git 在执行我的推送之前先进行拉取吗? (在同一个分支)
当您希望 command2
到 运行 当且仅当 command1
成功时(基本上 returns 0),运行 使用双符号 &&
,夹在它们之间:
command1 && command2
并且,运行command2
在command1
成功后或者即使失败,运行使用分号;
:
command1 ; command2
前一个用于 git pull && git push
,后一个用于 git pull ; date
您实际上不需要在推送之前拉取:如果在远程端有您在本地没有的提交,git 推送将失败并显示消息消息:
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.
如果你可以控制远程服务器,你可以在那里设置:
git config --system receive.denyNonFastForwards true
更一般地说,您可以 easily define an alias combining both commands:
git config alias.pullpush '!git pull && git push'
如果拉取失败,则不会执行推送。
最后,您可以在 bash 脚本中组合任何命令序列,命名为
git-pullpush
(无扩展名,可执行文件,存储在 $PATH
引用的文件夹中)
这将是一个常规的 bash 脚本(甚至可以在 Windows 上运行,因为它会被 msys bash 解释)
#!/bin/bash
# you can add any command you want
git pull && git push
你会用 git pullpush
来称呼它(就像一个别名)
根据 OP 的评论,他们似乎试图避免在最近的提交和远程提交之间进行合并提交,这通常由 git pull
在本地和远程历史记录有分歧。
执行此操作的明确方法是先获取和变基,最后推送。
git fetch
git rebase origin/master
git push
第二种方法是在调用 git pull
时传递 --rebase
。
git pull --rebase
最后,可以通过设置配置使其成为 git pull
的默认行为。
git config --global pull.rebase true
Git 方法是 customize Git hooks。
在您的情况下,您需要转到存储库中的 .git/hooks
目录,并添加一个名为 pre-push
的文件,这是您选择的脚本,例如(本例中为bash)
#!/bin/bash
echo "pulling ..."
git pull
此脚本将在您执行 git push
和实际推送之前调用。
显然,这只是一个非常幼稚的示例,但我希望您能理解。此目录中已有示例。有什么不明白的可以留言。