Git squash story after the merger

I have combined upstream from a large project with my local git repository. Before the merger, I had a small amount of history that was easy to read, but after the merger, a huge amount of the story is now in my repo. I do not need the whole story related to the repository up.

After this upward merge, other commits were made that I would like to keep. How can I squeeze the whole story that has been merged from the upstream into one commit while maintaining the commits after merging upstream?

+8
git git-merge squash
source share
3 answers

The solution I ended up using was to manually recreate the story. I did this mainly because I did not want to spend too much time on a refined solution, and there was no such story (about 30 commits that I would have to manually combine).

So, I created a branch before merging a huge stream up:

git checkout -b remove-history-fix <commit ID before merge> 

Then recombine the upstream using the --squash .

 git merge --squash <upstream tag> 

Then, manually, the cherry picked commits after merging with the old branch (the one that had a huge amount of backstory).

 git cherry-pick <commit ID> 

After all these commits were merged into my remove-history-fix branch, I deleted the branch with the previous history.

 git branch -D <upstream-history-branch> 
+6
source share

There is no way to do this, since you cannot return to this remote repository or any other project from the same project again or again. When crushing, you change the history, resulting in various sha1 hashes between your repository and the remote.

You will have to live with a great story.

+1
source share

A few options for you:

Registration Limit

Not quite what you asked for, but maybe a good alternative, and much easier. This allows you to use git as usual, but it hides everything you donโ€™t want to see (assuming the problem is the story cluttering your log and not the raw storage space. I think the merge crush in your branch wonโ€™t prevent git from including all commits from the upstream if you first chose the upstream for the merge action.).

In this case, you should do a normal merge, but when registering, you add --first-parent to the command.

For example, without the option that I could have (suppose the โ€œlarger sampleโ€ from 1 to 3 was actually much more significant)

 $ git log --oneline 0e151bf Merge remote-tracking branch 'origin/master' into nosquash f578cbb sample more 3 7bc88cf sample more 2 682b412 sample more 1 fc6e1b3 Merge remote-tracking branch 'origin/master' 29ed293 More stuff 9577f30 my local change 018cb03 Another commit a5166b1 Initial 

But, if I add --first-parent , it will clear up to this:

 $ git log --oneline --first-parent 0e151bf Merge remote-tracking branch 'origin/master' fc6e1b3 Merge remote-tracking branch 'origin/master' 9577f30 my local change 018cb03 Another commit a5166b1 Initial 

Pay attention to all the commits from the master after I forked ("my local change" is my diverging message) has disappeared. Only the commits I made appeared, including when I merged. If I used more efficient compilation messages during a merge, I might even know what a batch of changes is.

Replace story

This is for what you requested.

Using https://git-scm.com/book/en/v2/Git-Tools-Replace

What we will do here is squash the deleted history, replace their history with our compressed version from our point of view and combine the compressed version.

In my example repository, the changes that were added at the top, which I have not yet merged, were 682b412 "sample greater than 1" in origin / master (f578cbb "sample more 3") (although not so long for this example, pretend there are 50 commits or something in between). A.

The first thing I want is the local branch of the remote side:

 git checkout -b squashing origin/master 

Next, I want to quickly squash him

 git reset --soft 682b412~ git commit -m "Squashed upstream" 

Note the tilde character ~ . This causes our branch to be at the parent of the first commit in the range that we want to squash, and since we specified --soft , our index is still in the last commit in the range that we want to squash. The commit line results in one commit, consisting of what was from the first to the last, inclusive.

At this point, the origin / master and squashing branches have the same tree, but different stories.

Now we will tell git that when it sees links to the original origin / master commit, use a compressed commit instead. Using git log , I see that the new "Squashed upstream" commit is 1f0bc14, so we do:

 git replace f578cbb 1f0bc14 

In this case, your git will use a "compressed upstream" commit.

Let's go back to our original branch (if it was a "host")

 git checkout master git merge f578cbb 

It seems that combining the initial master (f578cbb) actually gets the contents of 1f0bc14, but registers it as having the parent SHA1 f578cbb

We no longer need a decoupling branch, so you can get rid of it.

Now, let's say, more features have been added upstream. In this simple example, in the upstream repository, the log can show this:

 84f5044 new feature f578cbb sample more 3 7bc88cf sample more 2 682b412 sample more 1 29ed293 More stuff 018cb03 Another commit a5166b1 Initia 

After we pick upstream, if we look at his journal from our repo, we see this instead:

 84f5044 new feature f578cbb squashed upstream 29ed293 More stuff 018cb03 Another commit a5166b1 Initial 

Pay attention to how, apparently, we also had a distribution story, and more importantly, SHA1 squeezed up shows the one used in the upstream history (for them, this is really โ€œan assembly of more than 3โ€).

So the merge continues to work as usual

 git merge origin/master 

But we do not have such a cluttered magazine:

 4a9b5b7 Merge remote-tracking branch 'origin/master' for new feature 46843b5 Merge remote-tracking branch 'origin/master' 84f5044 new feature f578cbb squashed upstream fc6e1b3 Merge remote-tracking branch 'origin/master' 29ed293 More stuff 9577f30 my local change 018cb03 Another commit a5166b1 Initial 

If the "new function" was fixed in the upstream, there were also a large number of commits, we could repeat this process to squeeze it down.

+1
source share

All Articles