GitHowTo

19. Switching branches

Goals

  • Learn how to switch between the repository branches.

Now our project has two branches:

Run

git log --all

Result

$ git log --all
9288a33 2023-11-28 | Added copyright statement with email (main) [Alexander Shvets]
903eb1d 2023-11-28 | Included stylesheet into hello.html (HEAD -> style) [Alexander Shvets]
b7614c1 2023-11-28 | Added HTML header (tag: v1) [Alexander Shvets]
46afaff 2023-11-28 | Added standard HTML page tags (tag: v1-beta) [Alexander Shvets]
555372e 2023-11-28 | Added css stylesheet [Alexander Shvets]
78433de 2023-11-28 | Added h1 tag [Alexander Shvets]
5836970 2023-11-28 | Initial commit [Alexander Shvets]

01 Switching to the main branch

To switch between branches, use the git switch command.

Run

git switch main
cat hello.html

Result

$ git switch main
Switched to branch 'main'
$ cat hello.html
<!-- Author: Alexander Shvets (alex@githowto.com) -->
<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

Now we are on the main branch. As you see, the hello.html has no traces of style.css. Don't worry; it is still in the repository, but we can't see it from the main branch.

02 Let us return to the style branch

Run

git switch style
cat hello.html

Result

$ git switch style
Switched to branch 'style'
$ cat hello.html
<!-- Author: Alexander Shvets (alex@githowto.com) -->
<html>
  <head>
    <link type="text/css" rel="stylesheet" media="all" href="style.css" />
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

We are back to the style branch. As you can see, our CSS-related changes are present.

20. Moving files18. Creating a branch