↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Why Use git switch Instead of git checkout?

git switch is presented as a newer command in Git 2.23 that makes branch changes more direct and easier to understand. The page contrasts it with git checkout, which can also handle file operations, and says that separation reduces mistakes when working across branches.

The article also shows common uses such as switching to an existing branch, creating a new one with -c, moving to a commit in detached HEAD with --detach, and forcing or discarding changes when local modifications block the switch. It closes by recommending thoughtful branch changes, git stash, descriptive branch names, and keeping Git updated.

Reading notes
#

  • git switch was introduced in Git 2.23 to switch branches in a simpler and more focused way.
  • git checkout accumulates branch switching, file operations, and branch creation, which can cause confusion.
  • The separation of responsibilities in git switch makes the flow more intuitive and reduces errors in branch management.
  • The basic syntax is git switch [options] <branch>.
  • -c <branch> creates a new branch and switches to it immediately.
  • --detach takes you to a specific commit without associating the navigation with a branch.
  • -f forces the switch and discards uncommitted changes.
  • --discard-changes also discards local modifications when switching branches.
  • When using git switch on an existing branch, uncommitted changes are preserved if they do not conflict.
  • A practical flow described uses git stash, git switch main, fixing the bug, and then returning to feature-branch with git stash pop.
  • The final comparison highlights that git switch covers branch switching, branch creation, and detached HEAD, but not file operations.
  • The common problems mentioned are local changes that prevent switching and a nonexistent branch.
  • The listed best practices are to check the branch before changing anything, combine it with git stash, use descriptive names, and keep Git updated.