Effective Git Workflow: Managing Version Control in Team Environments

hijuliancode - Sep 13 - - Dev Community

Git is a version control system used to manage changes to files, typically in code or design projects.

What is Version Control?

Version control refers to managing changes made to files over time. It allows us to:

  • Track modifications,
  • Revert to previous versions,
  • Collaborate effectively.

Why do we use Git?

Git helps us:

  • Preserve changes,
  • Track modifications across one or multiple files,
  • Save progress in projects, whether code or design.

While Git can be used for personal notes or academic purposes, it's mainly used for team collaboration. Simply put, Git is "a tool that helps me organize with my team and track the work we do."

Remember, Git operates within a directory, so even if you want to track a single file, Git must be initialized in the directory containing that file.


Useful Commands for Team Collaboration

# Initialize a Git repository in a directory
git init

# Update all remote branches
git fetch --all

# List local branches
git branch

# List both local and remote branches
git branch -a

# Add a remote repository
git remote add origin https://gitlab.com/hijuliancode/demo-git-tutorial

# Add a Heroku remote repository
git remote add heroku https://git.heroku.com/demo-heroku-repo.git

# Remove a remote repository
git remote remove heroku

# View all configured remote repositories
git remote -v

# Fetch and merge changes from the master branch of the remote repository
git pull origin master

# Push changes from the local master branch to the remote repository
git push origin master

# Display the state of your working directory and staging area
git status

# Stage a file for commit
git add filename.html

# Stage all changes in the current directory for commit
git add .

# Remove a file from the staging area
git reset filename.html

# Remove all changes from the staging area
git reset .

# Rename a file
git mv oldname.js newname.js

# Rename a file within a directory
git mv folder/oldname.js folder/newname.js

# Temporarily save changes and revert to the last commit
git stash

# Restore changes from the stash
git stash apply

# Create a commit with a descriptive message
git commit -m "Message"
Enter fullscreen mode Exit fullscreen mode

Example Workflow

Imagine we’re building a house as a project. Here’s how a workflow might look with three team members: Person A, Person B, and Person C.

  1. Person A initializes the repository:

    git init
    git remote add origin git@gitlab.com:hijuliancode/demo-git-tutorial.git
    
  2. Person A creates an initial file and pushes it to the repository:

    echo "## demo-git-tutorial" >> README.md
    git add README.md
    git commit -m "initial commit"
    git branch -M master
    git push origin master
    
  3. Person B and Person C clone the repository:

    git clone git@gitlab.com:hijuliancode/demo-git-tutorial.git
    cd demo-git-tutorial
    
  4. Person A creates the develop branch and pushes it to the remote:

    git checkout -b develop
    git push origin develop
    
  5. Person B and Person C update their local repositories to include the develop branch:

    git fetch --all
    git checkout develop
    

Now everyone has the repository and can start contributing.

  1. Person A creates a feature branch to work on the house plans:

    git checkout -b feature/house-plans
    touch house-plans.js
    git add house-plans.js
    git commit -m "Add house plans"
    git push origin feature/house-plans
    
  2. Person B and Person C update their local repositories to access the new branch:

    git fetch --all
    git branch -a
    git checkout feature/house-plans
    
  3. Person B wants to suggest improvements to the house plans. They can do so by creating a Pull Request (or Merge Request):

    • Navigate to the repository's GitHub or GitLab page,
    • Go to the Pull Request or Merge Request section,
    • Assign a reviewer (like a team lead or peer).

To apply the suggested changes:

   git add house-plans.js
   git commit -m "Improve house plans"
   git push origin feature/house-plans
Enter fullscreen mode Exit fullscreen mode
  1. Once the feature branch is reviewed and merged, it can be deleted:

    git checkout develop
    git pull origin develop
    git branch -D feature/house-plans
    

Repeat this process for new features or changes!

. . . .
Terabox Video Player