Skip to content

Branches & Moving Around

The Basics

Saving Your Work (Commit)

Think of a commit as saving a checkpoint in a video game. It takes a snapshot of your files exactly as they are right now.

git commit

Working on a Branch

A branch is a separate timeline. It lets you work on a new feature or fix a bug without messing up the main, working version of your code (usually called main).

# Create a new branch called "feature-xyz"
git branch feature-xyz

# Hop over to your new branch
git checkout feature-xyz

# Shortcut: Create a branch AND hop onto it immediately
git checkout -b feature-xyz

Merging vs. Rebasing (Combining Work)

When you are done working on your branch, you need to bring those changes back into the main timeline.

  • You can do this in two ways:

Method 1: Merging This takes your branch and ties it back into main using a special "knot" called a merge commit. It keeps a clear record of exactly when the two timelines met.

# Hop back to main, then pull the bugfix changes in
git checkout main
git merge bugfix

Method 2: Rebasing This takes all the new saves (commits) from your branch, lifts them up, and places them neatly on top of the latest save on main. This makes your project history look like a clean, straight line.

git rebase main

Moving the Time Machine (HEAD)

In Git, HEAD is a pointer that simply means "Where I am looking right now." Usually, it points to the newest save on your current branch.

# Jump directly to a specific older save point (using its name or ID)
git checkout c1

Relative Shortcuts

Instead of typing out long ID numbers to move backward, you can use these shortcuts:

^ (Caret): Go back one save point.

~ (Tilde): Go back a specific number of save points.

main^     # The parent of the main branch
main^^    # The grandparent of the main branch
HEAD^     # One step back from where you are standing right now

Forcing a Branch to Move

If you want to drag a branch pointer to an entirely different spot by force, use the -f flag:

# Force the main branch to move backward by 3 saves
git branch -f main HEAD~3

Undoing Mistakes

Option A: Reset (For Local Work Only) If you made a mistake on your computer and nobody else has seen it, you can rewrite history. This moves your branch backward in time as if the mistake never happened.

# Wipe away the very last save you made
git reset HEAD~1

Option B: Revert (For Shared/Public Work) If you already uploaded your work to the internet, erasing history will break things for your teammates. Instead, use revert. This creates a brand new save point that does the exact opposite of your mistake, neatly canceling it out.

git revert HEAD

Dealing with Merge Conflicts (When Timelines Clash)

Sometimes, when you try to git merge or git rebase, Git will stop and yell: "CONFLICT!" This happens because you and a teammate (or you on two different branches) changed the exact same line in the exact same file. Git doesn't know which version is the correct one, so it pauses time and asks you to decide.

How to Fix a Conflict:

  1. Open the conflicted file in your text editor (like VS Code).
  2. Look for the conflict markers that Git inserted: ```text <<<<<<< HEAD Your local changes look like this. ======= The changes from the other branch look like this.

    branch-name Delete the markers (<<<<<<<, =======, >>>>>>>) and edit the text so it looks exactly the way you want it to look.

  3. Tell Git you fixed it and finish the process:

# Stage the fixed file
git add filename.txt

# If you were MERGING, finish with:
git commit -m "Resolved merge conflict"

# If you were REBASING, finish with:
git rebase --continue