Git: Commit Code to Repo

Git: Commit Code to Repo
Created By: Chase Woodard

Committing New Code to Existing Repo

Everything you need to know

Table of Contents:

  • Why Version Control Matters
  • The Basic Git Workflow
  • Common Git Commands for Daily Use
  • Handling Merge Conflicts

Understanding Git Workflows: A Beginner's Guide to Code Commits

When you first embark on your network automation journey, one of the most fundamental skills you'll need to master is committing code to a repository. Whether you're using GitHub, Azure DevOps, GitLab, Bitbucket, or any other version control platform, the core concepts remain the same. This skill sits at the very heart of automation practices, enabling collaboration, version tracking, and safe experimentation.

Why Version Control Matters in Network Automation

Before diving into the mechanics, let's understand why this is so important. Network automation involves creating scripts and tools that configure, manage, and monitor your network infrastructure. Without version control:

You risk losing work if your local copy is damaged (this is never fun).

You can't easily collaborate with teammates.

You have no history of changes to roll back to if something breaks.

You lack a structured way to review changes before they're implemented.

The Basic Git Workflow (Click to Drop Down)

Git is the underlying version control system used by most modern code repositories. Here's the typical workflow you'll follow:

1. Clone the Repository

Start by creating a local copy of the repository:

git clone https://repository-url.git

This downloads the entire repository history and code to your local machine.

2. Create a Feature Branch

Never work directly on the main branch (often called main or master). Instead, create a feature branch:

git checkout -b my-feature-branch

This creates a separate line of development where you can make changes without affecting the main codebase.

3. Make Your Changes

Now you can modify files, create new ones, or delete existing ones. These changes are initially only on your local machine.

4. Stage Your Changes

Once you're ready to commit, you need to stage the files you want to include:

git add filename.py # Stage a specific file
git add directory/ # Stage an entire directory
git add. # Stage all changes

5. Commit Your Changes

Commit the staged changes with a descriptive message:

git commit -m "Add OSPF configuration automation script"

Your commit message should clearly explain what changes you made and why.

6. Push Your Changes

Upload your commits to the remote repository:

git push origin my-feature-branch

7. Create a Pull Request/Merge Request

Go to your repository platform (GitHub, Azure DevOps, etc.) and create a pull request (PR) or merge request (MR). This is a formal proposal to merge your changes (feature branch) into the main branch.

8. Code Review and Merge

Your teammates or yourself, can now review your code, suggest changes, and eventually approve the PR. Once approved, your code can be merged into the main branch.

Best Practices for Network Automation Repositories (Click to Drop Down)

  • Commit Frequently: Make small, focused commits rather than large, sweeping changes.
  • Write Clear Commit Messages: Explain what you changed and why, not just what you did.
  • Use Meaningful Branch Names: Name branches after the feature or fix they contain (e.g., add-bgp-config-template).
  • Test Before Committing: Ensure your automation scripts work as expected before committing.
  • Document Your Code: Include comments and documentation so others can understand your automation scripts.
  • Use .gitignore: Exclude sensitive files (like credentials) and unnecessary files (like cache files) from your repository.
  • Implement CI/CD: Set up continuous integration to automatically test your network automation code.

Common Git Commands for Daily Use (Click to Drop Down)

git status # Check the status of your working directory
git pull # Get the latest changes from the remote repository
git branch # List all local branches
git checkout branch-name # Switch to another branch
git diff # See changes you've made but not yet staged
git log # View commit history
git reset --hard HEAD # Discard all local changes (be careful!)

Handling Merge Conflicts (Click to Drop Down)

Occasionally, you'll encounter merge conflicts when your changes overlap with someone else's. Don't panic! Git will mark the conflicting sections in your files, and you'll need to manually resolve them by choosing which changes to keep. After resolving conflicts, stage the files and commit the resolution:

git add resolved-file.py

git commit -m "Resolved merge conflicts in OSPF configuration"

Tips to Prevent Merge Conflicts:

  • Work on Feature Branches: Always create and work on separate branches for each feature or bug fix instead of working directly on the main branch.
  • Pull and Rebase Frequently: Regularly pull the latest changes from the main branch into your feature branch (using git pull origin main --rebase or similar). 
  • Make Small, Frequent Commits: Break your work into small, manageable commits and submit pull requests often.
  • Review and Merge Pull Requests Quickly: Don’t let pull requests sit for too long. The

Conclusion

Understanding how to work with Git repositories is a foundational skill for network automation. It might seem complex at first, but with practice, these workflows will become second nature.

The time invested in learning proper version control practices will pay dividends as your automation projects grow in complexity and as you collaborate with others.

Remember, whether you're using GitHub, Azure DevOps, or any other platform, the underlying Git concepts remain the same. Master these basics, and you'll be well on your way to building a solid network automation practice.

Happy automating!

Read more