How to switch between Git Patchset

Viewed 371

I am relatively new to git and looking for the feedback related to Git command to switch between patch-sets. Here is my scenario:

I am on a branch "test" and working on a particular changeset on the same branch (created by another user). I pulled 3rd patchset from that and started working on it. Now, in the meanwhile same developer has put 4th patch set with some changes on the same files. Now, what GIT command is recommend to move to 4th patchset ( while avoiding merge conflict) and retain my work on latest patch set?

I simply tried running PULL command from Gerrit for that change set but it resulted in a merge conflict. Please advice. Thanks you in advance.

2 Answers
  1. Commit your changes and make a note of the commit SHA1.
  2. Fetch the 4th patchset and reset to FETCH_HEAD with --hard. git fetch origin refs/changes/xx/yyyxx/4 && git reset FETCH_HEAD --hard.
  3. Apply your commit. git cherry-pick <your_commit>. Solve the merge conflict if any.

In order to make it more traceable, you can create another branch for the 4th patchset and apply your commit on it. git fetch origin refs/changes/xx/yyyxx/4 && git checkout -b foo FETCH_HEAD && git cherry-pick <your_commit>.

You might want to amend the 4th patchset with your changes. If so, after git cherry-pick, you can use git reset HEAD^ --soft && git commit --amend to create a new patchset. Here, HEAD^ refers to the 4th patchset.

In this case I would do this:

  1. Commit my change
  2. Use the checkout branch command git fetch "ssh://gerrit.mycorp.net:29418/my-project" refs/changes/21/6121/4 && git checkout -b change-6121-4 FETCH_HEAD
  3. Rebase, or cherry-pick my change on the new branch and continue work from there

In general with Gerrit I would not recommend to ever use the pull command as this almost always leads to merge conflicts an that will make life harder when you want to submit your change later

Related