Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebase many commits to one in Git. What am I doing wrong?

Tags:

git

I would like to combine many commits in to one. I have followed the method described at a previous StackOverflow answer which says:

# Go back to the last commit that we want to form the initial commit (detach HEAD)
git checkout <sha1_for_B>

# reset the branch pointer to the initial commit,
# but leaving the index and working tree intact.
git reset --soft <sha1_for_A>

# amend the initial tree using the tree from 'B'
git commit --amend

# temporarily tag this new initial commit
# (or you could remember the new commit sha1 manually)
git tag tmp

# go back to the original branch (assume master for this example)
git checkout master

# Replay all the commits after B onto the new initial commit
git rebase --onto tmp <sha1_for_B>

# remove the temporary tag
git tag -d tmp

I assume that B in the above code block is the oldest commit.

Here is what happens to me:

$ cd rebase-test
$ git branch
  gui
* master
$ git log --pretty=oneline
7391d1b8e51b766190794ede49e3338307a64225 Merge branch 'gui'
c69d44b0d3615d4f537ca42fe67ee58e2728a31a Work in progress. Next port transform()
e710a839c5aee0b07178da1f97999fa6dba445d6 audio_load() implemeted in callback.c
...
$ git checkout c69d44b0d3615d4f537ca42fe67ee58e2728a31a
Note: moving to 'c69d44b0d3615d4f537ca42fe67ee58e2728a31a' which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at c69d44b... Work in progress. Next port transform()
$ git reset --soft 7391d1b8e51b766190794ede49e3338307a64225
$ git commit --amend
[detached HEAD ad4e92a] new
$ git tag tmp
$ git checkout gui
Previous HEAD position was ad4e92a... new
Switched to branch 'gui'
$ git rebase --onto tmp c69d44b0d3615d4f537ca42fe67ee58e2728a31a
First, rewinding head to replay your work on top of it...
Applying: Removed build files
Applying: Removed more build files
Applying: Fixed infile_handler crash
/home/louise/rebase-test/.git/rebase-apply/patch:90: space before tab in indent.
        for(int j = 0; j < data->audio_info_load->channels; j++) {
/home/louise/rebase-test/.git/rebase-apply/patch:91: space before tab in indent.
          if(j == selected_channel) {
/home/louise/rebase-test/.git/rebase-apply/patch:92: space before tab in indent.
            data->mono_channel[while_counter * const_frames_read + i] = bufferIn[i * data->audio_info_load->channels + selected_channel];
/home/louise/rebase-test/.git/rebase-apply/patch:93: space before tab in indent.
          }
/home/louise/rebase-test/.git/rebase-apply/patch:94: space before tab in indent.
        }
warning: 5 lines add whitespace errors.
Applying: sf_readf_double() crashes
Applying: Crash fixed
Applying: Created audio_load()
/home/louise/rebase-test/.git/rebase-apply/patch:73: space before tab in indent.
        for(int j = 0; j < data->audio_info_load->channels; j++) {
/home/louise/rebase-test/.git/rebase-apply/patch:74: space before tab in indent.
          if(j == selected_channel) {
/home/louise/rebase-test/.git/rebase-apply/patch:75: space before tab in indent.
            data->mono_channel[while_counter * const_frames_read + i] = bufferIn[i * data->audio_info_load->channels + selected_channel];
/home/louise/rebase-test/.git/rebase-apply/patch:76: space before tab in indent.
          }
/home/louise/rebase-test/.git/rebase-apply/patch:77: space before tab in indent.
        }
warning: 5 lines add whitespace errors.
Applying: Clean up
/home/louise/rebase-test/.git/rebase-apply/patch:58: trailing whitespace.

/home/louise/rebase-test/.git/rebase-apply/patch:60: trailing whitespace.

/home/louise/rebase-test/.git/rebase-apply/patch:67: trailing whitespace.

/home/louise/rebase-test/.git/rebase-apply/patch:72: trailing whitespace.

/home/louise/rebase-test/.git/rebase-apply/patch:80: trailing whitespace.

warning: squelched 11 whitespace errors
warning: 16 lines add whitespace errors.
Applying: transform_inv() implemented
/home/louise/rebase-test/.git/rebase-apply/patch:115: trailing whitespace.
  free(data->mono_channel); 
warning: 1 line adds whitespace errors.
Applying: audio_save() crash
Applying: Crash fixed
Applying: Backend finally implemented in gui.
$ git tag -d tmp
Deleted tag 'tmp'

I get the same error, when I assume that A is the oldest.

Can someone see what's going wrong?

Hugs, Louise

Edit: I have updated the output, so it shows what happens when I continue.

like image 820
Louise Avatar asked Nov 13 '09 15:11

Louise


People also ask

How do I rebase multiple commits in one?

Suppose that you want to merge the last 3 commits into a single commit. To do that, you should run git rebase in interactive mode ( -i ) providing the last commit to set the ones that come after it. Here, HEAD is the alias of the very last commit. Note that HEAD~3 means three commits prior to the HEAD .

What is the main issue with git rebase?

The golden rule of git rebase is to never use it on public branches. The rebase moves all of the commits in main onto the tip of feature . The problem is that this only happened in your repository. All of the other developers are still working with the original main .

How can you move from 4 commits to one commit?

You can use git merge --squash to squash the commits into a single one while merging into the branch. All the commits in original-branch will be merged into a single one, and applied to the target-branch .


1 Answers

If what you want to do is make this:

A-B-C-D-E

into:

A-BCD-E

You can simply give this command:

$ git rebase -i <sha1_for_A>

And then edit this:

pick B
pick C
pick D
pick E

to read:

pick B
squash C
squash D
pick E

If there are no conflicts, you are done.

However, if you want to create

ABCD-E

(in other words if you want to include the first commit of the repository in the combined commit), forget this answer and have a look at the approved answer of the question you refer to.

like image 150
Mark van Lent Avatar answered Sep 22 '22 02:09

Mark van Lent