Moving between branches

Now that you’ve made and recorded some changes on your working branch and synced them to the remote repo, we can look at how to move between branches and what that looks like in terms of file changes.

Switching branches is useful for when we might need to pause our work on one branch and continue working on an issue in another branch. Before you switch branches it is important to commit the changes you made in the branch you’re currently in. It’s also important to pull changes after you switch to your new branch.

Assuming you’ve not skipped any steps, you should now have at least 2 branches that you’ve had created during this tutorial: <github_username>/main and <github_username>/development, as well as the original main, branch.

Use git checkout to switch between these branches.

  • git checkout main
  • git checkout <github_username>/main
  • git checkout <github_username>/development

replacing <github_username> with your own username.

Tip

Check the contents of the git-academy-log.md file each time you switch branches and you should see changes occur everytime you switch.

Note

You can also use the git switch command interchangeably with the git checkout command for switching branches.

  • Go to the right hand side of RStudio and click on the Git tab.

  • Click on the downwards arrow to “pull” the latest changes from the remote repo. This ensures that:

    • you have a full list of remote branches.
    • you have the latest changes in the code to avoid conflicts.
  • Click on the drop down menu of branches.

  • Click on main

The Git panel in RStudio with red rectangles highlighting the downwards pull arrow, the branches drop down menu and the branch called main that we want to switch to.

  • Check the “git-academy-log.md” file and you’ll see your edits have disappeared.

  • Click on the drop down menu of branches.

  • Click on <github_username>/main

The Git panel in RStudio with red rectangles highlighting the branches drop down menu and an example of a user branch that we want to switch to.

  • Check the “git-academy-log.md” file and you’ll see your edits have returned.
  • Click on the branches drop down menu at the top of the screen.

  • Click the Fetch icon to “pull” the latest changes from the remote repo. This ensures that:

    • you have a full list of remote branches.
    • you have the latest changes in the code to avoid conflicts.
  • Click on “Remote” to see the full list of remote branches if you can’t already.

  • Click on mainand then click “Checkout”.

Highlighting PyCharm's branches dropdown menu, the Remote drop down sub menu, the branch called main and the Checkout option that appears when you click on that branch.

  • Check the “git-academy-log.md” file and you’ll see your edits have disappeared.

Highlighting PyCharm's branches dropdown menu, the Remote drop down sub menu, an example of a user specific branch and the Checkout option that appears when you click on that branch.

  • Click on the branches drop down menu at the top of the screen.

  • Click on <github_username>/main and then click “Checkout”.

  • Check the “git-academy-log.md” file and you’ll see your edits have returned.

Back to top