Branching

Why Branch?

Make mars.txt a pro/con list

No Change

No Change

branch

mars.txt

mars.txt

mars.txt

mars.txt

mars.txt

Master branch

New idea branch

you test your new idea on this branch

Everyone looks at this unchanged version

Branching

  • create a branch called pro_con_list
  • ask git which branch you are on
  • switch to your new branch
git branch pro_con_list
git branch
git checkout pro_con_list
  • Modify mars.txt to be a pro/con list
  • save your changes
  • View mars.txt on the pro_con_list branch
  • View mars.txt on the master branch

Exercise 1

  1. Make sure you are on the master branch
  2. Create a new file called packing_list.txt with an item you want to bring to mars
    1. Is this visible on in your file system?
    2. Switch to the pro_con_list branch. Is it visible?
  3. Switch back to the master branch. Add and commit packing list
  4. Repeat 2.1 and 2.2. Why is the result different?
  5. Switch back to the master branch
  6. Create a new branch called wolfman_packing
  7. Switch to wolfman_packing and add an item to packing_list.txt. Add and commit packing_list.txt to the wolfman_packing branch
  8. Switch back to master. Is your new item on the packing list?
git branch branch_name
git checkout branch_name
git branch

Merging

add item to packing_list.txt

add item to packing_list.txt

no change

packing_list.txt

packing_list.txt

packing_list.txt

packing_list.txt

packing_list.txt

packing_list.txt

Combination of red and yellow files

Merging: No conflict

  • Different lines modified
  • Merge pro/con list into master
  • Save merge commit message
  • Check that you are on the master branch
  • Look at mars.txt
  • Delete branch if needed
  • WARNING: you cannot recover branches once they've been deleted
git checkout master
git merge pro_con_list
git branch -D pro_con_list

Exercise 2

  1. view packing_list.txt on the master branch
  2. view packing_list.txt on wolf man_packing branch
  3. switch back to the master branch
  4. merge wolfman's packing list into the master packing list. DO NOT delete wolf man's branch
  5. Repeat #1 and 2

Merging w/Conflict

  • The same line modified
  • modify line 2 of mars.txt on the wolf man_packing branch
  • add and commit
  • checkout the master branch
  • modify line 2 of mars.txt on the master branch
  • add and commit
  • merge wolfman_packing into master
  • CONFLICT
git merge wolfman_packing

Resolving Conflicts

  • How to resolve it
    • open mars.txt
  • Edit file until it looks they way you want it to look.
  • Add and commit (you don't need the -m this time)

Text from master branch

Text from wolf man_packing branch

}

}

Exercise 3

  1. Modify the first line of packing_list.txt on the master branch
  2. add and commit
  3. Switch to the wolfman_packing branch
  4. Modify the first line of packing_list.txt on the wolfman_packing branch
  5. add and commit
  6. Merge the wolf man_packing branch into the master branch
  7. resolve conflicts
  8. add and commit

Resource: http://git-scm.com/book/en/

deck

By abostroem

deck

  • 1,683