0

I'm very new to Git, so any excessively detailed explanation is appreciated : )

Say, I have a game application that is released in two countries. Actually I have two code repositories for this application, which have the same architecture but differs more or less on implementation level. Now we sometimes would want the two repositories to exchange code, such as moving the code of a feature to another repository, and I have two questions:

  1. What is the best practice for grouping all the commits of a feature (we manage tasks on Atlassian Jira and let's say we can always label all the tasks of a feature there correctly)? I'm thinking of labeling commits and filtering them by Jira issue id, but how exactly?
  2. Once I get the list of all the commits I need, how do I apply them to the other repository, if there are many of them and they are discrete commits.

Thanks!

Chengteng Li
  • 11
  • 1
  • 4

1 Answers1

0

The best practice to group commits of a feature is simply to use a feature branch: all the commits of a feature are simply in the same branch. When you merge it without fast-forwarding, you can identify them in the commit history and also get the final delta in the merge commit.
This way, you get an history like this:

3     next commit
2     merge of feature #1
|\
| C   last commit of feature #1
| B   2nd commit of feature #1
| A   1st commit of feature #1
|/
1     commit before starting feature branch

You can identify the commit of feature #1 with:

# Get all the commits in second parent of merge that are not in first parent of merge
git log 2^2...2^1
# To get only the commit hash (to use as parameter for cherry-pick):
git rev-list 2^2...2^1

An other way is to mark them with a unique identifier in the commit message (e.g. a ticket reference), then you can list them by using the filters of git log:

git log --grep 'feature #1'
# To get only commit hash:
git log --format='%H' --grep 'feature #1'

To apply commits from one repository to another, you have, at least, those 2 solutions:

  1. Add the first repository as a remote in the second one, then you can simply cherry-pick the commits (you may have to fix potential conflicts):

    # In second repository
    git remote add other_country ULR_TO_FIRST_REPO
    git fetch other_country
    # Go on the right branch
    git cherry-pick COMMIT1 COMMIT2 COMMIT3...
    # If you have conflict: solve them (like for a merge) and then `git cherry-pick --continue`
    
  2. In the first repository, export the commits with git format-patch (multiple times if they are not in the same range) and apply them in the second repository with git am

zigarn
  • 10,892
  • 2
  • 31
  • 45
  • Thanks for your quick reply! – Chengteng Li Aug 18 '17 at 02:12
  • Thanks for your quick reply! I understand the part about using non fast-forward, but how exactly do I identify them in the commit history? I know I am allowed to add a comment with every commit (git commit -m "Feature-ID-2231"). Is this what you referring to? I'm sorry I don't understand the difference between the two ways. Okay, thanks! I'll look at some git GUI and see if there's a quick way to list all the discrete commits for cherry picking : ) – Chengteng Li Aug 18 '17 at 02:56
  • After some research, I've understood your first solution. And in my case, the second solution might be more feasible for me, because when I was talking about a feature, I was imagining many discrete commits of it along the main branch. So I must be particular about the branch naming convention : ) Also, this seems to be easier under a GUI context. Thanks! – Chengteng Li Aug 21 '17 at 04:05