Git rebase interactive the last n commits
Asked Answered
B

7

77

I have made a bunch of unpushed commits in my feature branch and now want to reorder and partly squash belonging commits visually. I reckon the solution somehow lies in the Git interactive, but how to invoke it?

$ git rebase --interactive --onto <the-ID-of-the-first-commit-to-rewrite>

just pops up the VI with a

noop

content followed by commented information. After exiting, my head is reset to the specified commit.

How to correctly trigger the interactive rebase for modifying the commits since a certain commit?

Blackington answered 4/1, 2017 at 13:16 Comment(1)
You're triggering it correctly, you just need to learn how to use it. Read the commented out section, it contains a brief explanation on what you should do.Nigh
S
78

you should use

git rebase --interactive <sha1>

where <sha1> should not be the sha of the first commit you want to rewrite, but the sha of the commit just before.

if your history looks like this:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit

There are two different ways to indicate the commit on which to rebase:

git rebase -i cb85072
git rebase -i 5116d42^

where

  • ^ means the commit just before.
  • -i is just short for --interactive
Stalagmite answered 4/1, 2017 at 13:23 Comment(2)
It looks like the "onto" caused the problems.Blackington
but the sha of the commit just before., this is the secret to everythingBerryman
S
50

You can also step back from your last commit by some number of commits. For example, if you want to rebase last 5 commits you can use this command: git rebase -i HEAD~5.

Slog answered 4/1, 2017 at 14:32 Comment(0)
P
49

To review and rewrite the last n commits, use:

git rebase -i HEAD~n

p, pick = use commit

f, fixup = like "squash", but discard this commit's log message

https://www.freecodecamp.org/forum/t/how-to-squash-multiple-commits-into-one-with-git-squash/13231

Portsmouth answered 15/8, 2019 at 22:38 Comment(0)
N
12

The accepted answer is right

Though, counting n commits to squash and picking the commit id for rebase is tricky

git rebase -i HEAD~[N]   // N is the number of commits, starting from the most recent one

enter image description here

git rebase -i HEAD~[7]

But if u have tons of commit to squash

git rebase -i [commit-id] // [commit-id] is the hash of the commit just before the first one 

git rebase -i 6394dc


Inspired by

Niel answered 20/11, 2019 at 2:38 Comment(0)
D
8

If you want to interactively rebase branch B onto branch A using its last N commits, you can generally do this:

git rebase -i --onto A B~N B

e.g.

git rebase -i --onto master feature~3 feature

Works well also non-interactively - without -i.

Demakis answered 30/4, 2020 at 10:56 Comment(0)
S
1

I miss the action rebase in your instruction:

git rebase -i <id-of-commit>
Skipp answered 4/1, 2017 at 13:22 Comment(1)
Yes, was a typo. I've fixed the question.Blackington
C
1

For anyone trying to run:

git rebase -i <commit_id>^

And gets the following error:

zsh: no matches found: <commit_id>^

The solution is to wrap the commit identifier with quotes like:

git rebase -i "<commit_id>^"
Cognate answered 25/4 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.