-2

I am using Android Studio and working with multiple branches.

Two of those branches are developing different things on the same core code (kind of like a chat app that has a common core functionality, but the UI, activities, fragments are completely different).

One of those branches implements fixes that must be applied to the other branches, however, because each branch has multiple commits that are not meant to be merged, when comparing branches or merging the changes it would break the target branch since all the files that are not related to the fix would be changed.

How can I tell Android Studio to ignore all the commits before a certain commit or "sync" both branches up to a certain commit, so that when merging the changes it will only use the commits from that point forward and not everything behind?

EDIT: This is not a duplicate of git - skipping specific commits when merging, this question asks how to do it using Android Studio, not terminal commands.

Shadow
  • 4,168
  • 5
  • 41
  • 72
  • 1
    Possible duplicate of [git - skipping specific commits when merging](https://stackoverflow.com/questions/727994/git-skipping-specific-commits-when-merging) – phd Apr 20 '18 at 12:44
  • @phd It is not, see the edit at the end of the question. – Shadow Apr 20 '18 at 13:38

4 Answers4

2

From comments on other answers, it seems you want to use only IDE commands to do this. That is likely to mean there is no solution. IDE integration typically provides for the most common operations; what you want to do is not a common operation, because (as Tim suggests in his answer) the best way out of this situation would have been not to get into it.

The main difference between different solutions is how you want the final commit graph to look. Let's say you start with

O -- o -- X -- X -- B1 -- B2 -- B3 <--(branch1)
      \
       X -- X -- A <--(branch2)

The cherry-pick solution others have suggested isn't a bad one for this situation. While there may not be IDE support for it, it is relatively simple. It leaves you with

O -- o -- X -- X -- B1 -- B2 -- B3 <--(branch1)
      \
       X -- X -- A -- B1' -- B2' -- B3' <--(branch2)

Now a merge between these branches might trip on the duplicate patches, but you indicated these weren't branches you'd fully merge anyway. (That raises a different question, about whether "branches sharing a repo" is the most sensible way to store these two things, since they aren't alternate versions of the same thing. I assume you're doing it to facilitate sharing the common code, but there are tools designed specifically to address that problem, and if you used them it would be obvious how to share bugfixes to the common code. Bug I digress, right now we can get back to dealing with the situation you have today...)

Another alternative that lets you preserve the knowledge that these are "the same changes in two places" would be to rebase the bugfix away from branch1 and then merge it into both branch1 and branch2. This involves a rewrite of branch1 history, so may not be a good option for you if you've already shared branch1 (i.e. pushed the Bn commits to a shared repository already). But if you were to do it, it would look like

git checkout branch1
git branch common
git reset --hard HEAD~3
git rebase --onto `git merge-base branch1 branch2` branch1 common
git checkout branch1
git merge common
git checkout branch2
git merge common

Note that I'm assuming the bugfix commits are at the end of the branch; the procedure is a bit more difficult if not (the reset and subsequent rebase would be replaced with interactive rebases). Even if they are, the exact arguments to the reset command depend on how many commits are being moved. This would give you

       X -------- x ------- M1 <--(branch1)
      /                    /
O -- o -- B1' -- B2' -- B3' <--(common)
      \                    \
       X ----- X ---- A ---- M2 <--(branch2)

The advantage is that you now have the common branch, which is where further changes to the shared code would be made (and then merged again into branch1 and branch2).

But again, if the bugfix commits had already been pushed to the remote on branch1, then branch1 would now have to be "force pushed", and that creates an "upstream rebase" condition for anyone else sharing the remote; so refer to the git rebase docs for info about that situation if you're unsure whether it's acceptable for your case.

There are other options, but those cover the most sensible options for how you might want the commit graph to end up.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
0

I think the best answer would be to come up with a workflow such that this problem doesn't/can't happen. Assuming it does happen, one option would be to make the bug fix in a given branch, and then cherry-pick the fix commit to the other feature branches. Assuming the feature branches will never merge with each as you describe, the only issue then might be when the feature branches get merged back to master. Then, the same bug fix would come in from multiple sources, and this may or may not cause confusion.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • The workflow is established and cannot be changed, otherwise I wouldn't have asked this question if I could have avoided it in the first place.. – Shadow Apr 20 '18 at 12:36
  • I did, but your question makes me think I might have misunderstood what you were saying. Did I? – Shadow Apr 20 '18 at 12:55
  • 1
    What I'm saying is that a quick and dirty way to port the bug fixes across all branches is to make a fix commit on one feature branch and then cherry pick that commit to the other feature branches. – Tim Biegeleisen Apr 20 '18 at 13:09
  • Oh, totally not what I understood, thanks for clearing that up. – Shadow Apr 20 '18 at 13:39
0

You can use git cherry pick to apply changes from another branch

Please refer below link to use git cherry pick:

https://git-scm.com/docs/git-cherry-pick

If you want to get commits then use git log

  • How can this be done with Android Studio without resorting to terminal commands? – Shadow Apr 20 '18 at 11:49
  • Can you please elaborate more? – Swati Dongre Apr 20 '18 at 12:02
  • Android Studio has its own tools for managing branches, compare, pull, merge, etc. and thanks to that we don't have to use terminals to interact with Git, there's no commands needed to use Git and all its actions in Android Studio. – Shadow Apr 20 '18 at 12:06
  • I have checked with Android studio version controls. There is not git cherry pick command in android studio. You can compare and merge branches and also you can see the commit files. I will suggest that you can use android studio terminal for that. – Swati Dongre Apr 20 '18 at 12:11
  • I found out that it actually does have a cherry pick option in the version control, but won't cherry pick apply all the changes up to that point? – Shadow Apr 20 '18 at 12:31
  • You can add multiple commit messages to apply changes to that point – Swati Dongre Apr 20 '18 at 12:50
  • My worry is if I cherry pick, as you suggested, won't it also pick all the changes from the other branch up until the point of the cherry pick? Or will it just pick the changes that are only affected in the commit I choose? – Shadow Apr 20 '18 at 12:56
  • 1
    @Shadow A commit in Git can be thought of as a diff. In your case, the commit/diff of interest is the one which you would make to fix the bugs. You may then cherry pick, or apply, this diff against other branches. This is not the same as a merge, where you meld two branches together. – Tim Biegeleisen Apr 20 '18 at 13:19
  • @TimBiegeleisen Thanks Tim, if that is the case then Swati's answer is the right one, in which case I'd like to ask her to update it to include the steps to do this in the Android Studio (right click a commit in the history log and select "cherry pick") since that was the goal of the question. – Shadow Apr 20 '18 at 13:40
  • @Shadow Android Studio is basically IntelliJ, and I don't know how to cherry-pick from within the IDE. What is your aversion to using the command line? – Tim Biegeleisen Apr 20 '18 at 14:02
  • I am not the only one working, almost no one uses git commands and this solution will be used by them as well. – Shadow Apr 20 '18 at 14:25
  • @Shadow If you are going to use terminal then you just need to use Git cherry pick a...c – Swati Dongre Apr 20 '18 at 14:34
  • @SwatiDongre I am not going to use the terminal, I have already explained that. I even asked that again in the first question to your answer. – Shadow Apr 20 '18 at 14:36
  • Then I don't have any idea how to apply it from Android studio. I will let you know if I found anything – Swati Dongre Apr 20 '18 at 14:37
  • @Shadow Please let me know if you get any solution on this – Swati Dongre Apr 24 '18 at 12:29
  • @Shadow I found one option in android studio for git cherry-pick. Please follow the path: Android studio-->Settings-->Version control--> Git--> Choose option "Commit automatically on cherry-pick". Please let me know if it works. – Swati Dongre May 31 '18 at 07:17
  • @SwatiDongre No such option exists on AS 3.1.2 – Shadow May 31 '18 at 09:08
-1

enter image description here

Please refer this screenshot. Maybe you will get your answer

  • How does this answer my question of "**compare branches while ignoring any changes before a specific commit**"? – Shadow May 31 '18 at 10:29