💻
How to Use Project Development Tools in a Remote C
  • How to Use Project Development Tools in a Remote Coding Team
  • Trello
    • Introduction to Trello
    • How to use Trello
      • What is Trello?
      • Starting up a Trello Board
      • Lists
      • Cards
  • Visual Studio Code
    • Introduction to Live Share in Visual Studio Code
    • How to use Live Share
      • How to Install
      • How to Use
      • Examples of Use Cases
  • Git & Github - Basics
    • Introduction to Git & GitHub
    • How To Use Git
      • Setting up Git
      • Creating GitHub Repositories
      • Managing Repository Access
      • Syncing with a Local Workspace
  • How to Manage a Codebase with Git
    • Introduction to Branching
    • How To Use Branches
      • What are Branches?
      • Working in Branches
      • Merging Branches
      • Best Practices For Branching
    • Git Codebase Management Cheat Sheet
  • Conclusion
  • Team Biography
  • References
Powered by GitBook
On this page
  • Navigate to a Git Repo
  • The Revision Tree
  • Creating and Switching Branches
  • Managing Branches on GitHub
  • Recap

Was this helpful?

Export as PDF
  1. How to Manage a Codebase with Git
  2. How To Use Branches

Working in Branches

PreviousWhat are Branches?NextMerging Branches

Last updated 4 years ago

Was this helpful?

With knowledge of these terms and what - on a more technical level - branches really are, we can begin to demonstrate how to use them.

Navigate to a Git Repo

To get started with branching, point your Git console within a repo, as is necessary for running any Git commands. We will demonstrate these tasks on a new GitHub repository with nothing but the default readme file:

The Revision Tree

Github offers a way to visualize a repository and its branches under insights -> network on the page for your repo. With our initial commit, the tree is quite bare:

As we begin to use branches, we will see how the tree allows us to visually depict how different branches are developed on parallel.

Creating and Switching Branches

Whenever working in Git, you will be working on a particular branch (master by default). To create or switch to a branch, use:

git checkout -b <branch name>

If the “-b” argument is provided and the branch name does not exist, a new branch will be created with that name. Any uncommitted changes on your current branch will be transferred to the new branch. Then, whether the branch already existed or a new one was created, you will be switched to working on that branch.

Now make a new file, with some basic contents, and commit it to the repo. We will use the following example:

Example.txt (example-branch is the branch name we will use)

// Hello World!

Pushing a branch to a remote repo (such as on GitHub) requires a special command if the branch does not exist there yet (otherwise just push normally):

git push --set-upstream origin <branch name>

When this is done, we may view the branch in our repository on GitHub. By using the small dropdown on the top left to select the current branch. First, we notice that our commit and corresponding file are not in the repository by default. This is because we are viewing the main branch and not our new branch. When viewing the new branch, we see that our changes are there.

Now, use the git checkout command to switch back to the main branch. You will notice that any new changes in the branch you created are missing. This is because they are only on that branch! We now make a change here, to demonstrate how the two branches can be developed independently:

Example.txt (main)

// Hola!

Now we may commit this file, and view the revision tree, which will reveal what our repository looks like now with its two branches, each with their own different "code":

As we can see, the two branches are now split from the point we made the new branch, and each has their own commits with different content.

Managing Branches on GitHub

GitHub also provides a user interface on the website for creating and managing branches. By clicking on "branches" next to the branch selection dropdown when viewing the repo, you will be able to view the branches that have been created. Here you can manage and navigate between branches. To delete a branch, click the small trash can icon for the branch you wish to remove.

Returning to the homepage for the repo, typing a non-existent branch name in the branches dropdown and hitting enter will create a new branch from the one currently being viewed.

Recap

In summary, we have learned how to use branches. From this guide you should know how to:

  • Push branches to a remote repository

  • View them on GitHub

  • Add files and code to them independently

  • Visualize the revisions and branching history in a tree-like view

  • Create and manage branches through GitHub's interface instead of the terminal

The example repository to be used for this tutorial
The revision tree
The newly committed example file is only visible when the repository is being viewed within the branch.
The revision tree after committing a new branch
The branches window
Creation of a new branch by typing an unused branch name in the branches dropdown