Getting Started with Git in DevOps: A Technical Introduction
Git has proven itself to be an indispensable tool in software development, as it enables to manage your codebase and track its changes overtime. Interestingly, Git plays an equally important role in DevOps. In this article we will cover the basics of version control using Git , which will give us a solid foundation to tackle more complex subjects later on.
Let’s start with defining the DevOps culture and where Git fits into the picture. DevOps is a set of practices designed to optimize the development and deployment of software and, indeed, reduce its time to market. This culture relies heavily on collaboration and continuous improvement. Git, being a powerful version control system that allows you to track the changes you make to your codebase and collaborate with other developers, makes perfect sense to be at the very core of the DevOps culture.
The diagram below illustrates a simplified Git workflow, describing the journey of code changes from local development to a remote repository. The steps involved in this workflow will be explained in the next sections.
Git Essentials: A basic local version control workflow
Let’s dive into the basics. A repository is a digital storage for all your code and its history. By creating a new repository, developers have a new space where they can store and manage their source code. It’s as simple as running the git init
command in the terminal in your working directory.
When changes are made to your codebase, you’ll have to save a snapshot of it. This is done by using the git add
command to stage these changes, followed by the git commit
command to save a new version of your code. You can associate each commit with a message that describes the changes you made. Always make sure you use meaningful messages for your commits, so that other people who might take a look at your code can easily understand the various changes. The below code snippet describes the commands used to execute a full local version control workflow.
# Start a git repository for and existing project
cd /path/to/your/existing/project
git init
# Cloning an existing repository
git clone <repository_url>
# Check the status of the repository
git status
# Add files to the repository
git add <file_name>
git add . # to add all files in the current working directory
# Commit changes
git commit -m "commit message"
Collaboration: Syncing and Remote repository
Collaboration is also a key feature of Git. Git allows you to connect your local repository to a remote repository. By doing so, you can push your changes to the remote repository and collaborate with other developers on your project, but also pull changes made by others, which can be merged into your local repository. The code below explains how you can interact with the remote repository, push and pull changes from it.
# Get remote connections to other repositories
git remote
git remote -v
# Push a specified branch to a remote repository
git push <remote> <branch>
git push <remote> -- all # pushing all local branches
# Pull the specified remote's copy of the current branch
git pull <remote>
Collaboration: Git branches
Git also allows you to create branches, which are separate versions of your code. The main idea behind branches is that it enables you to work on different features, ensuring that the main branch remains stable. Once you’re happy with the changes, you can merge them back into the main version using the command git merge
.
The diagram above portrays a fundamental Git branching scenario, illustrating the creation, development, and integration of a feature within a project. Upon completion, the features can be integrated into the main project . The commands below can be used to execute the workflow on the diagram.
# Create a branch
git branch <new_branch>
git checkout -b <new_branch>
# List all branches in your repository
git branch
git branch --list
# Checking out into a branch
git checkout <branch_name>
# Merge your feature_branch changes into main
git checkout main
git merge feature_branch
# Delete branch from local and remote repository
git branch -d <branch_to_delete>
git push <remote> -d <branch_to_delete>
Conclusion
In summary, Git is a powerful tool that allows you to easily make changes to the codebase, collaborate with others, and even revert to previous versions if needed. In DevOps , Git plays an important role in automating the deployment of code to different environments. Mastering Git is essential to streamline your workflow, collaborate more effectively and deploy code changes faster and more frequently.