Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between git command - checkout and switch

What's the difference between git checkout -b branch_name vs git switch -c branch_name?

I tried git command -h as a way to find solution and I was looking for it in documantation. In neither way I managed to find it. In git's documantation I've found this:

Note that this will create the new branch, but it will not switch the working tree to it; use "git switch " to switch to the new branch.

but either way I typed checkout or switch commnad, my current branch was changed.

like image 958
wz7475 Avatar asked Mar 02 '23 16:03

wz7475


1 Answers

Generally, as it was mentioned in the comments there is no actual difference between git switch & git checkout in terms of what You can do with branches.

The idea to create the git switch & git restore because they were introduced together, arose from multiple questions & issues that new users had with git checkout. Previously, you could use the git checkout to switch to a different branch, but also you could use it to restore changes from some specific commit or even to restore the changes for a single file.

The problem that was noticed by some of the users is the fact that operation on commits/files & operation on branches are quite different and having them grouped by a single command can be troublesome. That's why there were two new commands introduced restore to allow restoring changes and switch to allow operations on branches.

So, to sum up, there is no difference between the git switch and git checkout in terms of switching branches and there is no difference between git restore and git checkout in terms of restoring changes for files or commits. You can use them interchangeably. The only thing to have in mind is the fact that switch & restore are still marked as experimental, so I wouldn't use them for creating long-living scripts or so.

like image 137
Dominik Wosiński Avatar answered Mar 05 '23 14:03

Dominik Wosiński