Remote Repositories
As previously discussed, Git is a distributed version control system in which the entire repository exists on each developer's computer. As developers make and commit changes, these changes still only exist on their local machines. So, how do developers share their commits? This is where remote repositories come in.
A remote repository is a Git repository stored in a location that all team members can access. This remote repository is then connected to the local repositories, which allows for easy and fast syncing between the local and remote repositories.
Preparing Git to work with GitHub
We need to connect our local(on our computer) git installation with our GitHub account. This is a two step process:
- Generating an personal access token on GitHub
- Using a credential helper to remember our token on our computer
Generating a personal access token
Login to your GitHub account and go to your account settings, clicking on your profile icon on the top right corner.
From the left-side menu select the Developer Settings option
On the next screen select the Personal access tokens setting and click Generate new token
Enter your GitHub password if prompted
Next add a custom name for your token, and select repo
and admin:repo_hook
scope for your token
Your token is ready, you can copy the token by clicking the clipboard icon from this screen when needed. Leave this window open for later reference.
Installing a credential helper
In this step we will install a credential helper for caching out personal access token so we do not have to enter it every time we are working with our remote repository.
These instructions are taken from GitHub docs.
Open your Visual Studio Code and open a new terminal from Terminal > New Terminal option at the top.
Mac Users
- Find out if Git and the osxkeychain helper are already installed:
git credential-osxkeychain
If you see the following output then the credential helper is already installed on your Mac.
Usage: git credential-osxkeychain <get|store|erase>
- If the osxkeychain helper isn't installed and you're running OS X version 10.9 or above, your computer will prompt you to download it as a part of the Xcode Command Line Tools:
git credential-osxkeychain
xcode-select: note: no developer tools were found at '/Applications/Xcode.app',
requesting install. Choose an option in the dialog to download the command line developer tools.
- Tell Git to use osxkeychain helper using the global credential.helper config:
git config --global credential.helper osxkeychain
# Set git to use the osxkeychain credential helper
Windows Users
Install the credential helper by running the following command in your VS Code > Terminal
git config --global credential.helper wincred
Cloning a Repository
The git clone
command is used to "copy" a remote repository to a local computer. This process will also make a connection between the two so that syncing can occur.
We have a remote repository on GitHub that we want to clone down to our local machine. We would navigate to the repository page on GitHub, https://github.com/imdac/git-up, and from there get the repository URL, which is found under the Clone or download button. Clicking the clipboard icon (📋) will copy the URL.
We can clone a remote repository using the Command-Line. We start by opening our command-line tool and navigating to our projects folder.
# navigate to the projects folder
cd projects
Next, we type git clone
followed by the URL that we copied. Then hit enter.
# clone the repository
git clone https://github.com/imdac/git-up.git
Syncing Repositories
A remote repository is only useful if changes made locally can synchronize to the remote repository. Fortunately, this can be done using the pull
and push
commands.
Pulling
Let's say a week has gone by, and other team members have made changes to the remote repository. We need to "pull" changes down to our local repository.
# pull commits from remote repository
git pull
This process can also be done using VS Code by using the Synchronize Changes button found in the Status Bar.
Pushing
So, we have cloned the remote repository and are now working locally. We made some changes to our local repository and committed those changes locally. Now we are ready to sync our commits to the remote repository. We do this using git push
.
# push commits to the remote repository
git push
That's it. Git will take all of the commits that we have added since we synced our project and "push" them to the remote repository.
This process can also be done using VS Code by using the Synchronize Changes button found in the Status Bar. For more information, refer to the Official Documentation