-1

I want to squash all the commits till a particular commit-id in a branch.
I tried using the command git reset --soft HEAD~223
The number 223 was generated as follows:
I ran the command git rev-list branch-name
and then found on which line the commit-id from which I am interested in squashing appears.
It was on line 223. So I tried running the command git reset --soft HEAD~223
(While on the current branch)
But I got the following error message:
fatal: ambiguous argument HEAD~223: unknown revision or path not in the working tree
Not sure what could be the reason for this.

Probably the way I have generated the number 223 is wrong.

8
  • Does your history contain merges? If so, the ~223 counts only the first-parent commits, but not the commits on the side branches. git rev-list does list all commits. Commented Sep 6, 2023 at 16:51
  • That said, since you have been able to look up the commit in the output of git rev-list, why, then, aren't you just using the commit ID instead of HEAD~223? Commented Sep 6, 2023 at 16:52
  • @j6t so just doing git reset --soft commit-id will it squash all the commits above it? Commented Sep 6, 2023 at 16:57
  • Unless the branch is a straight line, I don't think that's gonna be correct because ~ works on first parents always and so, if there are merges, they will be counted in the lines that you saw but they would be out of the line that git will follow with HEAD~223. You should try with the commit id so that you do not miss it. Commented Sep 6, 2023 at 17:03
  • git reset --soft is not a "squash commits" command. It is more like "please reseat the branch pointer to that commit, but don't change any checked-out files nor what is recorded in the index." The squash happens only when you make a commit with the state that is still in the index at this point. "squash all the commits above it"? If your history is completely linear: yes; if it is branch-and-merge-y, not necessarily. Commented Sep 6, 2023 at 17:19

2 Answers 2

1

Say for example, if you are in a local work tree and you have 15 commits and want to squash them, you can perform the following with --soft. This will set the HEAD at the 15th commit and then you can push your changes to your branch as if you were on the first commit, but all the subsequent changes would be included in the commit.

git reset --soft HEAD~15
git push <repository> <branch>

OR you can move the HEAD to a particular commit hash

git reset --soft <commit hash>
git push <repository> <branch>

OR another option is rebase. This will rewrite the commit hash.

It's highly recommended not to rebase if you push to a remote where others are working on the same branch. if you want to squash PRIOR to pushing to a remote branch, you can do the following:

<number of commits> is the physical number of commits you want to rewrite.

-i is interactive mode which allows you to modify the commits

git rebase -i HEAD~15

OR

You can also rebase and squash any commits using the first commit hash prior to your work. This says you want to rebase ON TOP of the last commit before yours. When the editor opens, you need to change the keyword to squash on all the commits you wish to squash.

git rebase -i <hash prior to your first commit>
Sign up to request clarification or add additional context in comments.

Comments

1

In some situations, generally when pointing at a commit 1 or 2 levels before your current commit, it is convenient to use HEAD~ or HEAD~2. It is however never mandatory to use this syntax, even when your intention is to squash commits together.


You can use the sha of the target commit straight ahead in your git reset command:

git reset --soft <sha>

HEAD~xx is one of the many ways to point to a specific commit. Check git help gitrevisions for all the technical details -- HEAD~xx is explained here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.