64

I set up a branch in the remote repository and made some commits on that branch. Now I want to merge the remote branch to the remote master.

Basically follows are my operations:

  1. checkout branch
  2. checkout master
  3. merge branch and fix merging errors
  4. commit
  5. push origin HEAD:refs/for/master

But get error messages on the 5th step:

remote: Resolving deltas:   0% (0/12)

remote: ERROR: missing Change-Id in commit message
...

remote: Change-Id: I55862204ef71f69bc88c79fe2259f7cb8365699a

To ssh://prc@test.gerrit.xxx.com:29418/hello_git
 ! [remote rejected] HEAD -> refs/for/master (missing Change-Id in commit message)
aynber
  • 22,380
  • 8
  • 50
  • 63
Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34
  • 4
    just do as git promot:`gitdir=$(git rev-parse --git-dir); scp -p -P 29418 user@host:hooks/commit-msg ${gitdir}/hooks/` then `git commit --amend --no-edit`. – Galley May 29 '19 at 06:27
  • @Galley, I think this is the simplest of the solutions for the problem posted above. I wanted to confirm with SO from what is suggested by git on the terminal. It works for me. – saichand Aug 25 '20 at 05:58

13 Answers13

101

Check if your commits have Change-Id: ... in their descriptions. Every commit should have them.

If no, use git rebase -i to reword the commit messages and add proper Change-Ids (usually this is a SHA1 of the first version of the reviewed commit).

For the future, you should install commit hook, which automatically adds the required Change-Id.

Execute scp -p -P 29418 username@your_gerrit_address:hooks/commit-msg .git/hooks/ in the repository directory or download them from http://your_gerrit_address/tools/hooks/commit-msg and copy to .git/hooks

Now git commit --amend --no-edit inserts the line.

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • I've found that the Change-Id: can be missed by Gerrit if you add e.g. a "Conflict:" line below it. So make sure it is on the last line of the commit. – qneill May 10 '12 at 14:35
  • 2
    @qneill Sounds like a bug, could you report it to the Gerrit developer? – Rafał Rawicki May 10 '12 at 15:21
  • @RafałRawicki: By design Gerrit (JGit, actually) only recognizes "Key: Value" footer lines in the last paragraph of the commit message. Gerrit can't help that the "Conflict:" lines that Git adds are appended. What might be feasible is changing the commit-msg hook to warn or bail out if there are "Conflict:" lines at the end of the message and below a "Change-Id:" line. – Magnus Bäck Jan 05 '14 at 15:46
  • 2
    I've found the changedId using `git log`,but still get the same error. – jiashie Oct 14 '14 at 02:34
  • downloading from http://your_gerrit_address/tools/hooks/commit-msg and copying to .git/hooks worked for me. Thanks Rafal for the awesome aswer. – Shridutt Kothari Jul 03 '15 at 09:57
  • Change-Id: -- should be on last line - however comment lines does NOT count as lines – serup Apr 05 '16 at 11:13
  • It boils down to adding a line `Change-Id: I` at the end of the commit message. Details can be found in the documentation: [link](https://gerrit-review.googlesource.com/Documentation/user-changeid.html) – Dominik Sep 06 '16 at 10:48
  • Do all the earlier commits done in git repo (before inception of gerrit), have to be amended? – hemantvsn Apr 12 '17 at 10:28
  • Could you go into more detailon how to do the rebase? I cannot get it to work. There are zero resources on this on the internet, gerrit doesn't have any useful docs either. – Tomáš Zato Jun 09 '17 at 08:25
  • second option works perfectly for me. Awesome! thanks – asitis Jun 25 '18 at 05:56
  • Not clear about `git rebase -i` - how to use it? Create new empty branch before doing this? If I already installed `commit-msg` hook, could I just do `git rebase` to repeat all the changes adding `Change-Id:` automatically to every commit message? – Aleksey Kontsevich Sep 13 '18 at 15:27
31

Try this:

git commit --amend

Then copy and paste the Change-Id: I55862204ef71f69bc88c79fe2259f7cb8365699a at the end of the file.

Save it and push it again!

alex
  • 479,566
  • 201
  • 878
  • 984
  • 2
    This works as a quick fix, but the 'right' answer is provided @Rafał Rawicki – JsAndDotNet Aug 02 '17 at 14:39
  • Works like a charm. Just needed to add the ChangeId: XXXXXXX under the commit comment. This is way easier than other solutions and must be voted as the answer. – Sammy Aug 02 '18 at 15:36
7

If you need to add Change-Id to multiple commits, you can download the hook from your Gerrit server and run these commands to add the Change-Ids to all commits that need them at once. The example below fixes all commits on your current branch that have not yet been pushed to the upstream branch.

tmp=$(mktemp)
hook=$(readlink -f $(git rev-parse --git-dir))/hooks/commit-msg
git filter-branch -f --msg-filter "cat > $tmp; \"$hook\" $tmp; cat $tmp" @{u}..HEAD
Matt Cowell
  • 79
  • 1
  • 4
  • Can you explain how to run this? – user963935 Oct 02 '14 at 11:17
  • copy this content to file and run it as a shell script. – tmp120210 Jun 25 '15 at 13:10
  • This doesn't work for me, it says I don't have an upstream configured. Could you explain what you're doing here? Also, `readlink -f` doesn't work on mac. – Emma Strubell Aug 03 '16 at 21:38
  • `@{u}` is a quick way to reference the upstream branch you are tracking and should be pushing to. If you are not tracking an upstream branch for some reason, you can use the full ref name in place of `@{u}` instead, such as `origin/foo` . You could alternatively set your upstream using the command provided by Git, such as `git branch -u origin/foo`. `@{u}..HEAD` is simply specifying the range of commits to rewrite, so you can use whatever format you like to specify a range. – Matt Cowell Aug 16 '16 at 13:30
  • For me above script returns `Found nothing to rewrite`. How to change it to add `Change-Id` to every commit message in my case: https://stackoverflow.com/q/52321292/630169. Thanks! – Aleksey Kontsevich Sep 13 '18 at 20:46
5

It is because Gerrit is configured to require Change-Id in the commit messages.

http://gerrit.googlecode.com/svn-history/r6114/documentation/2.1.7/error-missing-changeid.html

You have to change the messages of every commit that you are pushing to include the change id ( using git filter-branch ) and only then push.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • I run git commit --amend to fix the commit log message: Here are the current log messages: commit 8152da05ce0235cb779620410474731868664296 Author: PRC Date: Thu Jan 12 17:24:33 2012 +0800 Merge my branch to master Change-Id: I70aee922f6310e4766eb15694deb2fb3579ed042 But I still can't push the branch to the master with the same errors. – Pan Ruochen Jan 13 '12 at 07:21
2

You might be an admin doing a one-off push directly into refs/changes/<change_number>.

For example, once a commit without Change-Id landed into Subversion, you pull it out of Subversion using git-svn, and you'd like to archive it as a Gerrit patchset into a Gerrit change.

If so, you can go to project settings page (http://[installation-path]/#/admin/projects/[project-id]) and temporarily change "Require Change-Id in commit message" value to False.

Don't forget to afterwards change it back to Inherit or True!

Ivan Vučica
  • 9,529
  • 9
  • 60
  • 111
2

Check your git repo before committing

gitrepo/.git/hooks/commit-msg

if this file is not present in that location then you will get this error "missing Change-Id in commit message" .

To solve this just copy paste the commit hook in .git folder.

mahinlma
  • 1,208
  • 3
  • 11
  • 24
2

You need to follow below 2 steps instructions:

[Issue] remote: Hint: To automatically insert Change-Id, install the hook:

1) gitdir=$(git rev-parse --git-dir);

2) scp -p -P 29418 <username>@gerrit.xyz.se:hooks/commit-msg ${gitdir}/hooks/

normally $gitdir = ".git". You need to update the username and the Gerrit link.

Kashan
  • 906
  • 10
  • 15
2

1) gitdir=$(git rev-parse --git-dir);

2) scp -p -P 29418 <username>@gerrit.xyz.se:hooks/commit-msg ${gitdir}/hooks/

a) I don't know how to execute step 1 in windows so skipped it and used hardcoded path in step 2 scp -p -P 29418 <username>@gerrit.xyz.se:hooks/commit-msg .git/hooks/

b) In case you get below error, manually create "hooks" directory in .git folder

protocol error: expected control record

c) if you have submodule let's say "XX" then you need to repeat step 2 there as well and this time replace ${gitdir} with that submodules path

d) In case scp is not recognized by windows give full path of scp

"C:\Program Files\Git\usr\bin\scp.exe"

e) .git folder is present in your project repo and it's hidden folder

Ravi Yadav
  • 405
  • 4
  • 16
0

under my .git/hooks folder, some sample files were missing. like commit-msg,post-commit.sample,post-update.sample...adding these files resolved my change id missing issue.

schin chan
  • 59
  • 1
  • 5
0

By default, Gerrit will prevent pushing for review if no Change-Id is provided. Change-Id gets added to the footer of the commit message on the client side. You must download and setup the commit-msg script at the beginning. You can find more in Gerrits documentation.

git clone ssh://gerrithost:29418/RecipeBook.git RecipeBook
scp -p -P 29418 gerrithost:hooks/commit-msg RecipeBook/.git/hooks/
chmod u+x .git/hooks/commit-msg
fflores
  • 192
  • 1
  • 6
-1

I got this error message too.

and what makes me think it is useful to give an answer here is that the answer from @Rafał Rawicki is a good solution in some cases but not for all circumstances. example that i met:

1.run "git log" we can get the HEAD commit change-id

2.we also can get a 'HEAD' commit change-id on Gerrit website.

3.they are different ,which makes us can not push successfully and get the "missing change-id error"

solution:

0.'git add .'

1.save your HEAD commit change-id got from 'git log',it will be used later.

2.copy the HEAD commit change-id from Gerrit website.

3.'git reset HEAD'

4.'git commit --amend' and copy the change-id from **Gerrit website** to the commit message in the last paragraph(replace previous change-id)

5.'git push *' you can push successfully now but can not find the HEAD commit from **git log** on Gerrit website too

6.'git reset HEAD'

7.'git commit --amend' and copy the change-id from **git log**(we saved in step 1) to the commit message in the last paragraph(replace previous change-id)

8.'git push *' you can find the HEAD commit from **git log** on Gerrit website,they have the same change-id

9.done
kagb
  • 616
  • 1
  • 7
  • 19
-1

We solved this issue this morning by re-cloning repository and re-applying changes. This is the simplest way to re-sync your local copy with Gerrit. As always we created a backup first.

Although there are a number of other wildly complicated solutions, its often advantageous to take a simple approach to avoid making things worse.

Brad Hein
  • 10,997
  • 12
  • 51
  • 74
-1

This can also happen if you have this restriction:

Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit.

and you do like me: write a commit message starting with "#" .....

I had the same error, but I already had the commit-msg and did the rebase and everything. Very silly mistake though :D

mech
  • 2,775
  • 5
  • 30
  • 38
bbu23
  • 7
  • 5