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
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
- 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
main
and then click “Checkout”.
- Check the “git-academy-log.md” file and you’ll see your edits have disappeared.
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.