jpganz18
jpganz18

Reputation: 5858

Squash/rebase last commits without losing any change for git?

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?

Upvotes: 0

Views: 2170

Answers (3)

eftshift0
eftshift0

Reputation: 30212

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!

Upvotes: 2

Gary van der Merwe
Gary van der Merwe

Reputation: 9533

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.

Upvotes: 0

Gary van der Merwe
Gary van der Merwe

Reputation: 9533

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

After you do this, you need to run git commit

Upvotes: -1

Related Questions