The Importance of Git Workflow and Practical Troubleshooting in Next.js Development

Introduction

Next.js is a powerful framework that offers a fast development experience, but a solid understanding of Git is crucial for deployment and collaborative work. This article, based on my real experience with the Nurohive blog project, explains the importance of Git workflow in Next.js development and offers solutions for common Git problems that often arise during deployment.

Development Workflow and Branch Strategy

A healthy Git workflow is essential for keeping a project clean and ensuring stable deployments.

Keeping the main branch clean

The main branch should always hold the most stable code, ready for production deployment. Avoid working directly on this branch; always create a new branch from main to start new development.

Feature-based Branch Strategy

I follow a simple branch strategy:

  • feat/ branch: Used for adding new features.
    • Example: feat/add-hamburger-menu (implementing a hamburger menu)
  • fix/ branch: Used for fixing bugs.
    • Example: fix/hide-whensday-service (hiding an unfinished service)

Once the work on these branches is complete, a pull request is created to main, and after a review, it is merged.

Pull Requests (PRs) and Merging

A pull request is a "proposal" to integrate your changes into the main branch. Performing a code review before merging a PR helps prevent unintended bugs and issues.

Practical Git Troubleshooting

Here are some common Git issues encountered during Next.js development and their solutions.

Solving [rejected] errors

When you run git push, you may encounter a [rejected] error. This happens because the history of your local branch is older than the history of the remote repository.

Solution: Use the git pull command to fetch the latest changes from the remote and integrate the histories.

git pull origin <branch_name>
git push origin <branch_name>

If merge conflicts occur, resolve them manually before pushing again.

Do not commit .env.local

The .env.local file, which contains sensitive information like OPENAI_API_KEY, should never be committed to Git. Exposing this information on GitHub creates a risk of misuse.

Solution: Add the following line to your .gitignore file, located in the project's root directory.

# Environment variables
.env.local

This tells Git to ignore .env.local, keeping your sensitive information safe.

Conclusion

Developing with Next.js is fast and exciting, but establishing a proper Git workflow is crucial for project success. I hope this development log helps make your own development experience safer and smoother.