Reputation: 555
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.
Upvotes: -1
Views: 689
Reputation: 52186
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
Upvotes: 1
Reputation: 3307
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>
Upvotes: 1