Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: remove duplicate commits from public branch?

Tags:

git

rebase

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?

like image 435
michael Avatar asked Apr 26 '11 19:04

michael


People also ask

How do I remove a repeated commit in git?

If you want to clean things up, then the thing to do is a git rebase --interactive and remove the merge commits.

Does cherry-pick duplicate 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.

Why you should rebase instead of merge?

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 .

What is git pull rebase?

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.


1 Answers

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.

like image 191
Eugene Sajine Avatar answered Oct 31 '22 12:10

Eugene Sajine