git
如果您尝试重新设置已经推送的分支,有什么方法可以警告您?(即在您可能“改写历史”的情况下。)
(我意识到在某些情况下git
不会知道您是否正在重写历史记录,但通常(通常?)它会有这些信息。)
You can use git's pre-rebase
hook to produce a warning or error in that situation. (The documentation for the various githooks
is here.) The sample pre-rebase
hook does something very similar to what you want, although (as http://git-scm.com/book/en/Customizing-Git-Git-Hooks points out) you'll need to change the branch name next
to whatever your published branch is called.
It's probably also worth pointing out (as pmr mentions in a comment above) that if you've rewritten public history, then attempting to push that rewritten branch will not succeed - you'll get an error to avoid just this problem.
Furthermore, you should know that you can usually safely do:
git rebase <upstream-remote-tracking-branch>
For example, if you're working on the master
branch, and its upstream remote-tracking branch is origin/master
, then:
git rebase origin/master
... will only consider reapplying commits that aren't contained in origin/master
, and that remote-tracking branch is updated when you push to master
in origin
as well as when you fetch from origin
. (The reason I say "usually" above is that this is more complicated if you're using multiple remotes, or you've set up remote-tracking branches in an unusual way.)