Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git be configured to prevent rebase of already published commits?

Tags:

git

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.
In this case the merge on pull is mandatory.

Is there a way to configure git so it rejects the rebase if some of the commits that are going to be rebased was already pushed to other remote?

like image 416
xsilmarx Avatar asked May 31 '15 11:05

xsilmarx


People also ask

Does git rebase create new commits?

The Rebase Option But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

Does git rebase rewrite history?

Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.


1 Answers

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

like image 149
Joseph K. Strauss Avatar answered Sep 23 '22 02:09

Joseph K. Strauss