问题
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?
回答1:
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
回答2:
General rule of thumb: Never rewrite history that's already been published. Chances are that someone may have already fetched your commits; if you rewrite the history, those persons will be in trouble as soon as they pull the rewritten history.
Less strict rule of thumb: In some cases, it may be considered okay to rewrite published history when you are absolutely sure that you are the only one working on that branch.
Personally, I would consider it okay to rewrite the history of an already-pushed feature branch in an invidivual fork of a GitHub repository, because I would not expect anyone else to be working on that branch. That's just a personal opinion, though.
来源:https://stackoverflow.com/questions/29767077/what-are-the-best-practices-for-having-git-clean-history