Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to squash all commits on `master` branch? [duplicate]

Tags:

git

I'm looking for a way to squash all git commits into a single big commit in master branch. I fully understand the consequences of what I'm trying to do, no need to explain that this is dangerous or that it's not the right way to go - I want to lose all my history and turn this repository into a single big commit.

The main problem is: I have no other living branches, no local commits, and all of the previous commits have already been pushed to remote master.

Hacky scripts are also welcome.

like image 431
milosmns Avatar asked Mar 24 '19 16:03

milosmns


People also ask

How do you squash all commits in a branch?

Squashing by Merging With the –squash Option This can effectively clean the commit-graph in a branch. However, we sometimes make many commits in our feature branch while working on it. After we've developed the feature, we usually want to merge the feature branch to the main branch, say “master”.

How would you squash multiple commits together?

To "squash" in Git means to combine multiple commits into one. You can do this at any point in time (by using Git's "Interactive Rebase" feature), though it is most often done when merging branches. Please note that there is no such thing as a stand-alone git squash command.

How do I merge squash commits to master?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.

How would you squash multiple commits together without using git merge?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.


2 Answers

I would use git reset --soft:

git reset --soft id-of-first-revision-of-master
git commit --amend -m "single commit for master"

Then you can git push --force wherever you need that new branch.

Update

I can think of another simple way to do it, with more git commands:

git checkout --orphan some-branch
git commit -m "First commit"
git branch -f master # move the local branch
git checkout master
git branch -d some-branch # delete the temp branch

It could also be done like this, in a more hackish fashion:

git commit-tree -m "First commit" HEAD^{tree}

That will write a revision.... if you check it out, you will have a single revision, content will be exactly like it is where you were standing before.... feel free to move the local branch (as explained in the previous recipe)

like image 139
eftshift0 Avatar answered Oct 18 '22 02:10

eftshift0


You can still modify the history on the upstream by using git push --force-with-lease. However you have to be aware of the consequences.

Using git push --force will create a parallel tree on your upstream so all the developers may find themselves lost in a legacy branch.

In order to squash your history, simply do:

git rebase -i HEAD~10

Where 10 is the number + 1 of commits you want to squash together. If you want to squash all the commits, then just refer your <first-commit-hash> instead of HEAD~10. Then on the editor you select squash for all the commits you want to group together. You can do search/replace: pick by squash

Once done, simply push your changes:

git push --force-with-lease

I would never recommend to do --force because if another developer has pushed a commit in the meantime you will erase its move. By using --force-with-lease Git will prevent you to push if somebody else has pushed on the top of your last change (see this question for more details).

like image 34
nowox Avatar answered Oct 18 '22 02:10

nowox