Git rebase unpushed commits only

Viewed 531

Is there a way to rebase which can only rewrite local history (not yet been pushed). I guess it would fallback to merge if that check is not passed.

Here's the problem boiled down to a simple feature-branch.

git checkout -b feature master
# DO WORK
git commit -m "COMMIT1"
git push -u origin feature # COMMIT1 is public
# DO WORK
git commit -m "COMMIT2"
git fetch origin master:master # background update master with some random commits
git rebase master # This rewrites both COMMIT1 and COMMIT2
git push # fails because COMMIT1 was rewritten
git push -f # banned by Infrastructure

So I'd like something like:

  • git rebase master with something similar to merge --ff-only that fails if it would change public history
  • git pull --rebase but with "rebase-or-merge" semantics
1 Answers

When you do rebase, git keeps old commits in his storage so they are not deleted, but history is changed. Learn rebase.

At first, take a look at reflog. With this utility you can find necessary commits, which were rebased.

Then, simply use soft reset so that you point your feature branch to commit with old sha1 (the same like on the server).

And at last, to add changes of other commits, I would suggest use cherry-pick.

Related