How best to crush old commits

A developer recently left who, a few months ago, left a ton of commits in repos that look like "updated" ones. Ideally, I would like them to push them into one commit, but I only did this recently.

How can I do this, for example, the following commits (assuming that from 2 months ago means that there are hundreds of them)?

.... from 2 months ago

aabbcc updated aabbdd updated aabbee updated aabbff updated 

Not wanting / needing anything, just a simple solution. These commits were not publicly shared (except for me today), so not a single problem with rollovering other people makes history.

+12
source share
3 answers

To execute git squash, follow these steps:

 // X is the number of commits you wish to squash git rebase -i HEAD~X 

Once you crush your commits - select s for squash = it will merge all the commits into one commit.

enter image description here


You also have the -root flag if you need one

try: git rebase -i --root

- root

 Rebase all commits reachable from <branch>, instead of limiting them with an <upstream>. This allows you to rebase the root commit(s) on a branch. When used with --onto, it will skip changes already contained in `<newbase>` (instead of `<upstream>`) whereas without --onto it will operate on every change. When used together with both --onto and --preserve-merges, all root commits will be rewritten to have `<newbase>` as parent instead.` 
+4
source

The age of commits does not matter, compromising commits comment.

If rebasing is not preferable for you, or there are literally thousands of commits that you want to squash and cannot bother them with, you could simply reset gently for the first hash message and resend everything:

 $ git reset aabbff $ git commit -m "This commit now contains everything from the tip until aabbff" 

You will then only have one commit, the same as rebase -> squash.

+3
source

I know that this is an ancient question, but I needed a solution for this.

In short, my local git repository (in NFS, no upstream) works as a backup of certain files, and I wanted it to have a maximum of 50 commits. Since there are many files and backups are created quite often, I need something that automatically destroys the history, so I created a script that simultaneously backs up the files and destroys the history.

 #!/bin/bash # Max number of commits preserved MAX_COMMITS=50 # First commit (HEAD~<number>) to be squashed FIRST_SQUASH=$(echo "${MAX_COMMITS}-1"|bc) # Number of commits until squash SQUASH_LIMIT=60 # Date and time for commit message DATE=$(date +'%F %R') # Number of current commits CURRENT_COMMITS=$(git log --oneline|wc -l) if [ "${CURRENT_COMMITS}" -gt "${SQUASH_LIMIT}" ]; then # Checkout a new branch 'temp' with the first commit to be squashed git checkout -b temp HEAD~${FIRST_SQUASH} # Reset (soft) to the very first commit in history git reset $(git rev-list --max-parents=0 --abbrev-commit HEAD) # Add and commit (--amend) all the files git add -A git commit --amend -m "Automatic squash on ${DATE}" # Cherry pick all the non-squashed commits from 'master' git cherry-pick master~${FIRST_SQUASH}..master # Delete the 'master' branch and rename the 'temp' to 'master' git branch -D master git branch -m master fi 

So what the script basically does (I deleted the backup):

  1. If there are more than 60 commits, it combines all commits from 50 to 60+ into one commit.
  2. Creates and validates a new branch based on a commit
  3. Cherry picks the remaining commits from the master (1 to 49) to the branch
  4. Deletes the main branch
  5. Renames the new branch to master.
0
source

All Articles