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.

First you need to update your local repo with the changes made to the remote one. This ensures that you have a full list of remote branches and the latest changes in the code to avoid conflicts.

  • Enter the command below to update your local repo:

    git pull

  • Switch to the main branch by using this command:

    git checkout main

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

  • Switch back to your branch by using this command:

    git checkout <github_username>/main

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

Alternative method

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