51 lines
1.2 KiB
Markdown
51 lines
1.2 KiB
Markdown
# git branch, checkout & merge
|
|
|
|
## Goal
|
|
Create branches, switch between them, and merge changes.
|
|
|
|
## Create and List Branches
|
|
```bash
|
|
cd ~/lab-git-essentials/my-first-repo
|
|
git branch
|
|
git checkout -b feature/intro
|
|
git branch
|
|
```
|
|
|
|
## Add Work on the Feature Branch
|
|
```bash
|
|
echo "Feature text" >> README.md
|
|
git add README.md
|
|
git commit -m "Add feature text to README"
|
|
```
|
|
|
|
## Switch Back to Main and Merge
|
|
```bash
|
|
git checkout -b main 2>/dev/null || true
|
|
git checkout main
|
|
git merge feature/intro
|
|
```
|
|
|
|
## Create a Merge Conflict (Optional, for practice)
|
|
```bash
|
|
# On main, change the same line differently
|
|
sed -i '1s/.*/# My First Repo (main)/' README.md
|
|
git add README.md
|
|
git commit -m "Tweak README title on main"
|
|
|
|
# On feature branch, make a conflicting change
|
|
git checkout feature/intro
|
|
sed -i '1s/.*/# My First Repo (feature)/' README.md
|
|
git add README.md
|
|
git commit -m "Tweak README title on feature"
|
|
|
|
# Merge and resolve
|
|
git checkout main
|
|
git merge feature/intro
|
|
# Open README.md, resolve conflict markers <<<<<<<, =======, >>>>>>>
|
|
git add README.md
|
|
git commit # finalizes the merge
|
|
```
|
|
|
|
## Outcome
|
|
You can branch, switch branches with `git checkout`, and merge changes, including resolving conflicts.
|