StormsEngineering
StormsEngineering

Reputation: 686

How to interactive rebase with the most recent commit

Is there a way to use interactive rebase to edit the most recent commit in git? (Like changing the commit message or squashing commits, preferably with interactive rebasing) I want to squash my most recent commit and the one before that. This is to make a cleaner code base since I accidentally unselected a file when I committed.

I am currently trying to use this: git rebase head~2

But it doesn't have my most recent commit. I assume this has something todo with the HEAD, but I am still learning git.

Edit:

It does have the most recent commit, but at the bottom of the commits, which is confusing going from the github desktop to github command line. Since github desktop lists them from top to bottom instead of bottom to top.

Upvotes: 0

Views: 1243

Answers (2)

jthill
jthill

Reputation: 60625

Don't bother, for squashing a few tip commits just reset your parent and commit:

git reset --soft @~2
git commit

or you can preserve the previous commit's message with the current commit's contents with

git reset --soft @~
git commit --amend

and if you want to mix-and-match content and messages there's options to take the commit message from any commit you like, see the git commit docs.

Upvotes: 2

moebius
moebius

Reputation: 2279

You need to add the -i flag to do an interactive rebase:

git rebase -i HEAD~2

This will bring up the interactive screen like shown below:

pick 596d1e1 fix default image url
pick 924430f fix about page link

# Rebase 0f3ffa3..924430f onto 0f3ffa3 (2 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

Your commits are listed from oldest to latest at the top of the file.For eg. 924430f is my latest commit.

If you want to squash your latest commit onto the previous commit, edit the start of the latest commit line to read squash rather than pick:

pick 596d1e1 fix default image url
squash 924430f fix about page link

Upvotes: 0

Related Questions