Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:40:44+00:00 2026-05-11T06:40:44+00:00

With git rebase –interactive <commit> you can squash any number of commits together into

  • 0

With git rebase --interactive <commit> you can squash any number of commits together into a single one.

That’s all great unless you want to squash commits into the initial commit. That seems impossible to do.

Are there any ways to achieve it?


Moderately related:

In a related question, I managed to come up with a different approach to the need of squashing against the first commit, which is, well, to make it the second one.

If you’re interested: git: how to insert a commit as the first, shifting all the others?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:40:44+00:00Added an answer on May 11, 2026 at 6:40 am

    Update July 2012 (git 1.7.12+)

    You now can rebase all commits up to root, and select the second commit Y to be squashed with the first X.

    git rebase -i --root master  pick sha1 X squash sha1 Y pick sha1 Z 
    git rebase [-i] --root $tip 

    This command can now be used to rewrite all the history leading from "$tip" down to the root commit.

    See commit df5df20c13 (rebase -i: support –root without –onto, 2012-06-26) on GitHub from Chris Webb (arachsys).

    As noted in the comments, a git push --force-with-lease (safer than --force, as Mikko Mantalainen reminds us) would be needed after any rebase operation, if you need to publish that rework in a remote repository.


    Original answer (February 2009)

    I believe you will find different recipes for that in the SO question "How do I combine the first two commits of a git repository?"

    Charles Bailey provided there the most detailed answer, reminding us that a commit is a full tree (not just diffs from a previous states).
    And here the old commit (the "initial commit") and the new commit (result of the squashing) will have no common ancestor.
    That mean you can not "commit --amend" the initial commit into new one, and then rebase onto the new initial commit the history of the previous initial commit (lots of conflicts)

    (That last sentence is no longer true with git rebase -i --root <aBranch>)

    Rather (with A the original "initial commit", and B a subsequent commit needed to be squashed into the initial one):

    1. Go back to the last commit that we want to form the initial commit (detach HEAD):

      git checkout <sha1_for_B> 
    2. Reset the branch pointer to the initial commit, but leaving the index and working tree intact:

      git reset --soft <sha1_for_A> 
    3. Amend the initial tree using the tree from ‘B’:

      git commit --amend 
    4. Temporarily tag this new initial commit (or you could remember the new commit sha1 manually):

      git tag tmp 
    5. Go back to the original branch (assume master for this example):

      git checkout master 
    6. Replay all the commits after B onto the new initial commit:

      git rebase --onto tmp <sha1_for_B> 
    7. Remove the temporary tag:

      git tag -d tmp 

    That way, the "rebase --onto" does not introduce conflicts during the merge, since it rebases history made after the last commit (B) to be squashed into the initial one (which was A) to tmp (representing the squashed new initial commit): trivial fast-forward merges only.

    That works for "A-B", but also "A-...-...-...-B" (any number of commits can be squashed into the initial one this way)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try this: private bool FitsMask(string sFileName, string sFileMask) { Regex… May 11, 2026 at 3:35 pm
  • added an answer I've just released an open source project which is a… May 11, 2026 at 3:35 pm
  • added an answer It's much easier than above and no reflection required. Linq… May 11, 2026 at 3:35 pm

Related Questions

With git rebase --interactive <commit> you can squash any number of commits together into
I have a local git repository created with git svn clone . I make
How do I easily undo a git rebase? A lengthy manual method is: checkout
I've been using Git now for a couple of months on a project with
I have a branch that should be available to other contributors and that should

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.