10 minworkshop

Sync with Team Workshop

Sync with Team Workshop

Practice fetching updates, rebasing your work, and cherry-picking commits. These exercises simulate team collaboration scenarios.

Setup

Create a repository with some history to work with:

bash
1$ mkdir sync-practice && cd sync-practice
2$ git init
3$ echo "# Sync Practice" > README.md
4$ git add README.md
5$ git commit -m "docs: initial README"
6
7# Create some history on main
8$ echo "Line 1" > file.txt
9$ git add file.txt
10$ git commit -m "feat: add file.txt"
11
12$ echo "Line 2" >> file.txt
13$ git add file.txt
14$ git commit -m "feat: add line 2"

Exercise 1: Fetch and Inspect

First, let's simulate remote changes by creating a branch that represents "origin/main ahead of local":

bash
1# Create a branch to simulate remote state
2$ git checkout -b simulate-origin
3$ echo "Remote change" >> file.txt
4$ git add file.txt
5$ git commit -m "feat: remote team change"
6
7# Go back to main (simulating your local state)
8$ git checkout main

View What's Different

bash
1$ git log main..simulate-origin --oneline
2abc1234 feat: remote team change
3
4$ git diff main..simulate-origin

This shows what commits are on simulate-origin that aren't on main.

Exercise 2: Rebase Your Work

Create a Feature Branch

bash
1$ git checkout -b feature/my-work
2
3$ echo "My feature" > feature.txt
4$ git add feature.txt
5$ git commit -m "feat: add my feature"
6
7$ echo "More work" >> feature.txt
8$ git add feature.txt
9$ git commit -m "feat: continue feature work"

View Current State

bash
1$ git log --oneline --all --graph
2* abc1234 (feature/my-work) feat: continue feature work
3* def5678 feat: add my feature
4| * 123abcd (simulate-origin) feat: remote team change
5|/
6* 456defg (main) feat: add line 2
7* 789ghij feat: add file.txt

Rebase onto "Remote" Changes

Simulate rebasing onto updated main:

bash
1# First, update main with the remote changes
2$ git checkout main
3$ git merge simulate-origin
4$ git branch -d simulate-origin
5
6# Now rebase your feature
7$ git checkout feature/my-work
8$ git rebase main
9First, rewinding head to replay your work on top of it...
10Applying: feat: add my feature
11Applying: feat: continue feature work

View Rebased History

bash
1$ git log --oneline --all --graph
2* abc1234 (HEAD -> feature/my-work) feat: continue feature work
3* def5678 feat: add my feature
4* 123abcd (main) feat: remote team change
5* 456defg feat: add line 2
6* 789ghij feat: add file.txt

Notice: Your commits are now on top of the remote change (linear history).

Exercise 3: Cherry-pick a Commit

Create a Hotfix Branch

bash
1$ git checkout main
2$ git checkout -b hotfix/urgent-fix
3
4$ echo "Urgent fix" > urgent.txt
5$ git add urgent.txt
6$ git commit -m "fix: urgent production bug"
7
8$ echo "Documentation" > docs.txt
9$ git add docs.txt
10$ git commit -m "docs: add documentation"

Cherry-pick Only the Fix

Let's say we need the fix on main but not the docs:

bash
1$ git checkout main
2
3# Find the commit SHA
4$ git log hotfix/urgent-fix --oneline
5abc1234 docs: add documentation
6def5678 fix: urgent production bug
7
8# Cherry-pick just the fix
9$ git cherry-pick def5678
10[main ghi7890] fix: urgent production bug
11 1 file changed, 1 insertion(+)
12 create mode 100644 urgent.txt

Verify

bash
1$ git log --oneline -3
2ghi7890 (HEAD -> main) fix: urgent production bug
3123abcd feat: remote team change
4456defg feat: add line 2
5
6$ ls
7README.md file.txt urgent.txt

The fix is on main, but docs.txt isn't (it's only on the hotfix branch).

Exercise 4: Handle a Rebase Conflict

Create Conflicting Changes

bash
1$ git checkout main
2$ echo "Main's version" > conflict.txt
3$ git add conflict.txt
4$ git commit -m "feat: add conflict.txt on main"
5
6$ git checkout -b feature/conflict-demo
7$ git reset --hard HEAD~1 # Remove the main commit from this branch
8$ echo "Feature's version" > conflict.txt
9$ git add conflict.txt
10$ git commit -m "feat: add conflict.txt on feature"

Attempt Rebase

bash
1$ git rebase main
2Auto-merging conflict.txt
3CONFLICT (add/add): Merge conflict in conflict.txt
4error: could not apply abc1234... feat: add conflict.txt on feature

Resolve the Conflict

bash
1# View the conflict
2$ cat conflict.txt
3<<<<<<< HEAD
4Main's version
5=======
6Feature's version
7>>>>>>> abc1234 (feat: add conflict.txt on feature)
8
9# Edit to resolve (keep both or choose one)
10$ echo "Combined version from main and feature" > conflict.txt
11
12# Stage and continue
13$ git add conflict.txt
14$ git rebase --continue
15[detached HEAD def5678] feat: add conflict.txt on feature
16 1 file changed, 1 insertion(+), 1 deletion(-)
17Successfully rebased and updated refs/heads/feature/conflict-demo.

Exercise 5: Safe Force Push

If you had pushed your feature branch before rebasing:

bash
1# This would fail with normal push
2$ git push origin feature/my-work
3! [rejected] feature/my-work -> feature/my-work (non-fast-forward)
4
5# Safe force push
6$ git push --force-with-lease origin feature/my-work

Note: For this exercise, we're not connected to a remote, but this is how you'd handle it.

Checklist

  • Used git log A..B to see commits between branches
  • Rebased a feature branch onto updated main
  • Cherry-picked a specific commit
  • Resolved a rebase conflict
  • Understand when to use --force-with-lease

Cleanup

bash
1$ cd ..
2$ rm -rf sync-practice

Key Takeaway

Staying synchronized is essential for team collaboration. Fetch often, rebase before pushing, and cherry-pick for targeted changes. Always resolve conflicts carefully and use safe force push.