Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Branches | Working with Branches in Git
Git Essentials
course content

Conteúdo do Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

bookCreating Branches

git branch

When working with branches in Git, you’ll mostly use the git branch command. In fact, this command can be used for various purposes:

  • listing branches;
  • creating branches;
  • deleting branches;
  • manipulating branches (e.g., renaming branches).

Listing and Creating Branches

Without further ado let’s first list all the branches in our repository:

As you can see, the only branch we currently have is master which is the default branch. It may not necessarily be called master in your case, so don’t worry if you have it named otherwise.

Note

Currently, new versions of Git tend to use main as the name for the default branch instead of master.

There is also an asterisk (*) near the branch name which indicates that the HEAD points to this branch (we are currently on this branch).

To create a new branch, you can use the git branch command followed by the name of the new branch.

Where <branch_name> is the name of the branch you want to create.

Branch Naming Conventions

It's common to follow naming conventions for branches, such as using prefixes like feature/, bugfix/, or hotfix/ to denote the purpose of the branch. Consistent naming conventions make it easier to understand the role of each branch in the development process.

Example Workflow

Let’s now create a new branch named testing/some-tests and then list the branches.

Switching between Branches

As for now, we have two branches, and since the asterisk (*) is near the master branch, the HEAD still points to this branch (we’re still on the same branch). However, HEAD is a movable pointer , so we can actually switch to the other branch via running the git checkout command:

Where <branch_name> is the name of the branch you want to switch to. This command updates the working tree to match the selected branch including both the files and the git history.

Let’s now switch to our newly created branch and list the branches:

As you can see, since the asterisk (*) is now near the new branch, we’re currently on it (the HEAD point to this branch). To verify it, let’s create a simple text file and commit it:

The commit is successful, so we can now check the two latest commits in our commit history:

Since we’re currently on the testing/some-tests branch, this branch points to the latest commit (so does the HEAD), while the master branch is one commit behind.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt