Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 3Name in the course: Git branching

Work in branches and merge them

A branch is a movable name for a line of commits. Work on a change in its own branch, keep main always working, then merge the branch back. Branches are cheap in Git — use one for every change.

  • 4 Lessons
  • 2 Commands

$ run as a normal user# run as root (or with sudo)

3.1

Create and switch branches

git switch -c NAME makes a new branch from where you are and moves to it.

git branch
* main
git switch -c feature/web-subnet
git branch -v
git switch main
git switch -
git branch -m feature/web-subnet feature/web-network
CommandDoes
git branchList local branches (-a also remote ones)
git switch -c NAMECreate and switch
git switch NAMESwitch
git branch -m OLD NEWRename
git branch -d NAME / -DDelete (merged / even when not merged)

Remember

Name branches by purpose: feature/…, fix/…, docs/….

Commands in this lesson

3.2

Merge a branch

git merge BRANCH brings the commits of BRANCH into the current branch.

git switch main
git merge feature/web-network
git merge --no-ff feature/web-network -m "Merge web network"
git branch -d feature/web-network
Merge typeWhenResult
Fast-forwardmain has no new commitsmain just moves forward
Three-way mergeBoth branches have new commitsA merge commit with two parents
--no-ffYou want a merge commit anywayHistory shows the feature as a group
--squashYou want one commit for the whole featureOne new commit, branch history not kept

Commands in this lesson

3.3

Solve merge conflicts

When both branches changed the same lines, Git stops and marks the conflict in the file. You choose the result.

variables.tf (during a conflict)
<<<<<<< HEAD
default = "Standard_B2s"
=======
default = "Standard_D2s_v5"
>>>>>>> feature/bigger-vm
git status
vim variables.tf
git add variables.tf
git commit
git merge --abort
git checkout --theirs variables.tf

At work

Small, short-lived branches give few conflicts. Merge main into a long branch often, or rebase it.

3.4

Keep main safe

main should always work. Changes go in through pull requests with checks — see Work with GitHub pull requests.

Commands in this lesson

All commands in this chapter