Set up Git with repository on GitHub or Azure DevOps Repos

github
In setting up our production environments, we’re started to get some code that we will want to backup, save, reuse, make changes, and share with others. We will want collaborate. And a source control system is idea for all this.

Git is distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is a primary tool for both developers and cloud engineers who are moving to infrastructure as code. In our next post you will learn more about Git workflows. But first, because our blog is related to enterprise production, you will you will want to set up a repository for your code.

The purpose of this article is to provide the steps to get set up and provide the steps for some common scenarios for both GitHub and Azure DevOps so you can get started checking in code.

Prerequisites

You will want some basic familiarity with Git, Azure DevOps, and GitHub. The article does not go into depth on each topic, but rather provides an overview to the steps and some helpful code snippets for common cases.

What’s the difference between Git and GitHub?

The key difference between Git and Github and Azure DevOps is that

  • Git is an open source version control system
  • Github and Azure DevOps Repos are both hosting service for Git repository

Git repository is a virtual storage of your project. It exists on your local development machine. A repository is where you save versions of your code, which you can access when needed. Your local computer has a Git repository. A git repository contains, among other things, the following:

  • A set of commit objects.
  • A set of references to commit objects, called heads.

There is nothing in Git that requires you to have a remote service like GitHub if all you want is version control. Your local git is just fine for that. Remote repositories are for backup and collaboration.

Many repositories work with Git, such as BitBucket, SourceForge, GitLab, and ones that we’ll talk about in this blog, Azure DevOps and GitHub. Or you can set up your own hosted Git repository.

GitHub or Azure DevOps

A Cloud Guru describes these as DevOps twins.

The choice isn’t necessarily between the two repos. Actually you can use both. Azure DevOps integrates nicely with Git and with GitHub. Azure DevOps integrates nicely with Bitbucket too, and many others.

Azure DevOps brings you the tools you need to manage large projects, including boards,

introducing-azure-devops

In this blog, we will show how to integrate into both GitHub and into Azure Repos. And we’ll show how to deploy using both Azure Pipelines and GitHub pipelines.

My answer is you can use either one or both. Even small organizations can easily use one or both.

Let’s take this in three steps:

  1. Set up Git
  2. Set up the hosted repository
  3. Connect Git local repository to the hosted repository

First, let’s set up your Git.

Set up Git

Git-Logo-2ColorLet’s get started by installing Git. Source control is one of the most basic tools that is used in software development. As a cloud engineer, Git becomes the tool for creating and making changes to your Infrastructure as Code. And Git provides tooling inside the tools you are used to using.

Git installation

First, you will need to install Git on your local development/engineering machine. Select one of the following.

Note: Git is already installed on Azure Cloud Shell.

You can access Git through your command line. Or you can use Git with your favorite editors.

Git in Visual Studio

Visual Studio has had a Git client built directly into the IDE since 2013 update 1. Open a project that’s controlled by Git (or just git init an existing project), and select the menu item View | Team Explorer from the menu.

azdevopsinvs

See Improved Git Experience in Visual Studio 2019 for how you can access the Git features. Many users prefer the visual approach in pushing and pulling code and branching of your repository. There are excellent comparison tools too for you to see changes between commits and changes made in branches.

Visual Studio Code

Visual Studio Code ships with Git support built in. VS Code will leverage your machine’s Git installation, so you need to install Git first before you get these features. You will need to have git version 2.0.0 (or newer) installed.

You can do the most common git operations from within the editor:

  • Initialize a repository
  • Clone a repository
  • Create branches and tags
  • Stage and commit changes
  • Push/pull/sync with a remote branch
  • Resolve merge conflicts
  • View diffs

And you can do pull requests with the GitHub Pull Requests and Issues extension.

See Using Version Control in VS Code for more information.

GitHub’s Hub

If you are creating a lot of repositories or want a tool to help you get started quicker, you can use hub, a command line tool for GitHub. Hub is designed to common workflows so simple that beginner and experienced developers alike are productive in working with Git and GitHub.

Other tooling

The Git documentation references in this section provides many helpful hint on having a good experience with Git.

Now that you have installed Git and installed it in your favorite tool, let’s create the repository.

Create a repository

In this step, you will create a repository to host your source code and allow your teams to interact, change, and approve.

Set up your repository so you can use the URL of the repo to connect your local Git repository with the hosted one. The scripts use the name of the remote URL on GitHub as https://github.com/user/repo.git.

Create a remote repository on GitHub

To create a remote repository on GitHub, first create a GitHub account.

  1. Go tohttps://github.com/join in a web browser.
  2. Create a username and entering an email address and  password.
  3. Verify you are not a robot.
  4. Select a GitHub Account Plan. The free version provides plenty to go for a long time with a small team — including unlimited public/private repositories
  5. Provide some information about your programming experience and how you want to use GitHub.
  6. Will end up at the dashboard.

In the GitHub portal, click New repository menu item.

newrepogithub

Type in the name you want to use and an optional description. Include your selection for visibility, meaning if you want to repository to be public or private.

And you have a repository. For the following steps, you will want the URL that looks like: https://github.com/username/projectname.git

Set the repo name into your command prompt:

For more infomation, including how you can use a template to create similar repositories, see Creating a new repository in Github.

Create a remote repository in Azure DevOps using the CLI

You will need an organization in Azure DevOps and permissions with the organization, log into Azure. Then use the following script to create a repository using Azure DevOps.

The name of the repository will have this format https://{organization}@dev.azure.com/{organization}/{teamProject}/_git/{repository}

For more information, see az devops configure.

Connect Git to your remote repository

In this step, you will connect your local Git repository with the remote repository on either GitHub or Azure DevOps. You will learn how to connect the two from two scenarios:

  • Clone from a remote repository. (Easiest and most common)
  • Start with code on your local development/engineering machine and then connect it to a repository

Clone from a remote repository

The following code creates a clone of the repository in the myclonedrepo folder. Ir uses the $REPOSITORY_URL that you got when you created the repository in the previous step.

For more information, see git-clone.

Start on local machine and connect to a remote repository on GitHub

If you start writing code on your local machine and then want to connect to the repo, just use git init and then connect to the repo using git remove add origin.

The following code shows the steps for when you start on your local machine and then want to connect to a remote repository.

Start on a local machine and connect to a remote repository on Azure Devops

When you create a new project in Azure Devops, go to the Repos panel. Click Files. Click the paper icon to copy scripts you can use to connect Azure Devops to push your remote repository.

pushexistingdevops

The name of the repository will have this format https://{organization}@dev.azure.com/{organization}/{teamProject}/_git/{repository}

The code will be similar to the following:

Check if the Local Repository is connected with Remote Repository

Once you have completed connecting, you will want to check the remote status using the following code:

You may see multiple rows in the response. These are the various repos that you can fetch or push your code to using  git fetch. For instance, git fetch origin fetches from the remote named origin.

Summary

In this post you learned about Git and Git repositories and how to set up a hosted repository in both Azure DevOps and GitHub.

In our next post, you will learn some basic workflow patters in Git using a cheatsheet.

References


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s