Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce the Git "Do not rebase published commits" rule?

Tags:

git

git-rebase

The Pro Git book strongly advises to follow this rule:

Do not rebase commits that exist outside your repository.

Is there a simple way to ensure you're not going to violate the rule? For example a set of Git commands?

I mean in case you're not sure if you published all or part of your recent commits or not (maybe you're involved in several projects that use Git, and you just switched back to one of your projects).

Or maybe this question is just nonsense? I'm not a Git expert.

like image 359
Bludzee Avatar asked Jul 09 '15 13:07

Bludzee


1 Answers

You can get a list of "unpushed" commits in your local repository by comparing your current branch to the upstream branch. E.g., something like:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

This tells us we have one commit that we have not pushed upstream. We can use git log to a list of these commits:

$ git log --oneline origin/master..master
bc9dacf added another file

So there is a list of unpublished commits that we could rebase willy-nilly without impacting any collaborators. As long as you limit yourself to these you're in good shape.

Of course, part of your question was about automating this behavior. There is a pre-rebase hook that is called prior to a rebase operation that could be used to enforce this, but I think I will leave that as an exercise to the reader. I'm not convinced that this is a big problem in practice.

If the upstream project with which you're collaborating has a workflow based on GitHub pull requests, or Gerrit, or something similar, you have a lot less to worry about: because you are never pushing to the shared repository, your changes will simply be rejected if you've managed to rebase your local repository in a way that isn't compatible with the state of the upstream code.

like image 121
larsks Avatar answered Oct 28 '22 00:10

larsks