28

I'm trying to rebase and squash all my commits from current branch to master. Here is what I'm trying to do:

git checkout -b new-feature

make a couple of commits, after it I was trying:

git rebase -i master

in this case commits will remain in new-feature branch

git checkout master
git rebase -i new-feature

It gives me and edit window with noop message.

I know about command:

git merge --squash new-feature

But I'm currently working on learning of rebase command.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81

2 Answers2

59

Lets go though the steps.

1 - We create a new feature branch

git checkout -b new-feature

2 - Now you can add/remove and update whatever you want on your new branch

git add <new-file>
git commit -am "Added new file"
git rm <file-name>
git commit -am "Removed a file"
cat "add more stuff to file" >> <new-file>
git commit -am "Updated files"

3 - Next, pick and squash any commits down into one nice pretty commit message

git rebase -i master

The key thing you need to remember here is to change the text that says "pick" to "squash" for all of the commits after the first commit. This will squash all of the commits down to your master branch.

4 - Select the master branch

git checkout master

5 - Move the HEAD and the master branch to where new-feature is:

git rebase new-feature

You can try all of the commands out in this visual tool: http://pcottle.github.io/learnGitBranching/

Joe
  • 1,320
  • 13
  • 11
  • 6
    You will probably get a 'cannot squash without previous commit' message doing this. http://stackoverflow.com/questions/2563632/how-can-i-merge-two-commits-into-one – backdesk Oct 25 '13 at 10:57
  • 1
    Nice... though I will usually squash my commits `git rebase -i HEAD~##` before the pull/rebase `git pull --rebase origin master` I just like doing it in two steps which lets me review before/after the rebase. – Tracker1 Aug 16 '16 at 18:04
  • 1
    This answer looks more suitable than the one picked – fernandopasik Sep 09 '16 at 18:49
  • 1
    I'd better donot rebase master: `git merge --ff new-feature` instead of `git rebase new-feature` – Eugen Konkov Dec 20 '16 at 15:30
  • A complete answer should go over how to merge in the latest changes to master into the feature branch – Ozymandias Jan 24 '19 at 00:43
  • Is there really no command to `git rebase -i` with default of squashing all? I find myself nearly always wanting to squash my whole feature branch and am surprised there doesn't seem to be a quick shortcut for this (that doesn't involve first counting the precise number of commits, or typing "s" manually next to every commit during the rebase). – Freewalker Nov 27 '19 at 13:53
7

When rebasing, Git will not move commits to another branch. It will move the branch including all its commits. If you want to get the commits into master after rebasing on top of it, use git merge <branch tip or commit of branch> to fast-forward the master branch to that commit.

knittl
  • 246,190
  • 53
  • 318
  • 364