I want to use the git pull --rebase
instead of merge but with this approach one can accidentally rebase commits that were already pushed to another remote.
You can write hook that check to see if any commit within the range of the commits are already in the targeted branch, if so do what ever you want (like rejecting the push)
Using the git commands git branch --contains <commit1> ... <commit_n>
you can check if the give branch contains any of the given commits.
git does not have anything out of the box for you, you will have to write some custom made code to do it, but you can use the git branch --contains
to find out if the branch already has the given commit in it.
Try this command:
git rebase --onto <remote>/<branch-name> $(git rev-list HEAD \
--not --exclude=$(git rev-parse --symbolic-full-name HEAD) \
--glob=refs/* --reverse | head -1)~
This will rebase only commits that were done on the current local branch.
If you want to include local changes to other branches that were not pushed yet change the --glob=refs/*
expression to --remotes
. Please be aware, though that you may push these local branches in the future, so use with caution.
Clarification:
Of course, since you are not using git pull
, you will need to execute a git fetch
prior to rebasing. (I happen to prefer git fetch
+ git rebase
or git merge
, so that I can be in control of what I am rebasing onto or merging.)