{"id":16347702,"url":"https://github.com/privatenumber/git-squash-branch","last_synced_at":"2025-03-21T00:30:20.768Z","repository":{"id":65599464,"uuid":"595449233","full_name":"privatenumber/git-squash-branch","owner":"privatenumber","description":"Script to squash the commits in the current Git branch","archived":false,"fork":false,"pushed_at":"2023-02-13T13:49:56.000Z","size":103,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-17T18:53:54.736Z","etag":null,"topics":["branch","git","script","squash"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/privatenumber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"privatenumber"}},"created_at":"2023-01-31T05:04:24.000Z","updated_at":"2024-11-18T10:30:19.000Z","dependencies_parsed_at":"2023-03-10T18:52:44.805Z","dependency_job_id":null,"html_url":"https://github.com/privatenumber/git-squash-branch","commit_stats":{"total_commits":27,"total_committers":2,"mean_commits":13.5,"dds":"0.14814814814814814","last_synced_commit":"b35f5454b5bcd09adc865b46952e05465c279fdc"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Fgit-squash-branch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Fgit-squash-branch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Fgit-squash-branch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Fgit-squash-branch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/privatenumber","download_url":"https://codeload.github.com/privatenumber/git-squash-branch/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244717073,"owners_count":20498279,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["branch","git","script","squash"],"created_at":"2024-10-11T00:45:00.014Z","updated_at":"2025-03-21T00:30:20.763Z","avatar_url":"https://github.com/privatenumber.png","language":"TypeScript","funding_links":["https://github.com/sponsors/privatenumber"],"categories":["TypeScript"],"sub_categories":[],"readme":"# git-squash-branch\n\nScript to squash commits in a Git branch or pull-request.\n\n\u003csub\u003eSupport this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️\u003c/sub\u003e\n\n## Why?\nTo consolidate the commits in a branch to a single commit for a cleaner Git history.\n\nExamples:\n1. Squashing WIP commits in the default branch of a new repository. I personally do this often when starting a new project.\n\n2. Squashing your PR's commits when [squash merging](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/configuring-commit-squashing-for-pull-requests) is disabled on the repository you're contributing to.\n\n3. Squashing your PR's commits when your PR is deployed through another PR (e.g. in a batched PRs, GitHub cannot squash individual PRs in the batch)\n\n## Usage\n\n### Squash current branch\nRun the script with [npx](https://nodejs.dev/learn/the-npx-nodejs-package-runner) from the repository branch you want to squash:\n```\nnpx git-squash-branch\n```\n\n#### Example: squashing new commits in current branch compared to base branch\n```sh\n# You must be inside the branch you want to squash\n$ git checkout branch-to-squash\n\n$ npx git-squash-branch --base develop --message \"feat: my new feature\"\n\nSuccessfully squashed with message:\nfeat: my new feature\n\nTo revert back to the original commit:\ngit reset --hard 4f0432ffd1\n\n# Force push the squashed branch to remote\n$ git push --force\n```\n\n#### Example: squashing all commits in current branch\n```sh\n$ git checkout main\n\n$ npx git-squash-branch --base main --message \"feat: init\"\n\nCurrent branch is the same as base branch. Squashing all commits to root.\nSuccessfully squashed with message:\nfeat: init\n\nTo revert back to the original commit:\ngit reset --hard 4f0432ffd1\n\n# Force push the squashed branch to remote\n$ git push --force\n```\n\n### Squash PR\n\u003e ⚠️ Requires [GitHub CLI](https://cli.github.com/) to be installed\n\nFrom inside the repository directory, pass in the PR number, URL, or branch:\n```\nnpx git-squash-branch pr [\u003cnumber\u003e | \u003curl\u003e | \u003cbranch\u003e]\n```\n\nIt will squash the PR branch into a single commit and force push it back to the PR branch.\n\n#### Example\n```sh\n# You must be inside the repository for gh to fetch the PR\n$ cd my-repo\n\n$ npx git-squash-branch pr 1234\n\n✔ Successfully squashed PR 1234 with message:\nfeat: my PR title\n\nTo revert the PR back to the original commit:\ngit push -f origin 4f0432ffd1:pr-branch-name\n```\n\n\u003e Note: This command will not update the PR with the latest base branch.\n\n### Manual\n```\nUsage:\n  git-squash-branch [flags...]\n  git-squash-branch \u003ccommand\u003e\n\nCommands:\n  pr\n\nFlags:\n  -b, --base \u003cstring\u003e\n  Base branch to compare against. If not specified, will try to\n  detect it from remote \"origin\".\n\n  -h, --help\n  Show help\n\n  -m, --message \u003cstring\u003e\n  Message for the squash commit (defaults to last commit message)\n\n  -r, --remote \u003cstring\u003e\n  Remote to fetch from (default: \"origin\")\n\n  --version\n  Show version\n```\n\n## What does this script do?\n\nBasically runs these commands, derived from this [StackOverflow answer](https://stackoverflow.com/a/25357146):\n```sh\n$ git reset --soft $(git merge-base \u003cbase-branch\u003e $(git branch --show-current))\n$ git commit -m \u003cmessage\u003e\n```\n\nOn top of that, it adds extra features such as:\n- Tries to automatically detect the base branch based on the `origin` remote\n- Defaults to using the commit message from the last commit, if not provided\n- Logs the current commit hash in case you want to revert the squash\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Fgit-squash-branch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprivatenumber%2Fgit-squash-branch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Fgit-squash-branch/lists"}