简体   繁体   中英

Warning if rebasing a branch that's been pushed to remote?

Is there any way to get git to warn you if you attempt to rebase a branch that's already been pushed? (ie in the case where you're likely to be "rewriting history".)

(I realise that in some cases git isn't going to know whether you're rewriting history or not, but often (typically?) it will have this information.)

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM