Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:54:17+00:00 2026-05-31T09:54:17+00:00

I use a combination of these 2 articles to collapse my Git history :

  • 0

I use a combination of these 2 articles to collapse my Git history : Collapsing a git repository's history and Combine the first two commits of a Git repository?

This is the result

git rebase -i THE_SHA_1_OF_FIRST_COMMIT
# change "pick" into "squash" for all commits except first one
# :wq (if your editor is vi)
git rebase -i THE_SHA_1_OF_FIRST_COMMIT
# change "pick" into "edit"
# :wq (if your editor is vi)
git reset --soft HEAD^
git commit --amend
git rebase --continue
# garbage collect
git reflog expire --expire=1.minute refs/heads/master
git fsck --unreachable
git prune
git gc

This work great but as I use this quite often, I wish to automatize this in a script.
The problem is some commands like “git rebase -i” open a file to edit and I have to do it manually (same problem for “git commit –amend”). Is there a way to automatize this process? I’m not asking for a solution ready to use, just the idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:54:18+00:00Added an answer on May 31, 2026 at 9:54 am

    I got it working using the command git config --global core.editor to send the list of commits to a script instead of the default editor. Feels a bit dirty (and maybe not optimized) but it works, and now I can collapse the history of my repositories within one command!

    You need 2 bash scripts.

    First script replacepick.sh, the script that’s used instead of the editor. Parses all lines and replace ‘pick’ by (argument 1) from line #(argument 2) in file (argument 3).

    #!/bin/bash
    
    # ./replacepick.sh replacewith fromindex filename
    
    REPLACEWITH=$1
    FROMINDEX=$2
    FILE=$3
    
    
    echo $FILE
    
    mathccount=0
    
    if [ -f $FILE ]
    then
        OUTPUT=$FILE.tmp
        echo "" > $OUTPUT
        cat $FILE | while read line
        do
            if [[ "$line" == pick* ]]
            then
                matchcount=$(($matchcount + 1))
                if [ $matchcount -gt $FROMINDEX ]
                then
                    echo ${line/pick/$REPLACEWITH} >> $OUTPUT
                else
                    echo $line >> $OUTPUT
                fi
            else
                echo $line >> $OUTPUT
            fi
        done
        newfilecontent=`cat $OUTPUT`
        echo $newfilecontent > $FILE
    fi
    

    And the bash script collapsehistory.sh calling the git commands :

    #!/bin/bash
    
    # Collapse all commits on second commit
    `git config --global core.editor "/Users/macbook/Documents/GitTests/replacepick.sh squash 1"`
    `git rebase -i $1`
    `git rebase --continue`
    
    # Collapse 2 commits on one
    `git config --global core.editor "/Users/macbook/Documents/GitTests/replacepick.sh edit 0"`
    `git rebase -i $1`
    
    `git reset --soft HEAD^`
    `git config --global core.editor "cat"`
    `git commit --amend`
    `git rebase --continue`
    
    # garbage collect
    `git reflog expire --expire=1.minute refs/heads/master`
    `git fsck --unreachable`
    `git prune`
    `git gc`
    
    # restore your favorite editor
    `git config --global core.editor "vi"`
    

    Then you execute /path/to/historycollapse.sh SHA1_OF_FIRST_COMMIT and you’re done! Hope it helps some of you too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently use a combination of LVL and Proguard as a first line of
can I use combination of OR and AND in mongodb queries? the code below
I actually use a combination of OS X, Linux and Windows, but Windows is
I have a PhoneGap/Cordova project that means I must use a combination of JQuery
Is is possible to use SignalR in combination with SqlCacheDependency (or SqlDependency) to push
I'm trying to figure out how to use NServiceBus in combination withCommon.Logging. I just
Currently I use a single equation with different combination of known/unknown parameters. As I
I'm trying to use the TouchSwipe plugin from Skinkers Labs in combination with jQuery
I am making use of a contenteditable div in combination with the rangy Javascript
I'm using a combination of PHP and Phonegap. I use PHP to build the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.