By: Matt Wollner
Our Branch-Based GitHub Workflow
Now that Microsoft has acquired GitHub, our team here at Key2 Consulting is starting the transition from Team Foundation Server (TFS) to GitHub for our database source control. If you are accustomed to working with TFS, the transition to GitHub can be confusing.
In this blog post, we share a basic workflow we have adopted to start using GitHub as our project source control. Our GitHub workflow takes advantage of Visual Studio Code (VS Code). We will cover (in this post) the basics of how to get a local copy, push code changes to the development branch, and then pull those changes into the master branch.
Master Branch and Develop Branch
There are often many code changes in between releases when working on a project. Some of these changes are ready for deployment to a production environment, while some are under development and testing.
At Key2 Consulting, we use a branch-based workflow to help pick the changes that are ready for deployment. Our environment has two main branches: master (or main) branch and develop branch.
Hotfix branches are created to move code from the develop branch to the master branch, and are then deleted after master has been updated.
- Master Branch – Once code is added to the master branch it is ready for deployment. The master branch will represent production-ready code.
- Develop Branch – All ongoing development should be done in the develop branch.
Downloading a Branch (Clone)
Let’s start by setting up our working environment. To do so, we will “clone” the develop branch. This will download the latest code in the development branch into our local PC.
In my personal working environment, I like to have a copy of both the develop and master branches. I create a root folder for the projects and a folder for each branch underneath.
- In Files Explorer, create a folder for your working develop environment.
- Open VS Code and open the new folder. File –> Open Folder
- Jump over to your GitHub website. Get the Clone HTML. Click the Clone or Download button and copy the URL.
- Clone the repository.
- Back in VS Code, go to the Terminal command prompt and type in the command: git clone https://githuburl…
- Open the Command Palette… (Ctrl+Shift+P)
- You should now see the repository in your local folder.
– In this example, I am pulling the https URL. You may set your URL to begin with git:// or use SSH transfer protocol user@server:path/to/repo.git.
OR
– Git: Clone
– Paste in URL
In VS Code, on the bottom left of the window you will see what branch you are currently working with. Mine currently says “master”. I need to change it to develop. If you click on master you will see the git command prompt drop down, but sometimes you may not see all the branches that are in the GitHub project.
To refresh the ref list you may need to run the fetch command.
- In the Terminal command prompt (Ctrl+`), run the command:
- Now if you click on the branch name in the bottom left, you should see all your branches in the drop down. Select the develop branch.
git fetch
Working With Your Local Copy
While working on your local copy of the repository, you should frequently get the latest version from GitHub. In Git, this is done with the “pull” command. If you are editing objects, you need to make sure you have the latest version before you begin.
Under the Source Control tab, click the “…” -> Pull
Or
Terminal command prompt
Git pull
Committing to the Remote Develop Branch
After you have made some code changes and they have all tested out, you will want to deploy the changes to the remote develop branch. From VS Code you will use the Stage, Commit and Push commands. All files that have been altered will show up in the Source Control tab.
- Select the files you wish to deploy from your local environment to the remote develop branch.
- “Stage”.
- “Commit”.
- “Push”.
- Create a new branch from the master branch.
- Under your main folder, create a Hotfix folder.
- Open VS Code and Open the new folder. File –> Open Folder
- Clone your repository. See “Clone the repository” above.
- In VS Code, switch over to the master branch.
- Open the Command Palette… (Ctrl+Shift+P)
- Git: Create Branch
- Name the new branch.
- You will now be pointing to the new branch.
- Select the Publish option to deploy the new branch to GitHub
- Copy over changes.
- There are many methods to accomplish this. I prefer to copy my changed objects in Files Explorer.
- You could also use tools like Cherry Pick.
- You can also merge all the changes and revert the changes you do not want.
- Push hotfix branch.
- After you have made all your changes, you will check in your changes with Stage / Commit / Push commands. See “Committing to the develop branch” above.
- Test your code.
- Pull hotfix branch to master.
- The final step is to get your hotfix code into the remote master branch. We will use the Pull command. I prefer to do this step on the GitHub website.
- Select the Hotfix branch.
- Click New Pull Request.
- Add a meaningful title and comments.
- Merge Pull Request (This will give management an opportunity to review all Pull Requests. Once they are approved, a Merge Pull Request will merge the code into the master branch.)
a. You can stage files by selecting the files you wish to stage. Right click Stage Changes.
b. Staging allows you to gather the files that you wish to commit.
a. Add a message. Click the ellipsis and select Commit Staged.
b. The commit will save your changes to your local repository. This can be a little confusing. You can no longer see the updated files, but they have NOT been pushed to the remote develop branch yet.
a. After the files have been committed to your local repository you will need to push them to the remote repository (aka GitHub server).
Your code changes are now on the remote develop branch. If you browse to your GitHub server, you will see a message next to each item for when it was last committed, along with the message you added during the commit. Any other developer who runs a pull command will download your changes to their local environment.
Deploying to Production
We now want to deploy our code changes to production (our remote master repository). Most of the time we will not want to deploy all the code in the develop branch. We will create a new local branch from the master branch and copy over all the code changes we wish to deploy. We will then pull those changes to the remote master branch.
Questions?
Thanks for reading. We hope you found this blog post to be useful. Do let us know if you have any questions or topic ideas related to BI, analytics, the cloud, machine learning, SQL Server, Star Wars, or anything else of the like that you’d like us to write about. Simply leave us a comment below, and we’ll see what we can do!
Keep Your Business Intelligence Knowledge Sharp by Subscribing to our Email List
Get fresh Key2 content around Business Intelligence, Data Warehousing, Analytics, and more delivered right to your inbox!