DEV Community

Rishi Kumar
Rishi Kumar

Posted on • Updated on

Guidelines for Pushing Code to a GitHub Repository

Pushing code to a GitHub repository is a critical step in the software development process. Following best practices ensures a clean, organized, and efficient project. Here are key guidelines to follow when pushing code to your GitHub repository:

1. Always Use .gitignore

The .gitignore file specifies which files and directories to ignore in a project. This helps keep unnecessary files out of your repository, ensuring only relevant code is pushed. Common entries in a .gitignore file include:

  • Operating system files (e.g., .DS_Store, Thumbs.db)
  • Dependency directories (e.g., node_modules/)
  • Log files (e.g., *.log)
  • Environment variables (e.g., .env)

Example:

# Node.js
node_modules/
npm-debug.log
yarn-error.log

# Logs
logs/
*.log

# OS generated files
.DS_Store
Thumbs.db

# Environment variables
.env
Enter fullscreen mode Exit fullscreen mode

2. Never Push node_modules

The node_modules directory contains all the dependencies required for your project. Pushing this directory to your repository is unnecessary because these dependencies can be installed via npm install or yarn install. Pushing the entire node_modules directory is redundant and increases repository size.

Example .gitignore entry:

node_modules/
Enter fullscreen mode Exit fullscreen mode

3. Push Root Level Folder Structure, Not Entire Folder

When organizing your repository, push only the essential root-level folders and files that are necessary for the project. Avoid pushing large nested directories that are not essential for understanding or building the project.

Example Root Structure:

git-repo/
├── .gitignore
├── README.md
├── package.json
├── src/
│   ├── index.js
│   ├── app/
│   └── config/
└── tests/
    └── test.js
Enter fullscreen mode Exit fullscreen mode

Ensure your repository includes:

  • A README.md file with project details and setup instructions.
  • Configuration files (package.json, .gitignore, etc.).
  • Source code in an organized directory structure (src/, app/, config/).
  • Test files in a separate directory (tests/).

4. Use Proper Git Commit Style

Effective commit messages help maintain a clear project history. Follow these guidelines for writing proper commit messages:

  • Header: A concise summary of the changes (50 characters max).
  • Body: Detailed explanation of the changes (optional, 72 characters per line).
  • Footer: References to issues or pull requests (optional).

Commit Message Structure:

<type>(<scope>): <description>

<optional body>

<optional footer>
Enter fullscreen mode Exit fullscreen mode

Types:

  • feat: A new feature.
  • fix: A bug fix.
  • docs: Documentation changes.
  • style: Code style changes (formatting, missing semi-colons, etc.).
  • refactor: Code changes that neither fix a bug nor add a feature.
  • perf: Performance improvements.
  • test: Adding or updating tests.
  • build: Changes to build scripts or dependencies.
  • ci: Changes to CI configuration.
  • chore: Other changes that don’t modify src or test files.
  • revert: Reverts a previous commit.

Example Commit Messages:

feat: add user authentication

This commit adds JWT-based user authentication to the application.

- Added login and signup endpoints
- Implemented token generation and validation
- Added tests for authentication endpoints

Resolves: #45
Enter fullscreen mode Exit fullscreen mode
fix: handle empty message in request body

The error occurred due to missing validation for empty messages.
Enter fullscreen mode Exit fullscreen mode

Following these guidelines will help maintain a clean, efficient, and professional repository on GitHub. By using a .gitignore file, avoiding the push of unnecessary directories like node_modules, maintaining a clear project structure, and adhering to proper commit message styles, you ensure that your codebase remains organized and accessible to all contributors.


References

Top comments (2)

Collapse
 
stark-akshay profile image
Akshay Manoj

Very useful!! this is great information. Thank you for writing it and sharing them.

Collapse
 
dinesh_41916 profile image
Kuddana Dinesh

Very Useful information
Thanks for sharing