Git best practices

Git

This is a guide for programming with version control.

Write a feature

Create a local feature branch based off main.

git checkout main
git pull
git checkout -b <branch-name>

Rebase frequently to incorporate upstream changes.

git fetch origin
git rebase origin/main

Resolve conflicts. When feature is complete and tests pass, stage the changes.

git add --all

When you’ve staged the changes, commit them.

git status
git commit --verbose

Write a good commit message. Example format:

Present-tense summary under 50 characters
  • More information about commit (under 72 characters).

  • More information about commit (under 72 characters).

    http://project.management-system.com/ticket/123

If you’ve created more than one commit, use git rebase interactively to squash them into cohesive commits with good messages:

git rebase -i origin/main

Share your branch.

git push origin <branch-name>

Ask for a code review in the project’s chat room.

Review code

A team member other than the author reviews the pull request. They follow Code Review guidelines to avoid miscommunication.

They make comments and ask questions directly on lines of code in the GitHub web interface or in the project’s chat room.

For changes which they can make themselves, they check out the branch.

git checkout <branch-name>
./bin/setup
git diff staging/main..HEAD

They make small changes right in the branch, test the feature on their machine, run tests, commit, and push.

When satisfied, they comment on the pull request Ready to merge.

Merge a branch

Rebase interactively. Squash commits like "Fix whitespace" into one or a small number of valuable commit(s). Edit commit messages to reveal intent. Run tests.

git fetch origin
git rebase -i origin/main

Force push your branch. This allows GitHub to automatically close your pull request and mark it as merged when your commit(s) are pushed to main. It also makes it possible to find the pull request that brought in your changes.

git push --force-with-lease origin <branch-name>

View a list of new commits. View changed files. Merge branch into main.

git log origin/main..<branch-name>
git diff --stat origin/main
git checkout main
git merge <branch-name> --ff-only
git push

Delete your remote feature branch.

git push origin --delete <branch-name>

Delete your local feature branch.

git branch --delete <branch-name>

TODO image::compendium:gitlab/workflow_tut_step3.png[img4, 1100,title="merge request"]

img5
Figure 1. merge request options