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 branch
- You can merge and resolve a conflict yourself
- You know branch naming conventions and the basic strategy
A branch is nothing more than a label pointing at a commit. It doesn't copy files, so creating and deleting one costs almost nothing. That's why in GIT it's normal to open a branch per feature and per bug. 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 code, 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.
Commands for this step
Don't just read them — type them into a real terminal. 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.