GIT Crash Course: Git Branch

Control Version 101

Pat Guillen
3 min readDec 19, 2022

Branches with Git

New Branch

git branch dev
Creating a branch called Dev

Switching between branches

Both checkout and switch work the same.
git checkout branchName
git switch branchName
Using git branch I created two more branches (inv = inventory | quest = story). You can create as many branches as needed. We can merge these later.

Reverting or Resetting by Branch

The new script called dev.. that I only want in my dev branch
get a recent push… what now?

where is all my stuff? well, I need to merge first.

I’m behind the main branch
# Merge from main to get files from main in dev
git switch main
git merge dev
Watch the dev script, when we change branches it is not present. But then we merge it then it gets added.

Inventory Script only exists in two branches (dev and inv) but not in the main.

git status
git add .
git commit -m "Dev and Inventory merged to update dev branch"
git push origin dev
Now I can see both branches.
Adding inv branch.
# Main update from dev
git switch main
git pull origin main
git merge dev # make sure everything is updated before merging.
git push origin main # Don't forget to push.
You can see that the dev and main are up to date but I have not updated the main with the inv branch.

Reverting/Reset Project by branch

git switch main
git log # This will give you all your prior commits. Which we can use as branches.
# Hit q to quit that screen
Why commit comments matter
The hash is the branch name for the commit.
git switch 736da86f18968b103c437ce4f7ef387bbda1912c

# Not the right branch?
git switch main
git log
# find the right one, copy and press q

# If its the right point where we want to branch from
git switch -c NewBranchName 736da86f18968b103c437ce4f7ef387bbda1912c

Go back to the beginning

can’t find it in the log…well there is another way. Go to GitHub and find the commit and the #
#This updated your local project
#reseting to the begining of the day.

git reset --hard 12eb979f8caa869fca75f701794bdc78b828bfd5
#This updated your REPO
#reseting to the begining of the day.
git reset --hard 12eb979f8caa869fca75f701794bdc78b828bfd5
#this only updated main not dev, inv or quest.
git push --force origin main

If you want to know more about me, feel free to connect with me on LinkedIn

--

--