Branching and Merging
Working on Features Safely
A branch is an independent line of development. It lets you work on a new feature without disturbing the stable, working version of your project.
The Main Branch
Every repository starts with one branch, usually called main. Think of it as the reliable, deployable version of your project.
Creating and Switching Branches
The modern command for both creating and switching is git switch. The -c option creates a new branch.
git switch -c login-feature # create and move to a new branch
git switch main # move back to the main branchYou can list all branches with git branch. The current branch is marked with an asterisk.
Why Branches Help
- Your unfinished work stays separate from stable code.
- Several people can work on different features at the same time.
- If an experiment fails, you can simply delete the branch.
Merging Branches
When a feature is finished, you bring its changes back into main by merging. First switch to the branch you want to merge into, then run git merge.
git switch main
git merge login-featureGit combines the histories of both branches. After a successful merge you can safely delete the feature branch.
git branch -d login-featureMerge Conflicts
A merge conflict happens when two branches change the same lines of the same file in different ways. Git cannot decide which version is correct, so it asks you.
Git marks the conflicting region inside the file like this:
<<<<<<< HEAD
Welcome to our website
=======
Welcome to DCCPS
>>>>>>> login-featureTo resolve it, edit the file to keep the correct content, remove the marker lines, then git add the file and commit.
Conflicts are normal in teamwork and not a sign of error. Pulling the latest changes often keeps conflicts small and easy to handle.
Discussion
0No comments yet — be the first to leave one!