DEV Community

Lori "Lei" Boyd
Lori "Lei" Boyd

Posted on • Updated on

An Example of a Git Workflow

Git is a distributed version control system for tracking changes in source code during software development. It’s a great tool to use when you're collaborating with other developers as well. But sometimes creating a workflow for your development process can be a little tricky.

Alt Text

First things first, you’ll need to set up a repository. Here’s an article from GitHub on that. I would then add the skeleton of my project and commit the changes with git commit -m "<commit message>"

After you have your repository set up, create a Development branch with git checkout. git checkout -b development

This creates a new local branch out of your master branch. This development branch is what you’ll be merging into until your project is in a working condition. Next, type in git push origin development to push your local development repo to a remote repo with the same name. This way your development repo can also be accessed by your collaborators.

If you’re collaborating on a project that isn’t yours, you’ll need to clone and fetch the repository.

git clone <repo link>
git fetch

Git fetch allows you to access any other existing branches.

Now whenever you start working on a new feature, you’ll create a new branch for each one. If you’re not in the development branch switch to it now with git checkout. git checkout development Then run git checkout -b <feature branch name> This creates a branch from your development branch. Your feature branch is only on your local machine until you push your commits into the remote repository. Commit often, usually after each deliverable. Once your feature is tested and working you can push to the remote repository with git push. git push origin <feature branch name>

Uh-oh! You pushed the wrong button and your program is broken!

Alt Text

No worries! If you’ve been committing often you’ll be able to reset to your last commit! Make sure you don’t have any modified files in your working tree that you wish to keep. You can check that with git status It'll look something like this.

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   newfile.rb

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    hhdjhd.txt

To reset to your last commit:

  1. go to your repo on GitHub and click commits
    Alt Text

  2. then copy the commit to your clipboard
    Alt Text

  3. in your terminal checkout the commit, and create a new branch

git checkout <commit string>
git checkout -b <branch name>

Alt Text

You may run into merge conflicts, and great communication with your team can remedy that. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Keeping track of who works on what is crucial, so I like to use Trello to break down tasks. In case a conflict does happen though, git will notify you and you’ll be able to delete whatever is causing the conflict.

When your development branch is a complete and working product, it’s time to merge to the master branch.

Alt Text

Do so by switching to master git checkout master and merge from your development branch git merge development Deploy!

All in all, keep a development branch and make small feature branches as you go. Commit often, and keep track of who is doing what. This is just a workflow that I found easiest to use, feel free to tweak and experiment with other workflows and let me know your results!

Top comments (0)