1

I have 4 commits that I have committed to github. I would like to squash them into 1 commit.

I have tried the following command: git rebase -i HEAD~4

This however gives me a large list of commits in addition to my 4. In other words, the list includes commits that are not mine. The list looks something like:

pick some other commit
pick my commit 1
pick some other commit
pick some other commit
pick my commit 2
pick my commit 3
pick some other commit
pick my commit 4

How do I squash only my 4 commits?

Melissa Guo
  • 948
  • 9
  • 32
  • https://stackoverflow.com/questions/2740537/reordering-of-commits covers what you need; interactive rebase to reorder (and then squash) commits. – Mikael Öhman Jul 06 '23 at 23:48
  • For clarity, are you doing this in a branch? if so, you may get less surprising results if you do `git rebase -i main` (or whatever you're planning on merging into) and then you're just adjusting for the differences between your branch and the target. In such a case you should see only the changes in your branch. – JasonTrue Jul 07 '23 at 00:58
  • In the remote repository, there is origin > master. In my local I have master. I tried git rebase -i main and then git rebase -i HEAD~4, but I still see the the other commits. – Melissa Guo Jul 07 '23 at 20:22

3 Answers3

0

There are two basic ways.

The first is: reorder your lines so that they are all adjacent, and replace 'pick' with 'squash' for the bottom three of your four commits. Then save and exit the file. This may be easiest, if none of the other commits conflict with your work.

The second is: create a new branch from master, cherry-pick all of your commits onto this new branch, and then redo the rebase. All of your commits should be adjacent now, making them easier to squash (replace the word 'pick' with 'squash' for the bottom three of your four commits).

JohnFF
  • 723
  • 5
  • 20
0

Change pick to squash for later commits should work

pick my commit 1
squash my commit 2
squash my commit 3
squash my commit 4
huan feng
  • 7,307
  • 2
  • 32
  • 56
0

I had made my changes on the master branch. To resolve the issue, I restored the master branch from the remote repository using the below commands

1. git checkout master

2. git pull -r

3. git fetch presto

4. git reset --hard presto/master

5. git push origin master --force

I then checked out a new branch and re-did my changes.

git checkout -b [branch name]

To push subsequent changes I used amend

1. git add .
2. git commit --amend -m "[commit description]" 
3. git push -f origin [branch name]
Melissa Guo
  • 948
  • 9
  • 32