Creating Branches

Creating and Switching Branches

git switch <branch> points HEAD at a branch and updates the index and working tree to its tip; -c <new> creates the branch first, and git switch - goes back. Git 2.23 1,932 (2019) split this out of git checkout, which also overwrites files, so one wrong argument can discard edits. Build the search feature on its branch:

Committing on a branch and switching backShell
git switch search-api
# Edit app.js: add the ?q= filter that git diff shows below
git diff
git commit -qam "Search book titles with ?q="
git switch main
Output
Switched to branch 'search-api'
...
@@ -41,6 +41,10 @@ function createApp() {
...
+      if (req.query.q) {
+        params.push(`%${req.query.q}%`);
+        clauses.push(`title ILIKE $${params.length}`);
+      }
...
Switched to branch 'main'

The search term travels as a query parameter, never pasted into SQL. Back on main, app.js has no search filter: switching rewrote only files that differ between the tips. If it would overwrite uncommitted edits, Git refuses until you commit or stash them (Stashing Work in Progress).