Using Git in Visual Studio Code

Once you’ve set up a GitHub repository, you can share and pull changes from your teammates while keeping track of the history of your code. This code-sharing process involves pushing, pulling, and sometimes merging code, all of which are important concepts in git. This tutorial will provide instructions for using the git integration within the Visual Studio Code environment. If you are familiar with the command line git, you can use that instead.

Pushing Changes

Once you’ve made changes to your repository, you need to push them to the remote repository to save the current version history so your teammates can access them.

  1. Run make clean or mingw32-make clean in the VS Code terminal to remove .o and .exe files
  2. Select the Source Control view in VS Code on the left-hand menu bar, shown in Figure 1

    Figure 1: Source Control View

  3. Stage your changes by selecting the “+” sign next to Changes

    Figure 2: Stage Changes

  4. Commit your changes

    Figure 3: Commit Changes

  5. Write a Git Commit Message (see the page on Git Commit Messages for tips on how to write a good commit message)

    Figure 4: Commit Message

  6. Push your changes to the remote repository so your teammates can access them

    Figure 5: Push Changes

    VS Code may ask if you would like to periodically Fetch. Select Yes. This will avoid you from having to manually fetch changes from your teammates into your local repository.

    Figure 6: Periodically Fetch

  7. Check your GitHub repository- you should see the changes you just pushed!

 

Pulling Changes

If your teammates have pushed changes, you need to pull them into your local repository. To do this, navigate to the Source Control view, hover over the three dots, and click on Pull.

Figure 7: Pull Changes from Remote

Make sure to always Pull before you commit your changes! This will help you avoid merge conflicts!

 

Managing Merge Conflicts

Sometimes when you attempt to push your changes that conflict with work a teammate has done, you will end up with merge conflicts. There are three types of merges: (1) fast forward (no merge needed), (2) no conflicts (automatic merge by git), and (3) conflicts. This third case will require work on your part to resolve.
  • Some files may be automatically merged
  • Any files that contain conflicts will be marked in your directory of files, and conflicting sections will be marked with auto-generated in-code comments and highlighting in Visual Studio Code
To resolve merge conflicts:
  1. Manually review every file with conflicts and keep the changes you want, removing the auto-generated in-code comments
  2. Stage, commit, and push

 

More Git Resources