Search code examples
gitandroid-studiointellij-ideamenurebase

I'm confused by the right click menu in IntellijIdea in Git tab


I should notice that I'm new to programming. I'm confused by the right-click menu in Android Studio and Idea in the "Git" tab. I will use the following terms: <current_branch> - the branch we previously switched to with checkout. <selected_branch> - The branch we right clicked on. When we right-click on a branch, the menu contains, among other things, the following options:

  • Rebase <current_branch> onto <origin/selected_branch>
  • Merge <origin/selected_branch> into <current_branch>

  • Pull into <current_branch> using Rebase
  • Pull into <current_branch> using Merge

Right click menu in Idea in the "Git" tab I'm confused by the grouping of these options in the menu. It turns out that in the 1st option (Rebase) we make changes in one branch (selected_branch), and in the next three we make changes in another branch (current_branch). In my opinion, it would be logical:

  1. Either in the first two options do rebase/merge to one branch (origin/selected_branch), and in the next two options (pull into...) to another (current_branch).
  2. Or in all 4 options do pull/merge/rebase into the same branch (current_branch)
  3. Or group menu options differently, so it doesn't look like rebase and merge (upper two options) are going to the same branch. Perhaps I'm misunderstanding something. In this case, please correct me.

I used the mentioned commands to pull the changes from the remote branch to the local branch. I want to figure out from which branch to which branch changes are made in each of the menu options mentioned in my question.


Solution

  • Your #2 is correct: All 4 options will only affect your currently checked out branch (current_branch). The wording of "rebase onto" is a little confusing at first, because it sounds like you're adding commits to that branch and modifying it, but instead, the wording:

    rebase branch X onto branch Y means:

    Take all of the (non-merge) commits on branch X that are not reachable by branch Y, and replay them, one by one, in order, on-top of branch Y, and then reset branch X to point to the end result.

    Note in the above paragraph the word "reachable" is used because Git history is a graph. It's basically a way of saying whether or not one commit is in the history of another commit (or branch).

    As for the difference between the two groups: rebase and merge, vs pulling with rebase or merge, the only difference is that with pull you will also perform a git fetch, meaning the origin branches will be updated if they have changed on the remote server since the last time you fetched (or pulled). If you use just rebase or merge, the commands will be performed with whatever commit origin/selected_branch is pointing to at that moment. Typically your remote is on another server, so you must be online in order to pull and get the latest. Rebase and Merge without pulling can be done even when you are offline.

    Side Note: you probably don't want to rebase a shared branch, such as master, which in your screenshot is the top most (underlined) option when you have a feature branch checked out. The only time that top option might make sense is if you have a shared branch like master currently checked out, and then you right click on a feature branch with the intent to rebase it onto master.

    Disclaimer: I've never used Android Studio, however, I know what these options mean in other tools, and I think it's safe to assume they have the same meaning here.