Branching and merging
Split work off and bring it back later. Once you grasp how cheap a branch is, the way you use GIT changes.
What you'll learn
- You can isolate work on its own
- You can and resolve a yourself
- You know branch naming conventions and the basic strategy
A branch is nothing more than a label pointing at a . It doesn't copy files, so creating and deleting one costs almost nothing. That's why in it's normal to open a branch per feature and per . git switch -c <name> creates one and moves you onto it in the same step; when the work is done you come back and merge.
You merge with git merge. If the two branches touched different files, GIT combines them for you. If they changed the same lines of the same file differently, GIT refuses to guess: it marks a conflict and stops. A conflict is not an error — it's GIT saying "a human has to decide this one".
In a conflict the file gains <<<<<<<, =======, and >>>>>>> markers. Above is your current branch, below is what's coming in. Pick one side or combine both into correct , delete every marker line, then git add and commit to finish the merge. Prefixing branch names with the purpose — feature/, fix/, chore/ — lets the branch list alone tell you what's going on.
Understand it easily
A branch is a side road. You leave the road to try something, and if it works out you join the main road again (merge). If it does not, you simply leave the side road behind, and the main road was never touched.
Commands for this step
Don't just read them — type them into a real . Your hands have to remember, not your eyes.
git switch -c feature/login- Create a branch and switch to it in one step.
git switch main- Go back to the branch you want to merge into.
git merge feature/login- Merge the finished branch in. On conflict, GIT stops and asks you to decide.
git branch -d feature/login- Delete a merged branch. Cleaning up keeps the list readable.
Common mistake and how to fix it
Forgetting which branch you are on and committing to main. The first line of git status shows the branch. If it already happened and you have not , make a branch at that spot with git branch feature/x, then run git reset --hard HEAD~1 on main. The commit stays safe on the new branch.
You mark this yourself. Nothing is graded here.
Mark this step as understood?
This cannot be undone yet, so mark it only when you really understand.
Marked as understood
You mark this yourself. Nothing is graded here.