Git for developers: a complete workflow from init to deploy
Git is the beating heart of modern software development. Whether you manage a solo project or work in a team, a solid understanding of Git is essential for efficient version control and collaboration. In this article, we walk you through the complete lifecycle of a Git project: from initializing a repository to deploying your code. We focus on practical workflows using both standard Git commands and handy tools such as gh (GitHub CLI) and glab (GitLab CLI).
Initializing Git: setting up the basics
Every Git project starts with creating a repository. This can be done locally or via a platform such as GitHub or GitLab:
- Create a local repository:
git init- Creates an empty Git repository in the current directory - Clone an existing repository:
git clone https://github.com/gebruiker/repository.git
After initializing, it is important to configure your identity in Git:
git config --global user.name "Your Name"
git config --global user.email "you@email.com"
Working with branches: developing in an organized way
Branches are crucial in Git workflows. They allow you to work on features in parallel without disrupting the main codebase.
Basic branch commands:
git branch- Shows all local branchesgit branch feature-x- Creates a new branchgit switch feature-x- Switches to the feature-x branchgit switch -c hotfix- Creates and switches to a new branch in one command
Committing code changes
The Git workflow process consists of three main steps:
git add bestand.txt- Adds changes to the staging areagit commit -m "Beschrijving van wijzigingen"- Commits the changesgit push origin branchnaam- Publishes changes to the remote repository
Tip: Use git status regularly to see an overview of the current status.
Collaborating with Pull/Merge Requests
For team projects, Pull Requests (GitHub) or Merge Requests (GitLab) are essential for code reviews.
GitHub CLI (gh) for Pull Requests:
gh pr create --title "Nieuwe feature" --body "Beschrijving van wijzigingen"
GitLab CLI (glab) for Merge Requests:
glab mr create --title "Nieuwe feature" --description "Beschrijving van wijzigingen"
Resolving merge conflicts
Conflicts are inevitable when working in a team. Git provides tools to resolve them:
git diff- Shows differences between versionsgit mergetool- Starts a visual merge tool- After resolving the conflict:
git add conflicterend_bestandandgit commit
Workflows with GitHub Actions and GitLab CI/CD
Modern Git hosting services offer powerful CI/CD tools:
GitHub Actions:
gh workflow view - Shows workflows and lets you inspect the latest runs
GitLab CI/CD:
Define a .gitlab-ci.yml file in the root of your project for automated pipelines.
Deploying from Git
Git integrates seamlessly with modern deployment processes:
- Git hooks: Scripts that run automatically on certain Git actions
- CI/CD pipelines: Automatic deployment after successful tests
- Direct deployment: Some hosting solutions allow deployment by simply pushing to a specific branch
Advanced Git techniques
For advanced users, Git offers powerful tools:
git rebase- Rewrites commit history for a cleaner timelinegit cherry-pick- Selective application of specific commitsgit bisect- Binary search for the commit that introduced a bug
Conclusion and best practices
Mastering Git is a must for every developer. Remember these core principles:
- Commit often, in small logical units
- Use descriptive commit messages
- Work in feature branches
- Review code via Pull/Merge Requests
- Automate testing and deployment
With this Git workflow, you can work efficiently on projects of any size, with confidence in your version control and collaboration process.