-
Notifications
You must be signed in to change notification settings - Fork 0
Commiting to Git
Chloe edited this page May 9, 2024
·
16 revisions
+---------+ +-------------------+
| Main |<------+ Hotfix Branch |
+----^----+ +--------^----------+
| |
| |
| +-----+------+
+-------------> | Feature |
| Branch |
+------------+
-
Main Branch:As the name says, the main branch, which is to be stable, tested version →main/ -
Feature Branch:For ongoing development, connected sets of changes, merged back into main once done →feature/ -
Hotfix Branch:Used for critical bug fixes on main, undergo review before merging back →hotfix/
Creating Branches
- Always create a new branch for each new feature or bug fix, so the code stays organized and isolated from the main codebase.
Life Cycle of Branches
-
Feature Branches: Merge feature branches back into the main branch once the feature is complete, tested, and reviewed.
- Ideally we want to avoid long lived branches, which can accumulate a significant number of changes, making merging difficult to do.
- Hotfix Branches: These branches should be merged back as soon as the fix is complete and verified.
Deleting Branches
- After a branch has been successfully merged into main, it should be deleted to keep the repository clean and manageable.
Regular Maintenance
- Every now and then to go back and check on the unused braches and review them to see if they should be deleted as this can make the repository a lot cleaner.
- Try to make smaller, but more frequent commits throughout the day, as it's easier to fix and review
- Add a descriptive message of
what kind of changes were made, andwhy they were neccessary
- Use prefixes in commit messages:
-
When ADDING a new functionADD: < what has been added > -
When FIXING an errorFIX: Bug < [what bug] > [description] -
When REMOVING code, filesDEL: < what has been removed > -
When UPDATING somethingUPD: < what has been updated >
- Committing not fully-functional additions is possible if
all tests passand nothing breaks because of it. - Every commit should be logically connected to one another, if it contains the word
and, split it into multiple commits - Use the
git add -pcommand to prepare and verify the contents of the commit before finalizing it andgit diff --cachedto review - Avoid force push unless really neccessary
-
Some tips about writing helpful commit messages:- Separate the title from the body with a blank line
- Subject line <=
50 characters+ imperative mood - Use the body to explain what and why vs. how.
- Body <=
72 characters+whyandhow
Good Commit Example:
Why it’s good:
- Clear Prefix: Uses a prefix "UPD" to indicate that it's an update, which is part of the standard convention.
- Descriptive: Clearly describes what was done (updating a YAML file) and why (to include an update option).
- Scope Limited: Focuses on a single change which makes it easier to understand the purpose and impact of the commit.
Bad Commit Example:
Why they’re bad:
- Non-descriptive Messages: The commit messages are vague, do not convey the purpose of the changes.
- Inconsistent Naming: They do not follow a clear or agreed-upon naming convention which makes them harder to track in the version history. (granted this did not exist back then, but it is the best example I have now, sorry Julius)
- Should Have Been Squashed: We have opted to squash merge to maintain a clean history, however this was merged as singular commits, making it more difficult to maintain a clean history.
-
Merge conflicts can arise when multiple branches have edited the same part of the same file. Here's how to resolve them:
-
Use
git mergetool: This will opens a GUI that you can use to resolve conflicts manually. Configure your preferred tool in your Git configuration withgit config --global merge.tool <toolname>. -
Manual Resolution: Open the conflict and look for the lines marked with conflict markers (
<<<<<<<,=======,>>>>>>>). Edit the file to fix the conflicts, then clickgit addthe file to mark it as resolved. - Command Line: Alternatively, you can solve the conflict through the command line
-
Use
- Know when to rebase and when to merge to maintain a clean and efficient project history:
-
Use
git rebasefor Feature Branches: Before creating a pull request (PR), rebase your feature branch onto the latest commit of theMainbranch. This practice helps create a nicer history, which simplifies both navigation and troubleshooting in the project history. Also when updates fromMainbranch are added into the feature branch you can spare merge conflicts. -
Use Pull Requests for Integrating Completed Features and Hotfixes: Once the feature or hotfix branch is fully tested and approved, squash merge it back into the
Mainbranch using a pull request.
-
Use
-
Cleaning Up Commits: You can use
git rebase -ifor interactive rebasing, which allows you to modify commits: rewrite, delete, and squash commits before they are merged into main.
-
Rebasing Best Practices: Always pull and rebase (
git pull --rebase) your feature branches before pushing your changes to remote repositories to minimize conflicts and ensure your branch is up to date with theMainbranch.This step should always be completed before opening a new pull request.
-
Managing Changes: If you need to quickly switch between branches but you don't want to commit your current work:
-
Stash Your Changes:
git stashsto save your current state. You can always go back to your previous branch without committing half-done work. -
Apply Stashed Changes: Use
git stash popto apply the saved changes after switching back or to another branch.
-
Stash Your Changes:

