Skip to main content
The 2024 Developer Survey results are live! See the results
added 136 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65

One Liner (for Bash/Zsh)

You can create a squash-all commit right from HEAD, without rebase at all, just run:

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Note: this requires a POSIX compatible shell like bash/zsh, or Git Bash on Windows.

Making an Alias in ~/.gitconfig

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"

Then just run: git squash-all -m "a brand new start"

Note: Either provide the commit message from standard input, or via the -m/-F options, just like the git commit command. See git-commit-tree manual.

Alternatively, you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

    Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

    The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  2. reset the current branch to the new commit

    Then git reset simply reset the current branch to the newly created commit object.

    This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.


Variation: New Repo from a Project Template

This is useful to create the "initial commit" in a new project using another repository as the template/archetype/seed/skeleton. For example:

cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

This avoids adding the template repo as a remote (origin or otherwise) and collapses the template repo's history into your initial commit.

One Liner

You can create a squash-all commit right from HEAD, without rebase at all, just run:

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Making an Alias in ~/.gitconfig

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"

Then just run: git squash-all -m "a brand new start"

Note: Either provide the commit message from standard input, or via the -m/-F options, just like the git commit command. See git-commit-tree manual.

Alternatively, you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

    Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

    The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  2. reset the current branch to the new commit

    Then git reset simply reset the current branch to the newly created commit object.

    This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.


Variation: New Repo from a Project Template

This is useful to create the "initial commit" in a new project using another repository as the template/archetype/seed/skeleton. For example:

cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

This avoids adding the template repo as a remote (origin or otherwise) and collapses the template repo's history into your initial commit.

One Liner (for Bash/Zsh)

You can create a squash-all commit right from HEAD, without rebase at all, just run:

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Note: this requires a POSIX compatible shell like bash/zsh, or Git Bash on Windows.

Making an Alias in ~/.gitconfig

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"

Then just run: git squash-all -m "a brand new start"

Note: Either provide the commit message from standard input, or via the -m/-F options, just like the git commit command. See git-commit-tree manual.

Alternatively, you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

    Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

    The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  2. reset the current branch to the new commit

    Then git reset simply reset the current branch to the newly created commit object.

    This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.


Variation: New Repo from a Project Template

This is useful to create the "initial commit" in a new project using another repository as the template/archetype/seed/skeleton. For example:

cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

This avoids adding the template repo as a remote (origin or otherwise) and collapses the template repo's history into your initial commit.

deleted 18 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65

You can create a squash-all commit right from HEAD, without rebase at allwithout rebase at all, by using the below one-linerjust run:

git reset $(git commit-tree HEAD^{tree} -m "A new start")
git reset $(git commit-tree HEAD^{tree} -m "A new start")
[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"
[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"
git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'
git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'
cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")
cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

You can create a squash-all commit right from HEAD, without rebase at all, by using the below one-liner:

git reset $(git commit-tree HEAD^{tree} -m "A new start")
[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"
git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'
cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

You can create a squash-all commit right from HEAD, without rebase at all, just run:

git reset $(git commit-tree HEAD^{tree} -m "A new start")
[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"
git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'
cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")
Update `git squash-all` to take all options that accepted by `git commit-tree` via `$@`.
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65

Update

One Liner

I've made an aliasYou can create a squash-all commit right from git squash-allHEAD.,
 Example usagewithout rebase at all, by using the below one-liner: git squash-all "a brand new start".

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Making an Alias in ~/.gitconfig

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} -m \"${1:-A new start}\"\"$@\");};f"

Then just run: git squash-all -m "a brand new start"

Note: Either provide the optional message is for commit message from standard input, if omittedor via the -m/-F options, it will default to "A new start"just like the git commit command. See git-commit-tree manual.

OrAlternatively, you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} -m "${1:-A new start}""$@");};f'

 

One Liner

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Here, the commit message "A new start" is just an example, feel free to use your own language.

TL;DR

No need to squash, use git commit-tree to create an orphan commit and go with it.

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

    Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

    The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  2. reset the current branch to the new commit

    Then git reset simply reset the current branch to the newly created commit object.

    This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.

Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

The expression HEAD^{tree} means the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  1. reset the current branch to the new commit

Then git reset simply reset the current branch to the newly created commit object.

This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.

 

Update

I've made an alias git squash-all.
 Example usage: git squash-all "a brand new start".

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} -m \"${1:-A new start}\");};f"

Note: the optional message is for commit message, if omitted, it will default to "A new start".

Or you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} -m "${1:-A new start}");};f'

 

One Liner

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Here, the commit message "A new start" is just an example, feel free to use your own language.

TL;DR

No need to squash, use git commit-tree to create an orphan commit and go with it.

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

The expression HEAD^{tree} means the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  1. reset the current branch to the new commit

Then git reset simply reset the current branch to the newly created commit object.

This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.

One Liner

You can create a squash-all commit right from HEAD, without rebase at all, by using the below one-liner:

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Making an Alias in ~/.gitconfig

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"

Then just run: git squash-all -m "a brand new start"

Note: Either provide the commit message from standard input, or via the -m/-F options, just like the git commit command. See git-commit-tree manual.

Alternatively, you can create the alias with the following command:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'

Explain

  1. create a single commit via git commit-tree

    What git commit-tree HEAD^{tree} -m "A new start" does is:

    Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.

    The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.

  2. reset the current branch to the new commit

    Then git reset simply reset the current branch to the newly created commit object.

    This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.

 
deleted 9 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
deleted 2 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
added 6 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
remove dup
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
add a section for the update about the alias `git squash-all`
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
create an alias
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
a variation is useful for starting a new project from a project template repo
Source Link
user23987
user23987
Loading
added 33 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
added 94 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
added 94 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
deleted 64 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
add information about the tree object syntax and reference
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
added 118 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
added 36 characters in body
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading
Source Link
ryenus
  • 16.8k
  • 5
  • 63
  • 65
Loading