📖 GitHub

While Git and GitHub desktop are great local version control programs, GitHub is an online tool for managing repositories with tight integration in VS Code. GitHub is vital for teams to collaborate on projects and to share code due to its ubiquitous availability. Once you establish using Git, you'll want to move to GitHub.

Getting a GitHub Account

There is no new software to install to begin using GitHub. You can actually use it without an account and without creating a repository (repo) of your own code since you could use it only for access to other developer's code. 

You are not required to download software to make GitHub work since VS Code provides tight GitHub integration, though you might want to download the desktop version. Here are the links to the available GitHub resources.

  1. Setup a GitHub account. You'll need a place to store your projects in the cloud. GitHub is designed to do just that.
  2. Download and install GitHub for desktop. This one is not required, but may be helpful especially if you are not comfortable with the command line.

Creating a Repository Online

It helps to already have a working knowledge of setting up a Git repository, staging changes and making commits before you delve into using GitHub. If you need a primer to get started using Git, review the Get Git article. Once you have your GitHub account setup, you'll want to create your own repository there. You can easily achieve that by connecting your VS Code to your GitHub account. Once that is done, you can use VS Code to sync your code changes to GitHub. You can also use GitHub desktop for this purpose.

Here are the steps in creating a GitHub repository from an existing local repository using VS Code.

  1. Stage all changes.
  2. Commit changes to local Git repository.
  3. Click the "Publish Branch' button.
VS Code screen shot showing Publish Branch button
VS Code Publish Branch Button

When you click the Publish Branch button, VS Code will ask if you want the repository on GitHub to be public or private. It's just like it sounds. Public will make your repository available for anyone who finds it on GitHub though there are copyright rules that govern the use of software. If you choose private, your code will not be available unless you specifically share it with someone.

VS Code screen shot showing Sync Changes button
VS Code Sync Changes Button

Login to your GitHub account and check to see that the repository has been added. From now on, you will be prompted to sync your changes to your remote GitHub account. This will be an optional step you may choose to do less often than your local commits. Don't worry. Git will keep up with your changes as you update them.

Cloning an Existing Repository

One of the new terms you will learn with GitHub is clone. Clone is a reference to the process of downloading a copy of a codebase from GitHub. This is part of the collaboration environment GitHub provides to developers. Your public repositories will also be available for others to clone and use within the designated Copyright License.

VS Code screen shot showing Sync Changes button
VS Code Clone Git Repository... Link

To clone an existing repository from GitHub, click Clone Git Repository... from the start menu in VS Code. The VS Code commands opens and prompts you to enter the URL for the repository. This link can be found in the repository in GitHub. Copy and paste the link in the VS Code command tool. You will then be prompted to choose a local location to save the repository. Navigate to the location of your choice and click Select Repository Location to finish the process. VS Code will download the repository and ask if you want to open the repository. 

VS Code screen shot showing Sync Changes button
GitHub Clone Repository Link

When you download files from an unknown source, VS Code will ask if you trust the source. Be careful that you do trust the source of the code you are loading. If it is malicious, it could harm your computer or steal your information. 

VS Code screen shot showing Sync Changes button
VS Code Trust Verification Warning

Other GitHub Processes

GitHub offers many other processes to support version control of software development for collaboration. Most have to do with controlling updates from multiple developers which include a process of discussions to determine whether a particular coding solution is acceptable before the original code base is updated. The method Git provides for this is called forking. A fork in the software development process is an activity whereby the code is copied for development on an alternate track. The code is copied using the clone process described above then developers work on revisions of the code in an effort to fix bugs and add features. When the developer is ready to propose the changes, the code is submitted back to the GitHub codebase through the push process. The developers on the team have a chance to review the proposed submissions and reject or accept part or all of the changes. The accepted changes are then merged into the original codebase.

Below is a list of common GitHub commands and a description of their related process taken from the cheat sheet at the GitHub training site. See if you think these are understandable, even intuitive, for software development.

Branches

Branches are an important part of working with Git. Any commits you make will be made on the branch you’re currently “checked out” to. Use git status to see which branch that is.

$ git branch [branch-name]
Creates a new branch
$ git switch -c [branch-name]
Switches to the specified branch and updates the working directory
$ git merge [branch]
Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.
$ git branch -d [branch-name]
Deletes the specified branch
Synchronize changes

Synchronize your local repository with the remote repository on GitHub.com

$ git fetch
Downloads all history from the remote tracking branches
$ git merge
Combines remote tracking branches into current local branch
$ git push
Uploads all local branch commits to GitHub
$ git pull
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge