1

My git object graph looks like below. I want to squash commits 2fb14b7, 39224ad, fe9252d, and 3e7a060 into one single commit.

* 650c464        (temp) adding hw2.txt
| * dd3674a      (master) added hw3
| | * 8dc0857    (FIX_REVIEW_COMMENTS) added hwa
| |/  
| * c39d943      hw2 added in master
| | *   e2a1c13  (HEAD, refs/stash, trying_to_squash) WIP on temp: 2fb14b7 hw7
| | |\  
| | | * 429b1de  index on temp: 2fb14b7 added hw7
| | |/  
| | * 2fb14b7    <-- added hw7
| | * 39224ad    <-- (another_branch) added hw3
| | * fe9252d    <-- hw2 added in master
| | * 3e7a060    <-- adding hw2.txt
| |/  
|/|   
* | ba55177      (old_fixes) added hw4
|/  
* a1ede1f        added another hello world to hw1
* 2ea750a        added hw1

Below is what I tried (and did not understand why it did not work).

Q1. Why does this not squash?

$ git checkout 2fb14b7 -b try_to_commit
$ git merge --squash 3e7a060
 (nothing to squash)Already up-to-date.
$

Q2. What does this do to my repository?

$ git rebase --interactive 3e7a060
$ # I choose pick for the first commit object, and squash for the rest
$ # but somehow this complicates my repository graph even more!
Moeb
  • 10,527
  • 31
  • 84
  • 110
  • 1
    You mean '2fb14b7, 39224ad, fe9252d, and 3e7a060'? – GoZoner May 26 '13 at 22:11
  • 1
    @Moeb can you explain what you mean by interactive rebase "complicating your repository graph even more"? –  May 26 '13 at 22:44
  • @ColdHawaiian: it created new branches, instead of simplifying my repository. – Moeb May 27 '13 at 05:14
  • the reason for that is that you have a some changes stashed on the old HEAD. pop the stash and your repo graph will look nice again. – Chronial May 27 '13 at 10:58
  • @Moeb can you show what your commit graph looks like afterwards? –  May 27 '13 at 13:46

1 Answers1

0

Update from 2021 (8 years later): you would not use git checkout anymore, but git switch

git switch -c try_to_commit 2fb14b7 

Why does this not squash?

Because you are merging a parent (past) commit (3e7a060) to a new one 2fb14b7

| | *   e2a1c13  (HEAD, refs/stash, trying_to_squash) WIP on temp: 2fb14b7 hw7
| | |\  
| | | * 429b1de  index on temp: 2fb14b7 added hw7
| | |/  
| | * 2fb14b7    <-- added hw7                   (more recent commit)
| | * 39224ad    <-- (another_branch) added hw3
| | * fe9252d    <-- hw2 added in master
| | * 3e7a060    <-- adding hw2.txt              (past old commit)

I would rather create the branch on the past commit, and squash-merge the more recent commit:

git switch -c try_to_commit 3e7a060
git merge --squash 2fb14b7    

What does this do to my repository?

 git rebase --interactive 3e7a060

It attempts to replay the current branch try_to_commit on top of 3e7a060: that should allow to pick/squash commits, but don't forget to then replay e2a1c13 on top of the replayed branch.

I would rather rebase the all branch try_to_commit, made from e2a1c13 instead of 3e7a060, and using the Git 2.18, Q2 2018, --rebase-merges option:

git switch -c try_to_commit c39d943
git rebase --rebase-merges --interactive 3e7a060

Note that the message (nothing to squash)Already up-to-date will change with git 2.32 (Q2 2021): a few variants of the informational message "Already up-to-date" has been rephrased.

See commit ad9322d (02 May 2021) by Josh Soref (jsoref).
See commit 80cde95 (02 May 2021) by Eric Sunshine (sunshineco).
(Merged by Junio C Hamano -- gitster -- in commit 5feebdd, 11 May 2021)

merge: fix swapped "up to date" message components

Co-authored-by: Eric Sunshine
Signed-off-by: Josh Soref
Signed-off-by: Eric Sunshine

The rewrite of git-merge(man) from shell to C in 1c7b76b ("Build in merge", 2008-07-07, Git v1.6.0-rc0 -- merge) accidentally transformed the message:

Already up-to-date. (nothing to squash)

to:

(nothing to squash)Already up-to-date.

due to reversed printf() arguments.

This problem has gone unnoticed despite being touched over the years by:

  • 7f87aff ("Teach/Fix pull/fetch -q/-v options", 2008-11-15, Git v1.6.1-rc1 -- merge)
  • bacec47 ("i18n: git-merge basic messages", 2011-02-22, Git v1.7.5-rc1 -- merge)
  • tangentially by bef4830 ("i18n: merge: mark messages for translation", 2016-06-17, Git v2.10.0-rc0 -- merge listed in batch #5)
  • 7560f54 (treewide: correct several , 2017-08-23, Git v2.15.0-rc0 -- merge listed in batch #7) (treewide: correct several "up-to-date" to "up to date", 2017-08-23).

Fix it by restoring the message to its intended order.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250