Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to automatically merge notes if commits for those notes get squashed?

Tags:

git

git-notes

For example:

git commit -am "Something"
git notes append -m "Dark "
git commit -am "Something"
git notes append -m "Side"

git rebase -i
# now I squash two commits and I expect to see "Dark Side" here
# but it show that note is undefined
git notes show 
like image 858
Andrew Avatar asked Jan 29 '13 14:01

Andrew


People also ask

How do you squash commits when merging?

To enable commit squashing as the default option in your repository: Navigate to your chosen repository and open the Settings sub-tab. Open the General Settings page. Check the box for Squash commits on merge default enabled.

How do you squash before merge?

You can choose to squash merge when completing a pull request in Azure Repos. Choose Squash commit under Merge type in the Complete pull request dialog to squash merge the topic branch.

Should I squash and merge or just merge?

Merge vs.You should consider using squash if your team prefers a linear project history. This means that the history held by your main branch should not contain merges. A squash merge makes it possible to keep changes condensed to a single commit, supporting this strategy nicely.

Why you should rebase instead of merge?

But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge .


1 Answers

The problem is almost certainly your config; assuming you have otherwise default config, you need to set the notes.rewriteRef option to refs/notes/commits for this to work.

The magic command you need is thus:

git config notes.rewriteRef refs/notes/commits

After the above, squashing the commits should join the two notes.

They will have newlines between them, however; I suspect disabling that behaviour so you get things on the same line as in your example would require hacking around in the Git source code.

Background

From git help config (emphasis mine):

notes.rewriteRef

When copying notes during a rewrite, specifies the (fully qualified) ref whose notes should be copied. The ref may be a glob, in which case notes in all matching refs will be copied. You may also specify this configuration several times.

Does not have a default value; you must configure this variable to enable note rewriting. Set it to refs/notes/commits to enable rewriting for the default commit notes.

This setting can be overridden with the GIT_NOTES_REWRITE_REF environment variable, which must be a colon separated list of refs or globs.

(See also the descriptions for notes.rewriteMode and notes.rewrite.<command>, both of which default to the values we need, ie concatenate and true respectively.)

Example

Here's something similar for the above test:

$ git init
Initialized empty Git repository

$ git config notes.rewriteRef refs/notes/commits

$ git add a # Here's a file I created earlier

$ git commit -am 'Initial commit'
[master (root-commit) 93219cb] Initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 a

$ echo a >>a

$ git commit -am 'Something'
[master 3c17aca] Something
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git notes append -m 'Dark '

$ echo b >>a

$ git commit -am 'Something'
[master 6732d81] Something
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git notes append -m 'Side'

$ git rebase -i HEAD~2 # Will squash the last commit into the one before and accept the default commit message.
[detached HEAD 552668b] Something
 1 files changed, 2 insertions(+), 0 deletions(-)
Successfully rebased and updated refs/heads/master.

$ git show
commit 552668b4b96e4b2f8fcd7763dcc115edd159eb89 (HEAD, master)
Author: me_and <[email protected]>
Date:   Wed Jan 30 10:09:10 2013 +0000

    Something

    Something

Notes:
    Dark

    Side

diff --git a/a b/a
index 7898192..4ac2bee 100644
--- a/a
+++ b/a
@@ -1 +1,3 @@
 a
+a
+b
like image 133
me_and Avatar answered Sep 19 '22 02:09

me_and