Programación Para Tontos

Git

Tutorial: create your first branch without fear

Learn to create and switch between Git branches to experiment without risking your main code.

A branch is a parallel copy of your project where you can experiment without fear of breaking the "real" version. It's like saving your game before trying something risky.

Step 1: check which branch you're on

git branch

You'll see something like * main, where the asterisk marks the active branch. main (or sometimes master) is usually your project's primary branch.

Step 2: create a new branch

git branch new-feature

This creates the branch, but doesn't move you to it yet (like creating a backup save without opening it).

Step 3: switch to the new branch

git checkout new-feature

Or, in one step (create and switch at the same time):

git checkout -b another-feature

-b tells Git "create this branch and switch to it right away". Now any commit you make lives only on this branch, without affecting main at all.

Step 4: make changes and go back to main

echo "testing something" >> notes.txt
git add notes.txt
git commit -m "Experiment on a new branch"

git checkout main

Back on main, you'll notice notes.txt doesn't have those changes (because they live on the other branch!). Nothing broke, nothing got mixed up, everything stays intact.

Step 5: merge the changes when you're ready

git merge new-feature

merge brings the changes from the new-feature branch into the branch you're currently on. Only do it once you're sure the experiment worked.

What did you just learn?

  • What a branch is and why it lets you experiment without fear.
  • How to create and switch branches with git branch and git checkout.
  • How to merge changes back with git merge.