The Centralized Workflow in Git emulates the workflow of traditional version control systems, where a central repository serves as the single source of truth. In this model, all team members directly pull from and push their changes to one shared repository. This workflow is typically used in smaller teams or environments where simplicity is key.
In the Centralized Workflow, all developers use a central repository on a shared server as the single place for all project files and updates. Unlike distributed workflows, the centralized approach resembles the structure of legacy version control systems, like SVN. In this workflow, each developer clones the central repository to their local environment, makes changes, and pushes updates back to the central repository.
The Centralized Workflow revolves around a single shared repository, often referred to as the main repository. Here’s a basic flow of the process:
This workflow is straightforward, but it can encounter conflicts when multiple developers make changes to the same parts of the code.
To use the Centralized Workflow, you’ll need access to a shared Git repository on a platform like GitHub, GitLab, or an internal server. Here’s how to set it up.
Each developer clones the central repository to their local environment:
git clone https://github.com/org-name/project-name.git
Once cloned, the repository should have a remote named origin
pointing to the central repository:
git remote -v
origin https://github.com/org-name/project-name.git (fetch)
origin https://github.com/org-name/project-name.git (push)
Each developer needs to follow a sequence to ensure consistency and avoid conflicts. Let’s break down each step with code examples and explanations.
Before starting work, each developer should pull the latest updates from the central repository to ensure they have the most recent codebase.
git pull origin main
main
branch into the developer’s local branch.Developers can now work on features or bug fixes. For example, let’s assume a developer adds a new file.
touch new-feature.txt
echo "This is a new feature" > new-feature.txt
After making changes, developers need to commit these changes to their local repository.
git add new-feature.txt
git commit -m "Add new feature file"
git add
: Stages the changes.git commit
: Saves the changes to the local repository with a commit message.
[main a1b2c3d] Add new feature file
1 file changed, 1 insertion(+)
create mode 100644 new-feature.txt
Once committed, the changes are ready to be pushed to the central repository
git push origin main
git push origin main
updates the central repository with local changes on the main
branch.
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/org-name/project-name.git
5f3c2a1..a1b2c3d main -> main
In a centralized model, conflicts can arise when multiple team members edit the same file. Git notifies developers when there’s a conflict during a git pull
or git merge
.
git push origin main
CONFLICT (content): Merge conflict in new-feature.txt
Automatic merge failed; fix conflicts and then commit the result.
3. Resolve Conflict: Open the conflicting file and edit as needed to resolve differences.
4. Mark Conflict as Resolved
git add new-feature.txt
git commit -m "Resolve conflict in new-feature.txt"
git push origin main
Explanation: Conflict resolution requires modifying the conflicting file, staging it, committing the resolved conflict, and pushing the final version to the central repository.
The Centralized Workflow is a simple and effective approach for managing small projects or teams. It follows a familiar model of using a single central repository as the main source for all team members. Although it may lead to conflicts in larger teams, this workflow remains an excellent choice for smaller projects that prioritize simplicity over flexibility. Happy Coding!❤️