7

I sent a commit (named "A commit") to review (Gerrit) using git review command.

Now, I make a new commit (named "B commit") and I want to send it to review as well, but I don't want to re-send the "A commit". There is no dependencies each other.

How to send a review to gerrit for a specific commit?.

UPDATE:

$ git add --all


$ git status

# On branch delete_role
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   path/to/file.ext


$ git status

# On branch delete_role
nothing to commit (working directory clean)


$ git branch

*delete_role
 master


$ git log --graph --decorate --oneline -n13

* 531bd84 (HEAD, delete_role) commit 3
* df68f1a (master) commit 2
* 4ce2d8d commit 1
*   6751c7d (origin/master, origin/HEAD, gerrit/master)

Commit "df68f1a" and "4ce2d8d" are dependant and they have been sent in a previous git review command, but commit "531bd84" belongs to a new branch (delete_role) because is a new issue.

$ git review

You have more than one commit that you are about to submit.
The outstanding commits are:

531bd84 (HEAD, delete_role) commit 3
df68f1a (master) commit 2
4ce2d8d commit 1

I want to send to Gerrit only the "531bd84" commit, not the other ones.

Lobo
  • 4,001
  • 8
  • 37
  • 67

4 Answers4

9

Create the B commit in a new branch.

While being on this branch, use git review and it will only push the content of this branch to Gerrit.

This way, Gerrit won't consider that your commit B needs your commit A and if you want, you can merge your commit B to the working branch before commit A

If your history is like this:

...-old(merged)-A(waiting for review)

what you want to do is:

...-old(merged)-A(waiting for review) <-master branch
       \B(new commit)                 <-new branch

Then, if you're on branch B, and use git review, it won't push anything else than commit B

If you're in this situation:

...-old(merged)-A(waiting for review)-B

, what you want to do to achieve the configuration we want is:

git log (Note the SHA1 of your B commit)
git reset HEAD^^^ (you go back in detched state three commits before, before the two you don't want to send)
git checkout -b Breview (you create a new branch there)
git cherry-pick +the SHA1 you noted (you copy your B commit on your new branch)
git checkout master (you return on your branch with the two commit)
git reset HEAD^--hard (you delete the B commit from this branch where you don't need it)

Now, you achieved the wanted configuration and to push your B commit, you just need to do:

git checkout Breview
git review

and it will only submit your B commit

Rudy Bunel
  • 784
  • 1
  • 7
  • 15
  • 1
    Yes, create commit B on a branch that is based on the remote branch (thus without commit A). It's better to always create a branch for every commit you want to push to review (except some commits depend on each other). – StephenKing Nov 04 '13 at 17:29
  • Ok, I made a new branch and I moved to it. I made a new commit on this new branch but, when I execute "git review", GIT says that "You have more than one commit that you are about to submit.", I mean, GIT shows commits from "master" branch and "the new" branch, not only from "the new" branch. I expected that there is only 1 commit, from "the new" branch. – Lobo Nov 07 '13 at 14:02
  • @Lobo Is your new branch dependent of the master branch? – Rudy Bunel Nov 07 '13 at 17:42
  • @Lobo I updated my answer. Was your new branch based on A commit or on commit that were already merged? – Rudy Bunel Nov 07 '13 at 17:50
  • @RudyBunel My new branch is based on commit A. – Lobo Nov 08 '13 at 08:29
  • @Lobo Your new branch needs to be based on the commit that was last merged in Gerrit. Else, Git will also submit commit A. – Rudy Bunel Nov 08 '13 at 08:50
  • @RudyBunel Ok, I'll try "git rebase" command but I don't want to send to Gerrit commit A again. If I execute "git rebase", Should I re-send commit A to Gerrit? – Lobo Nov 08 '13 at 08:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40805/discussion-between-rudy-bunel-and-lobo) – Rudy Bunel Nov 08 '13 at 08:59
4

Branches are the answer:

If you have not made commit A then before making commit A make branch A, then commit your code to that branch and submit it for review Then, go back to the master and create branch B, commit to branch B and submit for review. This way you ensure that there are no dependencies.

Ok, now lets say that you have committed everything in the master like this:

M----A----B

This is your log:

commit b58fff12319d7ad1b1bc6ebcf4395d986a8ffac3
Author: 
Date:   Fri Nov 8 09:48:23 2013 +0800

    Change B

commit 99f249cdf2352ace3ca9ee8bf7a8a732f27b31eb
Author: 
Date:   Fri Nov 8 09:47:56 2013 +0800

    Change A

commit f091b7a4cc5c0532973c5bd401d2171f6fb735ec
Author: 
Date:   Fri Nov 8 09:47:30 2013 +0800

    Initial commit

What we want is this:

    A
  / 
 /
M 
 \
  \
   B

To get this create perform the following commands:

# Create two new branches from the point where the current Gerrit repo master is now
git branch A f091b7a4cc5c0532973c5bd401d2171f6fb735ec
git branch B f091b7a4cc5c0532973c5bd401d2171f6fb735ec
git checkout A # Create a branch to hold change A
git merge 99f249cdf2352ace3ca9ee8bf7a8a732f27b31eb
git log
commit 99f249cdf2352ace3ca9ee8bf7a8a732f27b31eb
Author: 
Date:   Fri Nov 8 09:47:56 2013 +0800

    Change A

commit f091b7a4cc5c0532973c5bd401d2171f6fb735ec
Author: 
Date:   Fri Nov 8 09:47:30 2013 +0800

    Initial commit
git checkout B 
git cherry-pick b58fff12319d7ad1b1bc6ebcf4395d986a8ffac3
git log
# Note the new sha1 hash as this is change could not be fast forwarded
commit d3aa1acc2b208115c7de78d5c9c4f6a906ece84a
Author: 
Date:   Fri Nov 8 09:48:23 2013 +0800

    Change B

commit f091b7a4cc5c0532973c5bd401d2171f6fb735ec
Author: 
Date:   Fri Nov 8 09:47:30 2013 +0800

    Initial commit

Now you have two branches that hold one change each, pushing either of these changes to Gerrit will result in just one change being pushed.

Then you might want to clean up you master by remove the commits from there:

git checkout master
git reset --hard HEAD~2

Specially for the update question:

git branch B 6751c7d 
git checkout B
git cherry-pick 531bd84 

You should create the branch from the gerrit master 675c7d, then cherry-pick your commit 3 into the new branch, then you can delete your old branch delete_role

uncletall
  • 6,609
  • 1
  • 27
  • 52
  • I have 2 commit in "master" branch. I made a new branch and I changed to this new branch. I made some changes and I made a new commit. Now, when I launch "git review" GIT says that "You have more than one commit that you are about to submit." – Lobo Nov 07 '13 at 13:42
  • I expected that there is only 1 commit to send to review. – Lobo Nov 07 '13 at 13:51
  • 1
    Did you make the branch from the master as it is now on Gerrit? You should start the branch from the point where the Gerrit master is at now. – uncletall Nov 08 '13 at 02:12
  • I updated my question. I make the new branch starting for a previous commit, not from Gerrit master. What should I do now? I don't want to lose my previous commits sent to Gerrit. – Lobo Nov 08 '13 at 08:28
  • 1
    git branch B 6751c7d git checkout B git cherry-pick 531bd84 You should create the branch from the gerrit master 675c7d, then cherry-pick your commit 3 into the new branch, then you can delete your old branch delete_role – uncletall Nov 08 '13 at 09:23
  • Thank you, I just resolve my issue in the same way that you describe on your last comment. – Lobo Nov 08 '13 at 09:25
  • Sorry, but I chat with Rudy Bunel and he helps me to resolve my question. Rudy Bunel was more "quick" with his response. – Lobo Nov 08 '13 at 09:33
  • Different time zone ;) – uncletall Nov 11 '13 at 00:42
  • Yes, I had the same situation than you several times. The important thing is you answer can help to another people. – Lobo Nov 11 '13 at 09:33
1

The way I see it you have branch where you work, then you have commit A and next commit B is also in the same branch. So logically they don't depend on each other but for history those changes are dependent. So first commit A will need review and be merged to working branch before second B could be merged in.

Now when you push references to review, Gerrit already knows by change id's what you have pushed for review. If you push branch to review only new commits will be sent, or commits that were updated or rebased.

Long story short. Gerrit knows what is new and what is already in review.

Mateusz
  • 2,287
  • 20
  • 28
1

After pushing commit 'A' you can reset the local branch and start working on commit 'B'. Since every commit/change can be checked out from Gerrit later once it was pushed to review branch. you can get the command from Gerrit review board at each patchset download panel. an example:

git fetch https://android.googlesource.com/kernel/tegra refs/changes/43/69243/2 && git checkout FETCH_HEAD

After checking out a change it can be amend and pushed again as a new patchset. So you dont have to use local branches for each commit.

laplasz
  • 3,359
  • 1
  • 29
  • 40