Lecture 44

git branches

MCS 275 Spring 2024
Emily Dumas

View as:   Presentation   ·   PDF-exportable  ·   Printable

Lecture 44: git branches

Reminders and announcements:

  • Project 4 due 11:59pm today
  • As announced previously, I do not have office hours this afternoon
  • Future blackboard announcements:
    • When project grading done
    • When course grades submitted
    • When course material archive page ready

Last time

We really talked about collaboration, and the possibility of a failed git push.

This time

Resolve the failed push.

Work on the game and talk about git branches.

Branches

You can have multiple lines of development underway at once, each with their own name and history. You can switch between them at will.

A repo starts with one branch called main.

Each branch is just a pointer to its latest commit.

Branches are local by default but can also be shared.

Working with branches

  • git checkout -b my_branch_name – Create new branch
  • git checkout my_branch_name – Switch to existing branch
  • git checkout main – Switch to the main branch
  • git branch -d my_branch_name – Delete a branch
  • git commit – commit staged stuff to active branch
  • git push origin my_branch_name – push a branch to remote (default is to keep them local)

Why branches?

Often projects keep main clean (e.g. working!), and do all work on changes in branches.

One branch per feature (or task) is common.

This way, features can be worked on in parallel.

End of a branch

Branches "end" in two ways:

  • Abandon: switch away, never go back (maybe delete)
  • Merge: bring changes from one branch into another (often into main)

Merging

To merge alpha into beta, ending alpha:

  • git checkout beta
  • git merge alpha
  • Resolve any conflicts identified.

The merge is a commit to beta.

References

Revision history

  • 2024-04-26 Initial publication