I have a problem, when I cherry pick a commit from gerrit, make changes etc. And I want to commit them back with --amend to my commit before that cherrypick how could I do that? Right now I have just add -A all files, however I have no idea what to do next
-
Do you actually mean review/change instead of "commit from gerrit" and fetch/checkout instead of cherry-pick? – Boris Brodski Feb 21 '14 at 15:47
-
the cherry pick commit is a patchset from Gerrit? – laplasz Feb 21 '14 at 20:33
3 Answers
The Gerrit-workflow in your case is:
- Fetch a change from Gerrit using URL from the review page
- Making changes
- Add changes to the index with
git add -A .
- Amend previous commit with
git commit --amend
- Push amended commit back to the Gerrit
git push gerrit master:refs/for/master
(replacemaster
with your and remote branch names)

- 8,425
- 4
- 40
- 55
-
I have made some changes commited them, then I cherryPicked other commit, made another changes and now I want them to be pushed on the first commit right before my cherrypick, now when I am doing amend Im on that cherry picked commit unfortunately – user3274539 Feb 21 '14 at 15:54
-
you can do it with interactive rebase (`git rebase -i HEAD~3`). Then replace `pick` by `e` in front of the commit you want to amend. After you done with amending, issue `git rebase --continue` to finish the operation. – Boris Brodski Feb 22 '14 at 23:15
Amending is possible only for the latest commit. Let's assume that the commit you wand to amend is called (has a SHA-1 code) 111
. You can see the real SHA-1 code of your commit in git log
.
You can use git reset --hard
to make your local branch point to the commit you want to amend. Note that with "plain" git without Gerrit it would be a very bad idea as you are rewriting history by modifying already pushed commit!
I assume that you already have local, uncommitted changes to some files and you want to apply these changes to the commit 111
. You have to stash these changes, because git reset --hard
will remove them and it couldn't be undone.
So you have to do (replace master
with the name of a branch you're on):
git stash save
git reset --hard 111
git stash apply
<do some more changes to commit 111 if needed>
git commit --amend
git push gerrit master:refs/for/master
And voila - your commit 111 was amended and updated with Gerrit.
There is another way to do that - you can use git rebase --interactive
, but I find it more complicated than git reset
in this case.

- 7,876
- 2
- 33
- 59
I recommend using checkout instead of cherry pick a change/patchset from Gerrit. Whit checkout the commits wont depends. In your case amending the last modification will affect on the last commit, which is the cherry pick - that is normal. But if you would like to amend the change on the 1. commit, then just delete the cherry pick commmit first.