Git: how to crush all commits on a branch

I am making a new branch from master with:

git checkout -b testbranch

I make 20 commits to it.

Now I want to crush these 20 commits. I am doing this with:

git rebase -i HEAD~20

And if I do not know how many commits? Is there a way to do something like:

git rebase -i all on this branch

+200
git version-control
Aug 18 '14 at 5:25
source share
7 answers

Another way to destroy all your commits is to reset the index to master:

  git checkout yourBranch git reset $(git merge-base master yourBranch) git add -A git commit -m "one commit on yourBranch" 

This is not ideal, as it implies that you know which branch your branch is coming from.
Note: finding this source branch is not easy / impossible with Git (the visual method is often the easiest , as shown here ).




EDIT: you will need to use git push --force




Carlocha Hoa adds in the comments :

To reset you can do

 git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD)) 

[This] automatically uses the branch you are in.
And if you use this, you can also use an alias, as the command does not rely on the branch name .

+253
Aug 18 '14 at 5:59
source share

What you do is quite error prone. Just do:

 git rebase -i master 

which automatically reinstalls only your branch, takes on the current last master.

+87
Aug 18 '14 at 5:30
source share

Another easy way to do this: go to the branch of origin and run merge --squash . This command does not perform a squeeze commit. when you do, all messages about fixing your marriage will be collected.

 $ git checkout master $ git merge --squash yourBranch $ git commit # all commit messages of yourBranch in one, really useful > [status 5007e77] Squashed commit of the following: ... 
+62
Sep 21 '16 at 12:59 on
source share

Issue the branch for which you want to combine all the commits into one commit. Let's say it's called feature_branch .

git checkout feature_branch

Step 1:

Make a soft reset of your origin/feature_branch with your local master branch (depending on your needs, you can also perform a reset using origin / master). This will reset all additional commits in your feature_branch , but without changing any changes to your file locally.

git reset --soft master

Step 2:

Add all the changes in your git repo directory to the new commit that will be created. And do the same with the message.

git add -A && git commit -m "commit message goes here"

+46
Jun 15 '18 at 17:12
source share

Assuming you forked from the master, you don't need to constantly enter yourBranch in the reset step:

 git checkout yourBranch git reset --soft HEAD~$(git rev-list --count HEAD ^master) git add -A git commit -m "one commit on yourBranch" 

Explanation :

  • git rev-list --count HEAD ^master counts commits since you made your function branch from master, for example. twenty.
  • git reset --soft HEAD~20 will do a soft reset of the last 20 commits. This leaves your changes to the files, but removes commits.

Usage :

In my .bash_profile, I added an alias for gisquash to do this with one command:

 # squash all commits into one alias gisquash='git reset --soft HEAD~$(git rev-list --count HEAD ^master)' 

After reset and commit, you need to do git push --force .

Hint :

If you use Gitlab> = 11.0, you no longer need to do this, since it has a compression option when merging branches. Gitlab squashing option

+19
Dec 15 '17 at 5:43 on
source share

Based on reading a few Stackoverflow questions and answers on the distribution, I think this is a good one liner to squash all commits on a branch:

 git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master 

This assumes master is the base branch.

+13
Sep 07 '17 at 16:00
source share

The solution for people who prefer to click:

  1. Install sourcetree (it's free)

  2. Check how your commits look. Most likely you have something similar to this. enter image description here

  3. Right-click on the parent commit. In our case, this is the main branch.

enter image description here

  1. You can compress the commit with the previous one by pressing the button. In our case, we have to click 2 times. You can also change the commit message. enter image description here

  2. The results are amazing and we are ready for it! enter image description here

Note: if you pushed your partial commits to remote control, you should use a push after squash

0
Jun 28 '19 at 7:03
source share



All Articles