MENU

[AI from Scratch] Episode 238: Version Control with Git — Basics of Code Management and Team Collaboration

TOC

Recap and Today’s Theme

Hello! In the previous episode, we explored environment setup with Docker, learning how to ensure reproducibility using container technology. By using Docker, we minimized differences in environments and enabled seamless application deployment.

Today, we will focus on version control with Git, a crucial tool for code management and team collaboration in development projects. Git is indispensable for managing code changes and facilitating teamwork. This article covers the basic usage of Git and best practices for utilizing it effectively in team development.

What Is Git?

Git is a distributed version control system that records and manages changes in source code. By using Git, you can track code changes, revert to specific versions, and integrate changes from multiple developers easily, even when they work simultaneously.

Key Features of Git

  1. History Management: All code changes are recorded, allowing you to revert to previous versions or check past changes.
  2. Branching: Git enables creating branches to work on multiple tasks concurrently, allowing separate development for each feature or fix.
  3. Integration with Remote Repositories: Git can connect with services like GitHub or GitLab, enabling team-wide code sharing and management.

Basic Git Operations

Let’s explore the basic operations in Git. Understanding these commands will help you master the basics of Git usage.

1. Installing and Setting Up Git

First, install Git from the official site:

After installation, configure Git with the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
  • user.name: Sets the username displayed when making commits (recording changes).
  • user.email: Associates an email address with your commits.

2. Creating a Git Repository

To manage code with Git, first create a repository. Use the following command to turn a new project directory into a Git repository:

git init

This command sets up the directory for Git management, tracking all changes made.

3. Adding Files and Committing Changes

To add files to the repository and commit changes:

# Adding a file
git add filename.py

# Committing the changes
git commit -m "Initial commit"
  • git add: Stages files for tracking. You can add specific files or use git add . to add all changes.
  • git commit -m “message”: Commits the changes with a descriptive message.

4. Connecting with Remote Repositories

By linking your local repository to a remote one (e.g., GitHub or GitLab), you can manage your code online. Follow these steps to connect:

# Adding a remote repository
git remote add origin https://github.com/username/repository.git

# Pushing commits to the remote repository
git push -u origin main
  • git remote add origin: Links a remote repository (e.g., on GitHub) to the local one.
  • git push: Pushes local commits to the remote repository. The -u origin main flag sets the main branch as the default push target.

5. Working with Branches

Branches allow development of new features or bug fixes without affecting the main codebase (usually the main or master branch). Create and switch branches as follows:

# Creating a new branch
git checkout -b new-feature

# Switching to an existing branch
git checkout main
  • git checkout -b: Creates and switches to a new branch.
  • git checkout: Switches to an existing branch.

Once work on a branch is complete, merge it back into the main branch:

# Switching to the main branch
git checkout main

# Merging the new-feature branch into the main branch
git merge new-feature

6. Syncing Local and Remote Repositories

When other developers make changes to the remote repository, use the following command to sync your local repository:

git pull origin main
  • git pull: Fetches and merges changes from the remote repository into the local one.

Best Practices for Using Git in Team Development

Git is highly effective in team settings. Here are some best practices for efficient code management in team development:

1. Using Branching Strategies

When working in a team, adopting a branching strategy is essential. Common strategies include:

  • Feature Branch: Create a new branch for each feature, develop on it, and merge it back into the main branch once completed.
  • Develop Branch: Maintain a separate development branch where new features are integrated and tested before merging into the main branch.
  • Hotfix Branch: Use a hotfix branch for urgent bug fixes, merge it back into the main branch after testing.

2. Writing Effective Commit Messages

Clear commit messages help others understand changes quickly. Use a consistent format like:

  • Change Type: Indicate the change type (e.g., fix: bug fix or feat: add new feature).
  • Description: Provide a brief and clear description of the change (e.g., fix: improve error handling in API).

3. Using Pull Requests (PRs)

On platforms like GitHub or GitLab, PRs enable developers to request reviews from others. PRs are crucial for maintaining code quality and preventing bugs.

  • Request Reviews: After completing changes, ask team members for reviews to ensure code quality.
  • Establish Review Rules: For example, require at least two approvals or passing certain tests before merging.

4. Implementing CI/CD (Continuous Integration/Continuous Delivery)

By integrating Git with CI/CD tools (e.g., Jenkins, GitHub Actions, GitLab CI), you can automate testing and building processes whenever code changes are pushed to the repository. This helps detect errors early and automates the deployment process.

Example of Team Development Using Git

Here is a simplified workflow for team development using Git:

  1. Clone the Repository: Clone the team repository.
   git clone https://github.com/username/repository.git
  1. Create a New Branch: Create a branch for a new feature or bug fix.
   git checkout -b feature/new-function
  1. Make Changes and Commit: Apply changes and commit them locally.
   git add .
   git commit -m "feat: added new function"
  1. Push Changes to the Remote Repository: Push the changes to the remote repository.
   git push origin feature/new-function
  1. Create a Pull Request (PR): On GitHub, create a PR and request a review.
  2. Merge into the Main Branch: Once approved, merge the changes into the main branch.

Summary

This episode covered version control with Git, explaining the basics of code management and team development. Git is an essential tool in software development, and using it properly significantly boosts development efficiency. Practice using Git in your projects to enhance collaboration and code management!

Next Episode Preview

Next time, we will dive into Testing and Debugging Essentials, discussing methods to ensure code quality. Learn efficient testing and debugging techniques to improve the quality of your code!


Notes

  • Version Control: A system for recording and tracking changes to software source code.
  • Pull Request: A request for other developers to review code changes before merging them, commonly used on platforms like GitHub and GitLab.
Let's share this post !

Author of this article

株式会社PROMPTは生成AIに関する様々な情報を発信しています。
記事にしてほしいテーマや調べてほしいテーマがあればお問合せフォームからご連絡ください。
---
PROMPT Inc. provides a variety of information related to generative AI.
If there is a topic you would like us to write an article about or research, please contact us using the inquiry form.

Comments

To comment

TOC