Comprehensive Guide to Git Workflow for Branching and Pull Requests

David Doherty
3 min readNov 19, 2023

--

Introduction:

Version control is a crucial aspect of collaborative software development, and Git has become the industry standard for managing source code changes efficiently. This guide aims to walk you through a comprehensive Git workflow, focusing on branching and pull requests. By following these steps, you can contribute to projects seamlessly and avoid common pitfalls.

Branching is crucial!

Best practices for team projects:

Collaborating on GitHub projects with multiple contributors requires a strategic approach to ensure a smooth and efficient development process. This guide outlines a set of best practices aimed at fostering collaboration, maintaining code quality, and streamlining workflows for teams working on GitHub projects.

1. Use Branches Effectively:

  • Create feature branches to isolate changes.
  • Employ a branching strategy (e.g., Gitflow) for organized development.

2. Clear and Concise Commit Messages:

  • Write descriptive and concise commit messages.
  • Clearly communicate the purpose of each commit.

3. Frequent Pulls and Updates:

  • Regularly pull the latest changes to avoid conflicts.
  • Keep your local repository up-to-date.

4. Code Reviews:

  • Conduct regular code reviews for quality control.
  • Provide constructive feedback during code reviews.

5. Continuous Integration (CI) and Automated Tests:

  • Implement CI to automate testing on pull requests.
  • Ensure all tests pass before merging changes.

6. Use Issues for Tracking:

  • Utilize GitHub Issues for task tracking.
  • Reference issues in commit messages for traceability.

7. Documentation:

  • Keep documentation up-to-date, including README files.
  • Document the purpose and usage of code elements.

8. Follow Coding Standards:

  • Agree on and adhere to coding standards.
  • Use linters and automated tools for consistency.

9. Pull Requests (PRs) Guidelines:

  • Create meaningful PRs with clear descriptions.
  • Reference related issues in PR descriptions.

Setting the Stage:

Creating a New Branch:

To begin, create a new branch using the following command:

git checkout -b my_new_branch

This command not only creates a new branch but also switches to it in one step.

Committing Changes:

Commit your changes with a meaningful message using:

git add .
git commit -m "Your commit message"

You can also use git commit -am "Your commit message" to combine staging and committing in one step.

Pushing Changes:

Push your branch to the remote repository on GitHub:

git push -u origin my_new_branch

Collaboration and Pull Requests:

Pull Request on GitHub:

After committing changes, it’s time to create a pull request (PR) on the GitHub UI. This process initiates a discussion about your proposed changes and allows for code review.

GitHub will notify you of any merge conflicts. If conflicts arise, follow these steps:

Handling Merge Conflicts:

  1. Fetch the latest branches from GitHub:
git fetch

2. Switch to the master branch and pull the latest changes:

git checkout master
git pull origin master

3. Return to your branch and merge changes from master:

git checkout my_new_branch
git merge master

Most Integrated Development Environments (IDEs) will assist in conflict resolution. After resolving conflicts, commit the changes:

git commit -am "Merging master into my_new_branch"

Finally, push your changes:

git push origin my_new_branch

Finalizing the Pull Request:

After pushing the changes, revisit the GitHub UI and retry the pull request. Before merging, it’s advisable to wait for automated tests (if configured) to ensure your changes don’t break the existing codebase.

Conclusion:

Mastering Git workflows enhances collaboration and ensures a smooth development process. By following these steps for branching, committing, and handling pull requests, you contribute to projects with confidence. Git, coupled with a well-defined workflow, empowers developers to build robust and error-free software collaboratively.

--

--

David Doherty

I write about Fintech, it's past & future, leveraging 20+ years of experience in leadership roles at large Fintechs