Git Branches^

Branches are an area where Git shines over the embedded version control offered by other applications. If you think of a commit as a snapshot of a directory structure at a particular point in time, a branch can be thought of in terms of caching the current snapshot while you work on a separate feature.

For example, imagine if you wanted to revise a chapter in your novel. Instead of just revising it, you make a copy of the file to preserve its previous state while you work. You can still share the current draft without showing any of the changes under way.

Git takes all the fiddly tasks to manage multiple versions of your work in progress. The tool makes it easy for you to branch off and try something new and then jump back to where you were or merge the finished changes back into your main branch.

Show Branches^

Branches exist on both the local repository and the remote repositories. To see the branches you have available, use the git branch command.

$ git branch -vv
  ATLOG-1-log   e3f45ec [origin/main] ATLOG-1: Console
  ATTREE-1-ts   ak94jfa [origin/main] ATTREE-1: Tree Sitter
  ATTREE-2-ts-c c2ac4e8 [origin/main] ATTREE-2: C Parser
* main          f2f45e8 [origin/main] ATLOG-1: Console (#153)

Here, the -v option is given twice to show the hash of the last commit on the branch as well as its upstream remote. This also notes how many commits the branch has ahead of its upstream as well as how many commits behind it is from upstream.

If it shows both, it means the branch has commits not present on the upstream remote and the upstream remote has commits that have not been merged into the branch. This is the same information provided by git status when you have checked out a specific

Create Branches^

You can create a branch using the git branch interface, but generally this is more often done by pairing the checkout command with the -b option.

$ git checkout -b ATTREE-3-ts-rst origin/main

This command creates the ATTREE-3-ts-rst branch with the remote main branch set as its upstream and then checks out the branch so that it is the current state of the local repository.

So, if you were on a different branch with a lot of work in it, creating a new branch based on main would revert to the current local state of the main branch.

Checkout Branches^

In cases where you have already created a branch, you can use the git checkout command to switch to that branch. This command requires a clean repository. You can’t check out a branch if you have uncommitted work on your current branch.

To check out a branch, run the git checkout command with your branch name:

$ git checkout ATTREE-2-ts-c

When you need to check out a branch that does not exist on your local repository, you can pull it down using the git switch command:

$ git switch origin/ATTREE-6-ts-org

Local Merge^

When dealing with a local repository only, you can merge in changes from a branch yourself using the git merge command.

To start, check out the branch you want to receive the changes:

$ git checkout main

Then, merge in the changes from the other branch:

$ git merge ATTREE-1-tree-sitter

In the case of a remote, you would then push the changes up to remote.

$ git push origin main

Now both your local main branch and the remote have the changes from the branch.

Note

In practice, this tends not to be the way companies merge changes. When working with a team, more often you create a pull request on GitHub either from your fork or on the company repository and then request a code review from your colleagues.

The process of actually merging the PR into the main branch is handled through the web interface rather than the Git command.

Remove Branches^

When you have finished with a branch, either because you’ve abandoned the task or you have completed it and merged the changes into the remote main branch, you may want to do some housekeeping on your local repository and delete the branch.

To delete the local instance of a branch, use the -d option. Exempli gratia:

$ git branch -d ATLOG-1-logger

In cases where you need to also delete the remote instance of a branch, you can do so by pushing the --delete option to origin with the branch name.

$ git push origin --delete ATLOG-1-logger

This push sends the --delete option to the remote repository. If you have the appropriate privileges on that repository, Git deletes the given branch.

Git Basics Git Remotes