I've somehow got some duplicate commits in a branch, each with the same changes. It's a public branch, merged from several branches by several users. I need to periodically rebase this branch onto the master branch of another repo, and these duplicates make it clumsy.
Is there a way to remove these and push them to the public repo w/out making it complicated for other users working from the branch?
If you want to clean things up, then the thing to do is a git rebase --interactive and remove the merge commits.
The reason why you should use cherry-pick rarely is that it easily creates "duplicate" commits: when you integrate a commit into your HEAD branch using cherry-pick, Git has to create a new commit with the exact same contents. It is, however, a completely new commit object with its own, new SHA identifier.
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. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge .
Git pull rebase is a method of combining your local unpublished changes with the latest published changes on your remote. Let's say you have a local copy of your project's main branch with unpublished changes, and that branch is one commit behind the origin/main branch.
filter-branch is not necessary in this case IMHO and as Jefromi mentioned without making life a bit complicated for everybody else it is impossible. First rule of Git - do not rewrite published history.
If you really want to cleanup the branch that has got messed up, then you should rebase it locally, rearrange commits and force-push it to mainline if needed.
in order to do that (imagine that the branch is checked out locally and the last known good state after which you have started to get those duplicates is 20 commits ago)
git checkout yourPublicBranch
git rebase -i HEAD~20
This will fire up the editor in which you will be able to manage the commits. Then you will have to save the file and exit for rebase to start working. That may lead to conflicts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With