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?

jpganz18
  • 5,508
  • 17
  • 66
  • 115

3 Answers3

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!

eftshift0
  • 26,375
  • 3
  • 36
  • 60
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.

Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80
  • when you say just "S" means press the s key? I thought this was a log message and I wrote :wq! – jpganz18 Oct 23 '18 at 09: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 https://scotch.io/tutorials/getting-started-with-vim-an-interactive-guide to learn more on vim. You could also change the text editor that git uses to something you are more comfortable with. See https://stackoverflow.com/questions/2596805/how-do-i-make-git-use-the-editor-of-my-choice-for-commits – Gary van der Merwe Oct 23 '18 at 09: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 Oct 23 '18 at 09: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` – Gary van der Merwe Oct 23 '18 at 09:29
  • when I make git log I see 4 commits of this particular branch, all previous commits of came from the remote master – jpganz18 Oct 23 '18 at 09:33
  • I realised I see plenty of commits because in a point there was a merge with master, so it were added as part of this commit, is it possible to do something with this? or is it possible to squash by commit? – jpganz18 Oct 23 '18 at 09:52
-1
git reset --hard HEAD~5
git merge --squash HEAD@{1}

After you do this, you need to run git commit

Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80