Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase squash commits by hash

Tags:

git

rebase

squash

I have a branch with around 10 commits. 3 of these are merge commits merging master to my branch and adding ~500 commits in between each time. Is there a way to squash commits that were made on the current branch in one? my history looks something like this:

ad54ef86 My Commit 1
ad54ef86 Merge commit
ad54ef86 Others' Commits
....500 more others' commits
ad54ef86 My Commit 2
ad54ef86 My Commit 3
ad54ef86 Merge commit
ad54ef86 Others' Commits
....500 more others' commits
ad54ef86 My Commit 4
...

Now doing git rebase -i HEAD~2000 and searching is a lot of work. It there a way around it?

EDIT: So this question is being tagged as a duplicate of Squashing last n commits together. That is clearly not my problem. I want to squash the last n commits made ONLY on my current branch AFTER I merge another branch in this branch and get all those commits in the way as well.

like image 408
ayushgp Avatar asked Mar 06 '23 00:03

ayushgp


1 Answers

First, I would ask what is the purpose of your rebase? Do you want to eventually push your commits to the remote? Or, do you want to simply have your commits on top and just continue to work locally? Either way, I suggest you take the following steps:

  1. create a new branch starting from your remote master branch. This will get you all the commits from the remote master.
  2. Switch to the new branch.
  3. Start cherry picking your commits from the old branch you described. It would help to know the hashes of your commits. This will get all the commits from master under your commits. Please note that the hashes of your commits in the new branch will be re-written.
  4. If needed, you can now squash and push your commits to the remote.

Remember to do fetch and rebase next time you need to synchronize to the remote.

like image 195
rdf Avatar answered Mar 15 '23 02:03

rdf