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:
bash1$ mkdir sync-practice && cd sync-practice2$ git init3$ echo "# Sync Practice" > README.md4$ git add README.md5$ git commit -m "docs: initial README"67# Create some history on main8$ echo "Line 1" > file.txt9$ git add file.txt10$ git commit -m "feat: add file.txt"1112$ echo "Line 2" >> file.txt13$ git add file.txt14$ 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":
bash1# Create a branch to simulate remote state2$ git checkout -b simulate-origin3$ echo "Remote change" >> file.txt4$ git add file.txt5$ git commit -m "feat: remote team change"67# Go back to main (simulating your local state)8$ git checkout main
View What's Different
bash1$ git log main..simulate-origin --oneline2abc1234 feat: remote team change34$ 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
bash1$ git checkout -b feature/my-work23$ echo "My feature" > feature.txt4$ git add feature.txt5$ git commit -m "feat: add my feature"67$ echo "More work" >> feature.txt8$ git add feature.txt9$ git commit -m "feat: continue feature work"
View Current State
bash1$ git log --oneline --all --graph2* abc1234 (feature/my-work) feat: continue feature work3* def5678 feat: add my feature4| * 123abcd (simulate-origin) feat: remote team change5|/6* 456defg (main) feat: add line 27* 789ghij feat: add file.txt
Rebase onto "Remote" Changes
Simulate rebasing onto updated main:
bash1# First, update main with the remote changes2$ git checkout main3$ git merge simulate-origin4$ git branch -d simulate-origin56# Now rebase your feature7$ git checkout feature/my-work8$ git rebase main9First, rewinding head to replay your work on top of it...10Applying: feat: add my feature11Applying: feat: continue feature work
View Rebased History
bash1$ git log --oneline --all --graph2* abc1234 (HEAD -> feature/my-work) feat: continue feature work3* def5678 feat: add my feature4* 123abcd (main) feat: remote team change5* 456defg feat: add line 26* 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
bash1$ git checkout main2$ git checkout -b hotfix/urgent-fix34$ echo "Urgent fix" > urgent.txt5$ git add urgent.txt6$ git commit -m "fix: urgent production bug"78$ echo "Documentation" > docs.txt9$ git add docs.txt10$ git commit -m "docs: add documentation"
Cherry-pick Only the Fix
Let's say we need the fix on main but not the docs:
bash1$ git checkout main23# Find the commit SHA4$ git log hotfix/urgent-fix --oneline5abc1234 docs: add documentation6def5678 fix: urgent production bug78# Cherry-pick just the fix9$ git cherry-pick def567810[main ghi7890] fix: urgent production bug11 1 file changed, 1 insertion(+)12 create mode 100644 urgent.txt
Verify
bash1$ git log --oneline -32ghi7890 (HEAD -> main) fix: urgent production bug3123abcd feat: remote team change4456defg feat: add line 256$ ls7README.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
bash1$ git checkout main2$ echo "Main's version" > conflict.txt3$ git add conflict.txt4$ git commit -m "feat: add conflict.txt on main"56$ git checkout -b feature/conflict-demo7$ git reset --hard HEAD~1 # Remove the main commit from this branch8$ echo "Feature's version" > conflict.txt9$ git add conflict.txt10$ git commit -m "feat: add conflict.txt on feature"
Attempt Rebase
bash1$ git rebase main2Auto-merging conflict.txt3CONFLICT (add/add): Merge conflict in conflict.txt4error: could not apply abc1234... feat: add conflict.txt on feature
Resolve the Conflict
bash1# View the conflict2$ cat conflict.txt3<<<<<<< HEAD4Main's version5=======6Feature's version7>>>>>>> abc1234 (feat: add conflict.txt on feature)89# Edit to resolve (keep both or choose one)10$ echo "Combined version from main and feature" > conflict.txt1112# Stage and continue13$ git add conflict.txt14$ git rebase --continue15[detached HEAD def5678] feat: add conflict.txt on feature16 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:
bash1# This would fail with normal push2$ git push origin feature/my-work3! [rejected] feature/my-work -> feature/my-work (non-fast-forward)45# Safe force push6$ 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..Bto 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
bash1$ 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.