Git & GitLab How-to

Duration1h30

Introduction

This tutorial is a hands-on collaborative exercise that brings Git and GitLab concepts to life through a simple “Hello World” project. You’ll work with a classmate to experience real-world collaboration scenarios including conflicts, branching, and merge requests.

Each student plays both roles:

  • 🅰️ Alice: Create and manage your own project
  • 🅱️ Bob: Collaborate on your partner’s project

Now:

  • Find a classmate to partner with
  • Exchange GitLab usernames
  • You can work asynchronously (coordinate timing via messages)

Prerequisites

Readings

You must read these comprehensive guides before starting:

  1. Git Fundamentals - Complete Git workflow and commands
  2. GitLab Platform - GitLab interface, projects, and SSH setup

Quick SSH Test:

1
ssh -T git@gitlab.com

Expected: Welcome to GitLab, @username!

If you have any problem you need to declare the key on your system. First launch the ssh agent and then add your key with the following commands:

1
2
eval "$(ssh-agent -s)"
ssh-add \path\to\your\key

Git Configuration (Everyone First)

Before starting, verify and configure your Git identity:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Check current configuration
git config --list | grep user

# Set your identity (use your own email)
git config --global user.name "Your Full Name"
git config --global user.email "your.email@provider.com"

# Verify configuration
git config --get user.name
git config --get user.email

Section A: You as Alice (Project Creator)

A1. Create Your Project

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create project directory (use your name for uniqueness)
mkdir hello-world-[your-name]
cd hello-world-[your-name]

# Initialize Git repository
git init

# Create initial file
echo "Hello from [Your Name]!" > hello.txt
echo "This is my collaborative project." >> hello.txt
echo "Owner: [Your Name]" >> hello.txt

# First commit
git add hello.txt
git commit -m "Initial commit: Create hello.txt"

A2. Create GitLab Project

  1. Go to GitLab IMT
  2. Create new project: hello-world-[your-name]
  3. Set visibility to Private
  4. Don’t add README (we have local content)
1
2
3
4
5
# Connect local to remote (replace with your username)
git remote add origin git@gitlab.com:[your-username]/hello-world-[your-name].git

# Push to GitLab
git push -u origin main

A3. Add Your Partner as Collaborator

  1. In GitLab project: Settings → Members
  2. Add your partner’s GitLab username
  3. Set role to Developer
  4. Share project URL with your partner: https://gitlab.imt-atlantique.fr/[your-username]/hello-world-[your-name]

A4. Wait and Monitor Your Partner’s Contribution

Your partner will clone and contribute. Monitor in GitLab, then pull:

1
2
3
4
5
# Check for new commits
git pull origin main

# View updated file
cat hello.txt

A5. Create Template for Future Conflict

Add a template that both you and your partner will fill out:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Add a personal info template to your project
echo "" >> hello.txt
echo "=== Personal Info Template ===" >> hello.txt
echo "Favorite color:" >> hello.txt
echo "Hobby:" >> hello.txt
echo "Location:" >> hello.txt

# Commit and push the template
git add hello.txt
git commit -m "Add personal info template for team"
git push origin main

Notify your partner that the template is ready for them to pull and fill out.

A6. Fill Template (Coordinate with Partner)

Important: Coordinate with your partner to fill the template simultaneously:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Make sure you have the latest template
git pull origin main

# Fill out the template (but don't push yet!)
sed -i 's/Favorite color:/Favorite color: Blue/' hello.txt
sed -i 's/Hobby:/Hobby: Reading/' hello.txt
sed -i 's/Location:/Location: Brest/' hello.txt

# Commit locally (but don't push yet!)
git add hello.txt
git commit -m "Fill out personal info template"

# Coordinate with partner: decide who pushes first
# If you're chosen to push first:
git push origin main  # This will succeed

# If your partner pushes first, your push will fail → conflict!

A7. Resolve Conflict (If You Push Second)

1
2
3
4
5
# If your push fails:
git push origin main  # Will fail with "Updates were rejected"

# Pull partner's changes
git pull origin main  # Conflict occurs here

Understanding Merge Conflicts: When Git can’t automatically merge changes, it marks conflicts in your files with special markers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
=== Personal Info Template ===
<<<<<<< HEAD
Favorite color: Blue
Hobby: Reading
Location: Brest
=======
Favorite color: Red
Hobby: Gaming
Location: Nantes
>>>>>>> abc123...

Resolving Conflicts - Text Editor Method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Edit the file manually
nano hello.txt  # or your preferred editor

# Remove conflict markers and decide what to keep:
# Option 1: Keep both (create two sections)
=== Personal Info Template ===
Alice's Info:
- Favorite color: Blue
- Hobby: Reading
- Location: Brest

Bob's Info:
- Favorite color: Red
- Hobby: Gaming
- Location: Nantes

# Option 2: Create compromise solution
=== Team Personal Info ===
Team favorite colors: Blue and Red
Team hobbies: Reading and Gaming
Team locations: Brest and Nantes

Resolving Conflicts - VS Code Method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Open in VS Code
code .

# VS Code will highlight conflicts with:
# - "Accept Current Change" (yours)
# - "Accept Incoming Change" (theirs) 
# - "Accept Both Changes"
# - "Compare Changes"

# Click the action you want, then save the file

Complete the merge:

1
2
3
4
# After resolving all conflicts
git add hello.txt
git commit -m "Resolve conflict: Merge both personal info"
git push origin main

🔗 Why Conflicts Happen & How Branches Help:

Conflicts occur when two people modify the same lines in the same file. The template scenario demonstrates this perfectly - both people filled the same blank lines simultaneously.

Branches help minimize conflicts by:

  • Isolating features: Each developer works on separate branches
  • Reducing overlap: Different features are developed independently
  • Controlled integration: Changes are merged through review process
  • Parallel development: Multiple features can be developed simultaneously

This is why professional teams use branching strategies - you’ll practice this next!

A8. Understanding Branches & Merge Requests

Why Use Branches?

  • Isolation: Develop features without affecting main code
  • Collaboration: Multiple people can work on different features simultaneously
  • Safety: Main branch stays stable while experimenting
  • Review: Code can be reviewed before integration

What Are Merge Requests?

  • Code Review: Team members review changes before merging
  • Discussion: Collaborate on implementation details
  • Quality Control: Ensure code meets standards
  • Documentation: Track what changes were made and why

Create Feature Branch (Your Project):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Create and switch to feature branch
git checkout -b feature/add-project-info

# Add project information
echo "" >> hello.txt
echo "=== Project Info ===" >> hello.txt
echo "Created by: [Your Name]" >> hello.txt
echo "Collaborator: [Partner Name]" >> hello.txt
echo "Purpose: Learn Git collaboration" >> hello.txt

# Commit to feature branch
git add hello.txt
git commit -m "Add project information section"

# Push feature branch
git push origin feature/add-project-info

A9. Create Merge Request on Partner’s Project

Instead of merging on your own project, create a feature for your partner’s project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Go to partner's project
cd ../hello-world-[partner-name]

# Pull latest changes
git pull origin main

# Create feature branch
git checkout -b feature/add-[your-name]-contribution

# Add your feature
echo "" >> hello.txt
echo "=== [Your Name]'s Feature ===" >> hello.txt
echo "This feature was developed by [Your Name]" >> hello.txt
echo "Feature: Enhanced collaboration info" >> hello.txt

# Commit and push
git add hello.txt
git commit -m "Add [Your Name]'s collaboration feature"
git push origin feature/add-[your-name]-contribution

Create Merge Request:

  1. Go to your partner’s project in GitLab
  2. Click “Create merge request”
  3. Source: feature/add-[your-name]-contribution → Target: main
  4. Title: “Add [Your Name]’s collaboration feature”
  5. Description: “This feature adds enhanced collaboration information to the project”
  6. Assign to your partner for review

Section B: You as Bob (Collaborator)

B1. Clone Your Partner’s Project

Your partner will share their project URL with you:

1
2
3
4
5
6
# Clone partner's project
git clone git@gitlab.com:[partner-username]/hello-world-[partner-name].git
cd hello-world-[partner-name]

# Verify you're in the right project
cat hello.txt

B2. Make Your First Contribution

1
2
3
4
5
6
7
8
# Add your contribution
echo "Hello from [Your Name] (collaborator)!" >> hello.txt
echo "Great to work together!" >> hello.txt

# Commit and push
git add hello.txt
git commit -m "Add [Your Name]'s collaboration greeting"
git push origin main

B3. Fill Template and Participate in Conflict

When your partner notifies you that the template is ready:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Pull the template from your partner's project
git pull origin main

# Check the template
cat hello.txt

# Fill out the template (coordinate timing with partner!)
sed -i 's/Favorite color:/Favorite color: Red/' hello.txt
sed -i 's/Hobby:/Hobby: Gaming/' hello.txt
sed -i 's/Location:/Location: Nantes/' hello.txt

# Commit locally
git add hello.txt
git commit -m "Fill out personal info template"

# Coordinate with partner who pushes first
# If partner pushes first and you push second → you'll get the conflict to resolve!
git push origin main  # May succeed or fail depending on timing

B4. Review and Merge Your Partner’s Feature

When your partner creates a merge request on YOUR project:

  1. Go to your project in GitLab
  2. Open the merge request created by your partner
  3. Review changes in “Changes” tab
  4. Check the new feature they added
  5. Add a comment: “Nice feature! Thanks for the contribution 👍”
  6. Click “Approve”
  7. Click “Merge” to integrate their feature
  8. Choose “Delete source branch” for cleanup

B5. Sync After Merge

1
2
3
4
5
6
7
8
# Pull the merged changes
git pull origin main

# View final result
cat hello.txt

# Check project history
git log --oneline --graph

Complete Workflow Summary

What each student accomplishes:

As Alice (Project Owner):

  • ✅ Create Git repository and GitLab project
  • ✅ Manage collaborator permissions
  • ✅ Handle merge conflicts as project owner
  • ✅ Create feature branches and merge requests
  • ✅ Experience project management workflow

As Bob (Collaborator):

  • ✅ Clone existing project
  • ✅ Contribute to someone else’s codebase
  • ✅ Participate in conflict resolution
  • ✅ Review and approve merge requests
  • ✅ Experience contributor workflow

Collaboration Skills Gained:

  • Basic Git: clone, commit, push, pull
  • Conflict Resolution: merge conflicts handling
  • Code Review: merge request workflow
  • Team Coordination: asynchronous collaboration

Final Validation

Test your understanding:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Navigate to your project
cd hello-world-[your-name]

# Check project status
git status
git log --oneline

# Navigate to partner's project  
cd ../hello-world-[partner-name]

# Check collaboration history
git log --oneline
git remote -v

Success criteria:

  • Both projects have contributions from both people
  • You’ve resolved at least one merge conflict
  • You’ve created and merged a feature branch

Next Steps

🔗 Deepen your knowledge:

🎉 Ready for real projects! You now have the first collaborative Git skills used in professional development teams.