Pushing Squashed change subtree to gerrit

When adding a subtree:

git subtree add --prefix = vendor https://example.com/repo master --squash

Two commits created. One for compressed subtree commits

Squashed 'vendor' changes from 15fc2b6..c6dc29b 

and merge fixing

 Merge commit 'SHA1' into master 

When I want to push this change to gerrit, it needs changeID. But git doesn't let me do

git rebase -i HEAD~2

and redo, as I do for any other commit, as this is a compressed commit.

Now I cannot push this change to gerrit because of this. I cannot make changes in the heads (git) directly and break the material on the super module. It must pass assembly and testing. Any suggestion or help is appreciated.

+5
source share
1 answer

git filter-branch can do many things and at the same time update commit pointers so that the commit graph remains intact. One of them executes a command to edit all commit messages, such as the Gerrit commit-msg hook.

git filter-branch HEAD...HEAD~1 captures all commits in HEAD, but not HEAD ~ 1, which for merging, git -subtree is just a merge and squash.

git filter-branch --msg-filter accepts a run command, which receives a commit message sent to stdin and writes a new one to stdout. commit-msg scripts work on the file, so to write the original message to the file, you need to run the shell script, run the hook, and then send the file to stdout.

I put all this in a script to run before clicking on Gerrit:

 # This command re-writes the history after doing a git subtree {pull|add} # to add Gerrit Change-Id lines to the squash commit message. # # It assumes that HEAD is the merge commit merging the subtree into HEAD. # The original HEAD commit will be backed up under refs/original, which # is helpful if something goes wrong. set -e set -o pipefail GIT_DIR=$(readlink -f $(git rev-parse --git-dir)) TMP_MSG="${GIT_DIR}/COMMIT_MSG_REWRITE" git filter-branch --msg-filter \ "cat > ${TMP_MSG} && \"${GIT_DIR}/hooks/commit-msg\" ${TMP_MSG} && \ cat \"${TMP_MSG}\"" HEAD...HEAD~1 rm -rf ${TMP_MSG} 
0
source

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


All Articles