Git Workflow#

git is a version control system, which basically means it tracks changes to your files. For example, if you make a mistake, or want to start over, you can “undo” your changes. This becomes even more beneficial when working with others — for example, two people who make changes independently can “merge” their changes together easily.

Using git is an essential part of professional development, and is highly recommended for all projects.

Before following this guide, be sure you have a Terminal and Editor as described in our prerequisites.

Step 1: Choose a git host#

Using a centrally managed git host allows multiple people to collaborate together. It also ensures your code is always backed-up. Most likely, your company is already using one.

  • GitHub is the world’s largest git hosting service, offering free plans and paid plans for businesses.

  • Azure DevOps is Microsoft’s git service and project management solution for enterprises.

  • Bitbucket from Atlassian offers integration with Jira.

  • GitLab and srchut are a few independent open-source options.

Step 2: Install git#

Download and install from: https://git-scm.com/downloads

Step 3: Clone a repository#

Once you’ve created a repository (or “repo”) through your git host, you’ll want to “clone” it, which means to download it onto your machine.

We recommend organizing all of your projects in a “Source” folder on your computer, so we’ll reference ~/Source/ in this guide. Replace <project-url> below with the URL of your git repository.

Open up a terminal and enter the commands below to begin.

% cd ~/Source
% git clone <project-url>
% cd ~/Source/mypoject

Step 4: Work in branches#

A branch is a copy of the code which is isolated for your changes. For example, one branch might contain the version of the code deployed in production. Then you can make a second branch to safely work on your changes without disturbing the production code.

Most git repos use main or master as the branch containing production code.

Start by switching to the main branch, pulling (synchronizing) with the latest version, then creating a new branch for your code (which we’ll name mybranch):

% git checkout main
% git pull
% git checkout -b mybranch

Step 5: Edit code#

Now that you’re in a branch, you are ready to work. Edit and test your code following your normal process.

At any time, you can see what branch you are on, and what files have been changed by running git status.

% git status

Next, add (or “stage”) the changes. The dot “.” means to add everything.

% git add .

Now commit them, adding a message about what you changed.

% git commit -m "Added more widgets to the home page."

To share your changes with others, and to back up your changes, push them up to the git host.

% git push

Step 6: Request a review#

If you’re working with other team members, it is highly beneficial to do code reviews of eachother’s changes. Most git hosts offer a feature for this, called either Pull Request or Merge Request.

Using your branch (which we named mybranch), open a Pull Request online.

Step 7: Merge your branch#

Once your branch has been reviewed or approved, you’ll want to merge it in to the target branch, which is usually main (or if you’re targeting a staging site, we recommend staging).

If you opened a Pull Request - then you’ll want to merge it through the web browser using buttons provided by your git host.

If you are working solo, then you can merge the branch manually as so:

% git checkout main
% git merge mybranch
% git push

In some cases, you may experience conflicts. This can happen if two people edit the exact same lines in the exact same file. In that case, git will inform you that there is a CONFLICT in the terminal. Follow the instructions in the terminal and edit the file to fix the conflict.

Optional: set up auto-deploy#

If you’re using git for a website hosted on CodeRed Cloud, you can configure your git host to automatically deploy the changes form the main branch whenever it is updated. See: Automated Deployments.