Skip to content
Fix Code Error

How can I merge two commits into one if I already started rebase?

March 13, 2021 by Code Error
Posted By: Anonymous

I am trying to merge 2 commits into 1, so I followed “squashing commits with rebase” from git ready.

I ran

git rebase --interactive HEAD~2

In the resulting editor, I change pick to squash and then save-quit, but the rebase fails with the error

Cannot ‘squash’ without a previous commit

Now that my work tree has reached this state, I’m having trouble recovering.

The command git rebase --interactive HEAD~2 fails with:

Interactive rebase already started

and git rebase --continue fails with

Cannot ‘squash’ without a previous commit

Solution

Summary

The error message

Cannot ‘squash’ without a previous commit

means you likely attempted to “squash downward.” Git always squashes a newer commit into an older commit or “upward” as viewed on the interactive rebase todo list, that is into a commit on a previous line. Changing the command on your todo list’s very first line to squash will always produce this error as there is nothing for the first commit to squash into.

The Fix

First get back to where you started with

$ git rebase --abort

Say your history is

$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a

That is, a was the first commit, then b, and finally c. After committing c we decide to squash b and c together:

(Note: Running git log pipes its output into a pager, less by default on most platforms. To quit the pager and return to your command prompt, press the q key.)

Running git rebase --interactive HEAD~2 gives you an editor with

pick b76d157 b
pick a931ac7 c

# Rebase df23917..a931ac7 onto df23917
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

(Notice that this todo list is in the reverse order as compared with the output of git log.)

Changing b’s pick to squash will result in the error you saw, but if instead you squash c into b (newer commit into the older or “squashing upward”) by changing the todo list to

pick   b76d157 b
squash a931ac7 c

and save-quitting your editor, you’ll get another editor whose contents are

# This is a combination of 2 commits.
# The first commit's message is:

b

# This is the 2nd commit message:

c

When you save and quit, the contents of the edited file become commit message of the new combined commit:

$ git log --pretty=oneline
18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c
df239176e1a2ffac927d8b496ea00d5488481db5 a

Note About Rewriting History

Interactive rebase rewrites history. Attempting to push to a remote that contains the old history will fail because it is not a fast-forward.

If the branch you rebased is a topic or feature branch in which you are working by yourself, no big deal. Pushing to another repository will require the --force option, or alternatively you may be able, depending on the remote repository’s permissions, to first delete the old branch and then push the rebased version. Examples of those commands that will potentially destroy work is outside the scope of this answer.

Rewriting already-published history on a branch in which you are working with other people without very good reason such as leaking a password or other sensitive details forces work onto your collaborators and is antisocial and will annoy other developers. The “Recovering From an Upstream Rebase” section in the git rebase documentation explains, with added emphasis.

Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history. This section explains how to do the fix from the downstream’s point of view. The real fix, however, would be to avoid rebasing the upstream in the first place. …

Answered By: Anonymous

Related Articles

  • Git workflow and rebase vs merge questions
  • In git, what is the difference between merge…
  • Checkout another branch when there are uncommitted…
  • In plain English, what does "git reset" do?
  • Squash the first two commits in Git?
  • How to cherry pick a range of commits and merge into…
  • Why do I have to "git push --set-upstream origin "?
  • Git merge with force overwrite
  • sql query to find priority jobs
  • What's the difference between HEAD^ and HEAD~ in Git?
  • git pull while not in a git directory
  • Recalculate merge conflicts (ie. how to generate…
  • Git Using Remote Branch
  • How to modify existing, unpushed commit messages?
  • How do I make a Git commit in the past?
  • Change old commit message on Git
  • Combining multiple commits before pushing in Git
  • How can I reconcile detached HEAD with master/origin?
  • How do I 'overwrite', rather than 'merge', a branch…
  • Git Cherry-pick vs Merge Workflow
  • Ukkonen's suffix tree algorithm in plain English
  • How to recover stashed uncommitted changes
  • Retrieve specific commit from a remote Git repository
  • How can I combine two commits into one commit?
  • Setting up and using Meld as your git difftool and mergetool
  • Why does git perform fast-forward merges by default?
  • Why do git fetch origin and git fetch : behave differently?
  • What's the difference between "git reset" and "git…
  • rebase in progress. Cannot commit. How to proceed or…
  • Practical uses of git reset --soft?
  • How to squash commits in git after they have been pushed?
  • Various ways to remove local Git changes
  • How to fix Git error: object file is empty?
  • master branch and 'origin/master' have diverged, how…
  • git lfs push to github failure on Ubuntu 18.04
  • How can I move HEAD back to a previous location?…
  • How do I revert a Git repository to a previous commit?
  • Git submodule update
  • Why call git branch --unset-upstream to fixup?
  • Remove sensitive files and their commits from Git history
  • Why does Git say my master branch is "already up to…
  • How to make the script wait/sleep in a simple way in unity
  • How can I set up an editor to work with Git on Windows?
  • How to pull in changes from skeleton sub-repository…
  • Start redis-server with config file
  • error LNK2005: ✘✘✘ already defined in…
  • How to track untracked content?
  • Is it safe to shallow clone with --depth 1, create…
  • What is your most productive shortcut with Vim?
  • How to change the commit author for one specific commit?
  • Git submodule head 'reference is not a tree' error
  • How to use Visual Studio Code as Default Editor for Git
  • How to revert multiple git commits?
  • How to copy commits from one branch to another?
  • How to access this variable from my store / state in…
  • Does "git fetch --tags" include "git fetch"?
  • How to retrieve a single file from a specific…
  • "git rm --cached x" vs "git reset head --​ x"?
  • Git - Pushing code to two remotes
  • Why does git say "Pull is not possible because you…
  • Typescript: Type a group of partially applied…
  • How do I set initial state in Vuex 2?
  • Merge (with squash) all changes from another branch…
  • How to remember things when it comes to mixture of…
  • Pandas Merging 101
  • Your configuration specifies to merge with the from…
  • Simplest way to create Unix-like continuous pipeline…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to resolve…
  • UnboundLocalError: local variable 'event' referenced…
  • Break a previous commit into multiple commits
  • CKEditor setData not clearing or changing text in rte
  • How to get an input after a with open?
  • How to use git merge --squash?
  • How do I exit the Vim editor?
  • How to merge remote master to local branch
  • Polymer 2 dynamically merging templates
  • Git merge branch differences
  • How to remove selected commit log entries from a Git…
  • Pure JavaScript equivalent of jQuery's $.ready() -…
  • render function or template not defined in…
  • Why did my Git repo enter a detached HEAD state?
  • git cherry-pick says "...38c74d is a merge but no -m…
  • How to find the nearest parent of a Git branch?
  • When to use extern in C++
  • What is trunk, branch and tag in Subversion?
  • When would you use the different git merge strategies?
  • Merge, update, and pull Git branches without using checkouts
  • How to cherry-pick multiple commits
  • How to git reset --hard a subdirectory?
  • How do I close a tkinter window?
  • How can I selectively merge or pick changes from…
  • Git: How to squash all commits on branch
  • Move the most recent commit(s) to a new branch with Git
  • How To Edit Date and Time Picker Vuejs Vuetify From…
  • What exactly does the "u" do? "git push -u origin…
  • Skip Git commit hooks
  • How does GitLab merge request with squash work under…
  • VueJS and tinyMCE, custom directives
  • Mobile Safari "swallowing" events in Backbone.js app

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to access POST form fields

Next Post:

How to change to an older version of Node.js

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error