What git branch am I in?

What git branch am I in?

What git branch am I in? It’s an age old question I’ll ask myself maybe once an hour. I’ll make a big change, decide that I need to save the repo before I break something, I make a commit (often times with commitizen), and then git push origin ... . But where was I pushing again?

The solution is hilariously simple and even easier to remember! Just execute what we mention below. We’ll setup an example too for those who want to see it in action.

Solution

git branch

That’ll output all your local branches and it will highlight which branch you’re currently in.

My Setup

Recreating the Problem

Let’s recreate our forgotten branch problem. We’ll do it all by command line to work on our bash skills. So we’re making a folder to instantiate git in.

mkdir exampleGitDir && cd exampleGitDir

This will make the folder and maneuver you into the directory. Then let’s make some files just for fun. You can checkout this post if you have any questions on what touch is in the following commands.

touch txt1.txt
touch txt2.txt
touch txt3.js

So now you have three empty files in your directory. Let’s instantiate the git repo by executing git init.

git init

Your terminal will respond with what’s mentioned above. Then it’s time to create a couple branches, switch between them, and output where you actually are editing. You’ll need a first commit before branching out. Execute the following…

git add .
git commit . -m "First commit"

And now that the master branch has been created we can move onto making our random ass branch names. Let’s make three branches and switch between them.

git branch branch1
git branch branch2
git branch branch3

Now you have three branches for whatever nonsense you had planned. You should still be on master if you’ve followed along. Execute git branch if your’re not sure! I’m seeing the following which tells me I am in fact on the master branch.

Now let’s checkout branch 1 just for fun.

git checkout branch1

Now all our commits will be made to branch1. The same can be done to review the changes in each of the other branches. Now hopefully I won’t have to ask, “What git branch am I in?”

Leave a Comment