Post

Git Branch Cheatsheet

I’ve been using Git for a long time. Though for a long time I wasn’t using branches. I knew about them and I knew how powerful they were (or could be). I understood the concept. But you can’t use branches unless you know exactly what you’re doing with them.

So, I’ve finally taken the time and learnt about branches so I can start using them in my every day development.

I found a great YouTube video. With a great teacher. Who took it slowly. And together we learnt about branches and I created this cheatsheet based on that YouTube video.

Now im good to go. Now you are too.

Git Branch Functionality

What can you do with branches?

  • create
  • switch / checkout
  • compare
  • list
  • merge
  • publish

Head Branch

note: The Head Branch is the currently active branch and there is only ever 1

Local and Remote Branches

Mostly we work with local branches and git does not natively allow you to create remote branches.

Creating a New Branch

This will take the current situation of the code and make a branch out of it. though you will remain on your current branch.

1
git branch new-branch-name

If you want to create a branch from a previous commit you need to append the commit hash to the command

1
git branch new-branch-name COMMIT-HASH

Switch Branch

Hit tab after writtinggit checkout to see what branches you can switch to.

1
git checkout

or more specifically

1
git switch

Note: Git checkout has lots of functions tied to it.

Rename Branches

This will rename the currently checkedout branch

1
git branch -m <new-name>

Rename a non-checkedout branch

1
git branch -m other-branch branch-new

List Branches

1
git branch

Get current branch

1
git status

Branch deployment

Publish an existing local branch on a remote repo

note: its not possible to create a remote branch you must only publish/deploy an existing local branch note: The below command also establishes a relationship between the local and the remote branch

1
git push -u origin <local-branch>
  • orign is the name of the remote
  • -u tells git to establish a tracking connection which makes pushing and pulling easier

Remote and Local Branch Relationship

By default there is no relationship. But we do want the local branches to have a relationship with the remote branches and we can create one. Once the relationship has been established. Then you can use your vanilla git push and git pull git commands.

  • -u tells git to establish a tracking connection which makes pushing and pulling easier

Pull remote branch down to your local

If a branch exists on the remote but does not exist locally

1
git branch --track feature/login origin/feature/login

The first value is the name feature/login the second value is the where to get it from

Pull remote branch down to your local example 2

Here we are not setting a name for the local branch.

1
git checkout --track origin/feature/login

Pulling and Pushing Branches

Once the branch tracking has been set up properly you can just Use the native git pull and git push operations

What is Divergence?

Divergence is when your local and remote branch have different commits (some commits have not been push or some commits have not been pulled)

1
git branch -v

Will show you how current your local data is.

Deleting Branches

Most branches are not meant to live forever.

note: you cannot delete the current head branch (the currently checkedout Branch). Use switch to switch to another branch first.

1
git branch -d your-branch

Force deletion of branches

If you have commits on the branch you are about to delete. And these commits are nowhere else git will warn you that you are about to delete these commits permanently. In this case this is where you need to use the -f command to force the deletion

Delete a remote branch

1
git push origin --delete name-of-remote-branch

Merging Branches

integrating changes into your head branch from another branch

  1. Check out the branch that should receive the changes
1
git switch main
  1. Execute the “merge” command with the name of the branch that contains the desired changes
1
git merge name-of-branch-to-merge-into-head

Merge Commits

Sometimes when merging git creates its own new commit (which is a combination of both branches)

Rebasing

Rebasing is another way to merge branches. In which case rebasing does not create a new merge commit instead keeping the development history in a straight line.

Comparing Branches

compare 2 local brances

1
git log main..dev

Compare remote branch with Local

1
git log origin/main..main

More Info

more info

1
This post is licensed under CC BY 4.0 by the author.