DEV Community

Marcos Mtz
Marcos Mtz

Posted on • Updated on

Quick notes about Git Branches

From the book Beginning Git and GitHub - Tsitoara

A commit can contain information about its author, date, the project snapshot and the name of the previous commit.

The name of the previous commit is called parent and every commit except the first one has at least a parent.

Branches are made when two different commits have the same parent.

Simply speaking, a Git branch is just a simple reference to one commit in a list of chained links.

Git uses a special reference called HEAD to get information about which branch we are currently working.

Git branches are stored in files inside the .git/refs/heads/ folder and they contain the hash string of the latest commit for that branch.

If you are checking out a previous version using a hash string instead of a branch name, you will fall into a state called detached HEAD.

Caution Just like human bodies, never be in a state of “detached HEAD” if you can avoid it. It is a very dangerous situation to find oneself in.

For most situation, you can think of HEAD as the reference to the current branch, and every commit you create will use the last commit in that branch as a parent.
- No parents: The very first commit
- One parent: Normal commit in a branch
- Multiple parents: A commit created by the merge of branches

Note Like when we navigated between versions, you can’t switch branches if you have uncommitted changed files. Commit before you move. Or use a technique called “stashing” that we will see in later chapters.

Tip To immediately switch to a new branch after creating it, use the option “-b” with the git checkout command. For example, “git checkout -b testing” is the same as “git branch testing” and then “git checkout testing.”

git checkout -b anewbranch
Enter fullscreen mode Exit fullscreen mode

To delete a branch, simply use the same command as to create one but with the option “-d.”

$ git branch -D branchname
Enter fullscreen mode Exit fullscreen mode

Top comments (0)