Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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 (replace master with your and remote branch names)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top