英文:
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> - 当前分支,是我们之前使用checkout切换到的分支。
<selected_branch> - 我们右键点击的分支。
当我们右键点击一个分支时,菜单中包含以下选项之一,还有其他选项:
- Rebase <current_branch> 到 <origin/selected_branch>
- Merge <origin/selected_branch> 到 <current_branch>
- 使用Rebase将内容拉取到 <current_branch>
- 使用Merge将内容拉取到 <current_branch>
Idea中Git选项卡的右键菜单
我对这些选项在菜单中的分组感到困惑。事实证明,在第一个选项(Rebase)中,我们在一个分支(selected_branch)中进行更改,而在接下来的三个选项中,我们在另一个分支(current_branch)中进行更改。在我看来,逻辑上应该是:
- 要么在前两个选项中对一个分支(origin/selected_branch)执行rebase/merge操作,而在接下来的两个选项中(pull into...)对另一个分支(current_branch)执行操作。
- 要么在所有4个选项中都执行对同一个分支(current_branch)的pull/merge/rebase操作。
- 要么以不同方式分组菜单选项,以便不会看起来rebase和merge(前两个选项)都针对同一个分支。
也许我理解错了些什么。如果是这样,请纠正我。
我使用了我在问题中提到的命令来从远程分支拉取更改到本地分支。我想弄清楚在我问题中提到的每个菜单选项中更改是从哪个分支到哪个分支进行的。
英文:
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:
- 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).
- Or in all 4 options do pull/merge/rebase into the same branch (current_branch)
- 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.
答案1
得分: 1
你的第2点是正确的:所有4个选项只会影响你当前检出的分支(current_branch
)。"rebase onto"的措辞起初有点令人困惑,因为它听起来像是你正在向该分支添加提交并修改它,但实际上,措辞:
> 将分支X重新设置到分支Y 意味着:
>
> 获取分支X
上所有(非合并)不可通过分支Y
访问的提交,并按顺序一个接一个地在分支Y
上重放它们,然后将分支X
重置为指向最终结果。
请注意,在上述段落中使用了"可达"一词,因为Git历史是一个图形。这基本上是一种表达是否一个提交在另一个提交(或分支)的历史中的方式。
至于两组之间的区别:rebase和merge,与带有rebase或merge的拉取的唯一区别是,使用pull时还将执行git fetch
,这意味着如果自上次获取(或拉取)以来远程服务器上的原始分支发生更改,则它们将被更新。如果只使用rebase或merge,命令将根据那一刻origin/selected_branch
指向的提交执行。通常你的远程是在另一个服务器上,因此你必须在线才能拉取并获取最新的。在离线状态下,即使不拉取,也可以进行rebase和merge。
附注: 你可能不想rebase一个共享的分支,比如master
,在你截图中它是最上面的(被下划线标记的)选项,当你检出一个特性分支时。那个顶部选项可能只在你当前检出了共享分支(如master
)的情况下,然后右键单击一个特性分支并打算将其rebase到master
时才有意义。
免责声明: 我从未使用过Android Studio,但我知道这些选项在其他工具中的含义,我认为可以安全地假设它们在这里有相同的含义。
英文:
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.
答案2
得分: 0
术语在开始时可能会令人困惑,尤其是理解 rebase 和 merge 的用法。
简单来说:rebase 和 merge 都会更新项目的源代码。不同之处在于如何处理提交历史。
至于你提到的这4个选项,除了使用 pull 时会执行 GIT fetch 并获取你的远程分支的最新版本(在这种情况下是 master),它们都会更新你当前的分支,就像 TTT 已经提到的那样。
确切地说:
-
Pull into Current Using Rebase(对于远程分支)是为了从所选分支获取更改并将当前分支重新基于这些更改。
-
Checkout and Rebase onto Current(对于远程和本地分支都适用)是为了检出所选分支并将其重新基于当前检出的分支。如果远程分支在本地不存在,IntelliJ IDEA 将会悄悄地创建一个已跟踪的本地分支,切换到它并进行 rebase 操作。
-
Rebase Current onto Selected(对于远程和本地分支都适用)是为了将当前检出的分支重新基于所选分支。
这4个选项都是 git 命令,pull 是一个组合的 git 命令(fetch + merge/rebase),一旦执行它们,你可以在控制台中看到两者都执行的情况。
对比
你可以在这里找到更多信息:
https://www.jetbrains.com/help/idea/apply-changes-from-one-branch-to-another.html#rebase-branch-onto-another-branch
你也可以尝试使用:https://plugins.jetbrains.com/plugin/8554-ide-features-trainer(其中 git 部分很有趣)。
英文:
Terminology can be confusing at start, especially understanding the usage of rebase and merge.
Regarding this specific topic, I suggest you try: https://learngitbranching.js.org/, to learn git branching in more detail.
In Layman's terms: Both rebase and merge update project’s source code. However, the difference is how commit history is handled.
As for these 4 options you have mentioned, all 4 update your current branch as TTT already mentioned except that with pull GIT fetch is performed and you receive the latest version of your remote branch (in this case master)
To be exact:
Pull into Current Using Rebase (for remote branches) to fetch changes from the selected branch and rebase the current branch on top of these changes.
Checkout and Rebase onto Current (for both remote and local branches) to check out the selected branch and rebase it on top of the branch that is currently checked out. If the remote branch doesn't exist locally, IntelliJ IDEA will silently create a tracked local branch, checkout into it and rebase.
Rebase Current onto Selected (for both remote and local branches) to rebase the branch that is currently checked out on top of the selected.
All of these 4 options are git commands with pull being a coupled git command (fetch + merge/rebase) and once you execute them both can be seen in the console.
VS
You can find more information here:
https://www.jetbrains.com/help/idea/apply-changes-from-one-branch-to-another.html#rebase-branch-onto-another-branch
And you can try out: https://plugins.jetbrains.com/plugin/8554-ide-features-trainer (git part is interesting)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论