How to do `git switch <branch-name>` on IntelliJ IDEA?
When using Git to switch between branches in my working directory, I deliberately use `git switch <branch-name>` on the command line, in order to avoid inadvertent operations that the powerful `git checkout` can do.
For example, on the command line, when I do `git switch <feature_branch>` on a branch that does not exist in my repository, I get:
fatal: invalid reference: <feature_branch>
and I like that, because it reminds me of avoiding certain hasty git operations that can later cost me a lot of time to undo.
Since IntelliJ IDEA is so much more convenient, I was looking for the `git switch` equivalent in it, but all I could find is checkout:
Right-click a module > `Git` > `Branches…` > Select (local) branch > `Checkout`.
Aside from the lengthy mouse-clicking sequence, I am unsure what it will do when using it: Will it checkout a remote branch if a local one does not exist? Will it checkout or switch?
Please help me figure this out, so that when I perform this mouse-clicking sequence, I am confident what IntelliJ does behind the scene.
I am using IntelliJ IDEA 2023.2.1 (Ultimate Edition)
Thank you
请先登录再写评论。
Hi,
All git related commands that you perform in IDE are visible in the console once you perform them.
When you perform a checkout via UI, standard git check-out is performed.
As for “Will it checkout a remote branch if a local one does not exist? Will it checkout or switch?”,
If a local branch does not exist it will not be on the list. If you checkout a remote branch, a copy of it will be made as local immediately and it will checkout.
Thanks Dino.
I finally got to try:
but I could not notice any logging in IntelliJ's Terminal.
So, I went to the Git `Tool Window`, noticed a `Console` tab right to the `Log` tab, clicked it and, lo and behold, I can see the log of my manual
The log message indeed says “Switched to branch ‘staging’ (along with a timestamp)”
This essentially answers my question about how to `git switch` in IntelliJ: It basically follows the older git versions in which `git checkout` was the only way to `git switch`.
Hi,
Do you have added git switch and git restore in last Intellij version ?
git checkout is now deprecated by many company
Regards
Hi,
No, we do not have plans to do such action and we see no special need for this command to be introduced.
Do you have any use-case? Also, what does it mean that many companies deprecate “checkout” ?