Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase interactive the last n commits

Tags:

git

git-rebase

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?

like image 394
Thomas S. Avatar asked Jan 04 '17 13:01

Thomas S.


People also ask

What does git rebase -- Interactive do?

The interactive rebase gives you a script that it's going to run. It will start at the commit you specify on the command line ( HEAD~3 ) and replay the changes introduced in each of these commits from top to bottom. It lists the oldest at the top, rather than the newest, because that's the first one it will replay.

How do I continue Interactive rebase?

When you're finished making all your changes, you can run git rebase --continue . As before, Git is showing the commit message for you to edit. You can change the text ( "i cant' typ goods" ), save the file, and close the editor. Git will finish the rebase and return you to the terminal.

Does rebase create new commits?

The Rebase Option But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.


3 Answers

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
like image 177
Chris Maes Avatar answered Oct 19 '22 17:10

Chris Maes


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.

like image 37
Oleksandr Kobylianskyi Avatar answered Oct 19 '22 19:10

Oleksandr Kobylianskyi


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

like image 28
Golfer Avatar answered Oct 19 '22 18:10

Golfer