Git how to automate history crash with rebase

I use a combination of these two articles to collapse my Git story: Collapse the Git repository history and Merge the first two entries of the 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 works fine, but since I use it quite often, I want to automate this in a script. The problem is some commands like "git rebase -i" open the file for editing, and I have to do it manually (the same problem for "git commit -amend"). Is there a way to automate this process? I am not asking for a ready-to-use solution, just an idea.

+4
source share
2 answers

I started by using the git config --global core.editor to send a list of commits to a script instead of the default editor. It feels a little dirty (and maybe not optimized), but it works, and now I can ruin the history of my repositories within one command!

You need 2 bash scripts.

First, the script replacepick.sh , the script that was used instead of the editor. Passes all lines and replaces "pick" with (argument 1) from line # (argument 2) in the 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 calls 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 this helps some of you.

+2
source

You should look at git merge --squash , which will merge and go through all the commits to your current branch without committing. Then you can check and commit.

+1
source

Source: https://habr.com/ru/post/1400551/


All Articles