COS 3100:
Intro to GIT
menu ▾
lessons
tags
extras
links
syllabus
homeworks
online terminal
view full lesson
lessons
tags
extras
links
syllabus
homeworks
online terminal
view full lesson
## 6. Working with branches Most projects have a complicated history that is not a straight line and include contributions from multiple people --- ## What Are Branches? A **branch** in Git is essentially a moveable pointer to a specific commit. Branches are lightweight - a branch is just a file containing a commit hash. > Files you commit on one branch are not visible when you switch to another. --- ### Why Use Branches? - **Safe experimentation** - Try new features without breaking working code - **Parallel development** - Multiple people can work on different features simultaneously - **Easy rollback** - Discard experimental work if it doesn't work out - **Version support** - apply fixes to previous versions --- ## Listing Branches When you create a repository, Git automatically creates a default branch (usually called `main` or `master`). ```bash # See current branch git branch # See all branches with more details git branch -v # List all branches (local and remote) git branch -a ``` --- ### Branch Visualization Multiple branches can point to the same commit.  --- ## Creating a New Branch ```bash # Create a new branch (but stay on current branch) git branch feature-user-profile # Create and immediately switch to new branch git checkout -b feature-user-profile # Modern alternative (Git 2.23+) git switch -c feature-user-profile ``` --- ### Branch Naming Conventions ```bash # Good branch names feature/user-authentication bugfix/login-timeout docs/api-documentation refactor/database-queries # Poor branch names fix temp my-branch new-stuff ``` --- ## Switching Between Branches ```bash # Switch to existing branch git checkout main git checkout feature-user-profile # Modern alternative (Git 2.23+) git switch main git switch feature-user-profile ``` > Since I am old, you'd probably see me using the old commands out of habit. --- ## The HEAD meta-branch Git tracks the current branch in a *special ref* called `HEAD`. It is similar to a branch in that it can be used as an argument where-ever a branch is needed. But it is NOT a true branch - it points to the current branch ---  > Branches point to commits; HEAD points at a branch --- All of the following commands will do the same thing - display the contents of the *commit* that is pointed to by the *branch*, that is currently **checked-out** ```bash git show HEAD git show master git show f30ab ``` --- ## Checkout The checkout command does 2 main things: - Changes the branch that `HEAD` points to - Overwrites the contents of the working directory with the contents of the commit pointed by the new `HEAD` branch --- In this example `HEAD` is pointing to the `testing` branch which is one commit ahead of `master`:  --- <class-work> ### Replicate the state of the repository from the picture </class-work> --- ## Renaming Branches ```bash # Rename current branch git branch -m new-name # Rename specific branch git branch -m old-name new-name ``` --- ## Deleting Branches ```bash # Delete merged branch (safe) git branch -d feature-name # Force delete branch (even if unmerged) git branch -D feature-name # Delete remote tracking branch # more on this later git branch -dr origin/feature-name ``` --- ## Branches can diverge Until now we have been looking at linear histories. But often branches will diverge:  > Later we will explore how to **merge** them back together --- ## Viewing divergent trees `log` provides a couple of options for viewing commit ranges: ```bash # shows all commits on my_branch that # are NOT on master git log --oneline --graph main..my_branch # shows all commits on both main and my_branch # but NOT the shared ancestry git log --oneline --graph main...my_branch ``` ---  ```bash git log --oneline --graph master..experimental # shows C and D ``` ---  ```bash git log --oneline --graph master...experimental # shows E, F, C and D ``` --- ## Tracking Remote Branches ```bash # See all branches (local and remote) git branch -a # Create local branch that tracks remote git checkout -b feature-branch origin/feature-branch # Simplified version (Git 2.23+) # Auto-tracks if remote exists git switch feature-branch ``` --- ## Working with Remote Branches ```bash # Create local branch git checkout -b new-feature # make changes, commit and push to remote git push -u origin new-feature # Delete remote branch git push origin --delete old-feature # Update remote tracking branches git fetch --prune ``` --- ## Branch Life-cycle 1. **Create** branch from up-to-date main 2. **Work** on feature with regular commits 3. **Test** thoroughly before merging 4. **Request** code review 5. **Merge** to main after approval 6. **Delete** feature branch after merge 7. **Update** local main branch --- ## Detached HEAD If HEAD points directly at a commit, git will inform you that you are in a **detached HEAD** state. ```bash git checkout <commit> # prints: # Note: switching to '<commit>'. # # You are in 'detached HEAD' state. # You can look around, make experimental changes # ... ```
Working with remote repositories
::
Merging, rebasing and handling conflicts
Working with remote repositories
::
Merging, rebasing and handling conflicts