How to Sync Your VS Code Project with a New Branch on GitHub After Deleting the Old Branch

Step 1: Fetch the Latest Changes

To update the list of branches in your local repository, you need to fetch the latest changes from the remote repository. This step is crucial to ensure your local Git setup is aware of the changes made on GitHub.

  1. Open the Terminal in VS Code: You can open the integrated terminal in VS Code by pressing Ctrl +` (backtick).
  2. Run the Fetch Command: In the terminal, type the following command and press Enter: git fetch -p

  • The -p flag prunes any deleted remote-tracking branches, effectively cleaning up references to the old branch that you deleted on GitHub.

Step 2: Check Remote Branches

After fetching the latest changes, you should verify the list of remote branches to ensure the new branch is available.

  1. List Remote Branches: In the terminal, type the following command and press Enter: git branch -r

  • This command lists all remote branches, allowing you to see if the new branch is present.

Step 3: Check Out the New Branch

If the new branch exists on the remote repository, you can check it out locally. This step creates a local copy of the branch and sets up tracking for future changes.

  1. Check Out the New Branch: In the terminal, type the following command and press Enter: git checkout new_branch_name

 

  • Replace new_branch_name with the actual name of your new branch.
  • Track the Remote Branch: If you want to ensure your local branch tracks the remote branch, you can use the following command: 
  • git checkout -b new_branch_name origin/new_branch_name

 

  • This command not only checks out the branch but also sets up tracking between your local branch and the remote branch.

Step 4: Push the New Branch (if needed)

If you’ve created a new branch locally and need to push it to the remote repository, follow these steps:

  1. Push the New Branch: In the terminal, type the following command and press Enter:

  • This command pushes your new branch to the remote repository and sets the upstream tracking.

Step 5: Switch Branches in VS Code

Finally, ensure that the new branch is visible and selectable in VS Code.

  1. Open the Source Control Panel: Click on the Source Control icon in the Activity Bar on the side of the VS Code window.
  2. Switch Branches: In the Source Control panel, click on the current branch name. This action will open a drop-down menu where you should see the new branch available for selection.

Leave a comment

Your email address will not be published. Required fields are marked *