Search code examples
gitgithubgit-rebase

What are the best practices for having git clean history?


Reading about git workflows, I was wondering about the appropriateness of history rewriting. My workflow, and I imagine that of many others, is this:

  • Take Github repository, let's call this rep1
  • Make a fork, that'll be rep2
  • git clone it locally to work with, that's rep3
  • Make changes, commit to rep3
  • Once I am finished, push to rep2 and ask others for feedback before making a PR

As I get feedback, it seems like I would want to do some rebasing and squashing, as often the feedback are minor things like rewording comments or things that should have been different in the first place, and don't deserve their own commit.

But from the documentation, it seems like I am not supposed to change history on rep2, and indeed things like --ammend don't work in this scenario. Is my workflow wrong, or am I misunderstanding those warnings about changing history?


Solution

  • The rule about rebase is this: You should only rebase your local branches once the branch is a public branch which anyone can grab it, you should not rebase on this branch again, or you can damage all the other repo who have already cloned this branch.

    If for some reason you do decide to rebase public (pushed branch) you have to make sure all the users who pulled this branch are aware of your rebase.

    You can check it using git pre-rebase hook to decline the rebase or to display a warning or error in that case