0

I am strugging with this git rebase/squash.

I currently have 4 commits of code, these 4 commits have multiple changes on them.

I want to make all these 4 commits into 1, doesnt matter if I lose or not the commit messages.

So I tried this:

Squash my last X commits together using Git

git rebase -i HEAD~4.

Dont know in what part of my commit history sent me but I had many modified files and couple of them were outdated with the last commit.

Revert all to origina and then I tried

git reset --hard HEAD~5
git merge --squash HEAD@{1}

git reset worked and sent me to a commit, but then the --squash just couldnt perform

git merge with --no-ff and --squash

So basically couldnt use it either, couldnt figure it out how to the no-ff works without having tons of things to change

Is there any way that I could make it work?

1
  • Please could you tell us why git merge --squash HEAD@{1} didn't work? Did you maybe get conflicts? Commented Oct 23, 2018 at 9:14

3 Answers 3

2

When I want to make many commits into a single one, I git reset --soft and then commit.

Suppose it's the last 4 commits I want to squash into a single revision:

git reset --soft HEAD~4
git commit -m "Turning 4 revisions into a single one"

Done!

0

When you run git rebase -i HEAD~4, git will open a text editor with a file that looks something like the following:

pick fc7d27a Change 1
pick 09dd4e6 Change 2
pick 683af92 Change 3
pick a72e15a Change 4

# Rebase 3d8944f..a72e15a onto 3d8944f (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
#   However, if you remove everything, the rebase will be aborted.
#
#   
# Note that empty commits are commented out

This "open a file in a text editor" is an interface that allows you to tell git what you want to do then the rebase.

Change the pick to squash (or just s) for the commits you want to squash into the previous commit. e.g: Then save and exit your editor.

pick fc7d27a Change 1
squash 09dd4e6 Change 2
squash 683af92 Change 3
squash a72e15a Change 4

Change 2, 3 and 4 will be squashed into change 1.

6
  • when you say just "S" means press the s key? I thought this was a log message and I wrote :wq!
    – jpganz18
    Commented Oct 23, 2018 at 9:15
  • When you run git rebase -i, it opens a text editor which is an interface that allows you to tell git exactly what you want it to do. It sounds like the editor is vim, so you want to press i to get into insert mode, then change the pick to squash. Then press escape, and then :wq. See scotch.io/tutorials/… to learn more on vim. You could also change the text editor that git uses to something you are more comfortable with. See stackoverflow.com/questions/2596805/… Commented Oct 23, 2018 at 9:21
  • when running this command I see only the first commit of my 4, commits before that came before my branch, what should I do in this case?
    – jpganz18
    Commented Oct 23, 2018 at 9:27
  • Are you sure you are running git rebase -i HEAD~4 ? Are you sure you are starting in the previous state. Maybe check your git log Commented Oct 23, 2018 at 9:29
  • when I make git log I see 4 commits of this particular branch, all previous commits of came from the remote master
    – jpganz18
    Commented Oct 23, 2018 at 9:33
-1
git reset --hard HEAD~5
git merge --squash HEAD@{1}

After you do this, you need to run git commit

1
  • this throws an error as I mentioned before: fatal: You cannot combine --squash with --no-ff.
    – jpganz18
    Commented Oct 23, 2018 at 9:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.