Work with a remote Git Repository
After you created a link between your local Git repository and the remote repository either via the remote command or using the clone command, the synchronization isn't happening automatically. Use push and pull commands to get from and send data to the remote repository.
The best practice is to first execute a pull command and then a push.
git pull <remote-name> <branch-name>
If files are changed by other contributors, then you will probably end up with merge conflicts. Resolve these conflicts in the local repository before you push to the remote repository. The resolved conflicts will lead to a new commit on your local repository. The pull command will download all the new and modified files to the local repository. It will do this only for the branch that is currently in use. Afterwards, you can execute the push command.
git push <remote-name> <branch-name>
In Visual Studio Code, you can find the pull and push commands in the SCM command menu. Click the three dots to open the menu. You can also use the sync command, which will first execute a pull, and if there are no conflicts, it will automatically execute the push command.
You can also click the sync icon in the left bottom corner of the Visual Studio Code window, next to the branch name.
Each time when you do a local commit, Visual Studio Code will show you how many commits you are ahead of the remote repository. But it's possible that someone else added some new commits to the remote repository. This means that you are behind the remote repository and that you first need to execute a pull command before you do a push.
You can see the status in the left bottom corner of the window. In the next example, the local repository is one commit ahead of the remote repository. You can see that there is one commit to be pushed.
To check if there are new commits waiting, you can use the fetch command. The fetch command will ask the remote repository if there are structural changes to the repository. Are new branches created? Are new commits pushed? We'll look at branching in another module, but if a branch is created on the remote repository, you first need to run the fetch command to have your local repository discover that a new branch is created.
git fetch
In Visual Studio Code, you need to select View, Command Palette (Ctrl+Shift+P) to search for the fetch command. The Fetch (Prune) command will remove local entries to remote branches if these branches are deleted on the remote repository.
Visual Studio Code support the option of Autofetch. This setting will automatically execute the fetch command each number of seconds. This way Visual Studio Code will automatically show you if there are incoming commits on the remote repository. You can enable or disable this setting using File, Preferences, Settings (Ctrl + ,). In the settings list, search for Autofetch and you'll see two settings you can change. One to enable or disable the Autofetch setting, and the other one to set the period.
Working with a remote Git repository
Watch the following video to see a demonstration of how you can work with a remote Git repository.