In team projects, optimizing the GitHub workflow is crucial. Through better branch management and collaboration processes, you can reduce code conflicts, increase development efficiency, and ensure the stability and maintainability of the project. This article will explore how to effectively utilize these methods to enhance the quality of team collaboration.

Source video for this study note:

GitHub Workflow — Bilibili

Terminology

  • Git: A widely used version control tool.
  • GitHub: A platform that supports using Git to modify and store files.
  • Remote: A remote repository, typically on platforms like GitHub, Gitlab, or Gitee.
  • Local: In this context, it refers to a local repository that is managed using the Git version control tool.
  • Disk: Refers to the state of opening source files in an editor from the disk.
  • main (master): The default name of the Git main branch. Around 2021, the default name for the main branch was changed from master to main.
  • Pull Request: In team projects, a request to merge code into the main branch or other branches.
  • Squash and Merge: The operation of compressing multiple commits into a single commit before merging it into the target branch.

Clone

Clone the remote repository to your local machine:

git clone https://github.com/example/example.git

0-git-clone

Modification

Create a new branch:

git checkout -b my-feature

1-git-checkout.png

Check the differences between the currently modified files and the files saved in Git:

git diff

2-git-diff.png

Stage the modified files (add files to the staging area):

git add <changed_file>

3-git-add.png

Commit the modifications to Git:

git commit

4-git-commit.png

Push

Push the modified code to GitHub:

git push origin my-feature

5-git-push-origin.png

If there are new commits in the main branch on GitHub, you need to check whether the my-feature branch can still be pushed.

First, switch back to the main branch:

git checkout main

6-git-checkout.png

Sync the latest main branch from GitHub to the local Git repository:

git pull origin main

7-git-pull.png

Switch back to the my-feature branch:

git checkout my-feature

8-git-checkout.png

Sync the changes from the main branch:

This step aims to rebase the current branch on top of the latest main branch. Specifically, Git will temporarily move all commits after the common ancestor between the current branch and the main branch, then "move" the current branch to the latest state of the main branch, and finally apply the moved commits on top of the new main branch.

The result is that the commit history of the current branch will appear as if it is based on the latest main branch, making the code history clearer. However, be aware that conflicts may arise during the rebase process, and these conflicts must be resolved manually to complete the rebase.

git rebase main

9-git-rebase.png

Push the modified code to the remote repository again:

Since we performed a git rebase in the previous step, a force push is required.

git push -f origin my-feature

10-git-pull.png

Merge

Send a Pull Request:

11-pull-request.png

After the author reviews the code, use Squash and merge to merge the branches:

Since the commit history of the feature branch is usually complex, using Squash and merge can combine all changes into a single commit, avoiding merging other complex commit histories into the main branch.

12-squash-and-merge.png

Delete Unnecessary Branches

It's easy to delete unnecessary branches on GitHub, but we also need to delete unnecessary branches locally.

Switch to the main branch:

git checkout main

13-git-checkout.png

Delete the my-feature branch:

git branch -D my-feature

14-git-branch-delete.png

Finally, sync with the remote repository again to ensure that the local Git repository is consistent with GitHub:

git pull origin main

15-git-pull.png