> ## Content Index
> Fetch the complete content index at: https://www.techielass.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Git branches - 14 days of Git
- URL: https://www.techielass.com/git-branches/
- Published: 2022-09-28T07:01:46.000Z
- Updated: 2024-07-02T11:52:14.000Z
- Description: Join me as I look at working with Git branches in more detail and the commands that will help.
- Author: Sarah Lean
- Tags: 14daysofgit, Git for everyone, Git

It is Day 9 of my learning journey into Git. So far on my learning journey has taken me on the following path: 

- [What is version control?](https://www.techielass.com/what-is-version-control)
- [What is Git?](https://www.techielass.com/what-is-git)
- [Installing Git](https://www.techielass.com/installing-git/)
- [Basic Git commands to get started](https://www.techielass.com/basic-git-commands-to-get-started)
- [Inspect a Git repository](https://www.techielass.com/inspect-a-git-respository)
- [Git File Operations](https://www.techielass.com/git-file-operations)
- [Undoing commits and changes](https://www.techielass.com/undoing-commits-changes)
- [Rewriting history](https://www.techielass.com/rewriting-git-history/)

Today I am going to dive into what branches are and how they can be used. 

### What are Git branches?

Branches are an everyday part of the process when using Git. Effectively they are a point to your changes. You might create a new branch when you want to work on a new feature, or bug fix and keep those changes completely separate until you are ready to release them. 

There are a few commands that you can use to work with branches, let's take a look at them. 

### Create a new branch

If you have a copy of a repository and you want to make changes then creating a new branch is best practice. Do create a new branch you can run the command: 

```bash
git checkout -b newbranchname

```

You can now start to work on your changes and commit them there. 

### See what branch you are on

When you are working within a repository, and you want to check what branch you are on you can use the following command: 

```bash
git status

```

### Switch to a local branch

If you have a branch within your local repository that you've created then you can easily switch to it using the following command: 

```bash
git checkout branchname

```

### Switch to a branch that's came from a remote repo

If you have cloned a Git repository from a remote location and it's come with a bunch of branches, then you can use the following command:

```bash
git checkout --track origin/branchname

```

### Push a branch

When you create a branch on your local copy of the repository it won't automatically create within the remote location. When you come to push your changes to that remote location you can't just use the *git push* command you need to use a slightly different command, ***either*** of these: 

```bash
git push -u origin branchname
git push -u origin HEAD

```

Referencing HEAD saves you from having to type out the exact name of the branch. 

![git push -u origin HEAD command](https://storage.ghost.io/c/08/96/08960c71-63a2-449b-91b1-8d4628166dd2/content/images/2022/09/image-5.png)

git push -u origin HEAD command

Of course, if your branch already exists in the remote location, you can just run *git push*. 

### Merge a branch

So, you've created a branch, and you are ready to merge that branch into the main one, ready for production or the next step in your development phrase. How can you do it?

The first thing you need to do is switch to the branch you want to merge your changes into. In this example I want to merge my changes from the branch "new-feature-from-sarah" into my main branch. So I type the command: 

```bash
git checkout main

```

Now I am in the branch I want to merge my changes into I type:  

```bash
git merge new-feature-from-sarah

```

*When you issue this command you may receive merge conflicts, we will be looking at how to deal with them in* [*Day 13 of 14 days of Git*](https://github.com/weeyin83/14daysofgit?ref=techielass.com)*.* 

I now have to push this merge from my local repository to my remote repository, I do that with the command: 

```bash
git push

```

### Clone a specific branch

### Delete a local branch

If you are finished with a local branch, either you've merged your changes or you've decided that the changes aren't right. Then you can use one of two commands. Both commands are very similar but there is a subtle difference. 

If you want to delete a branch that has already been merged you can use the command: 

```bash
git branch -d branchname

```

If you want to delete a local branch regardless of whether it has been merged or not, then the command to use is: 

```bash
git branch -D branchname

```

There is a subtle difference, the capital D is really a shortcut for --delete --force. 

### Command line or GUI?

Working with branches within a command line seems straight forward, but it could be tricky if you forget which branch you are working in and merge or push changes to the wrong location. 

It may be easier to work with branches within a graphical user interface (GUI) such as [Visual Studio Code](https://code.visualstudio.com/?WT.mc%5Fid=AZ-MVP-5004737&ref=techielass.com) or [GitKraken](https://www.gitkraken.com/invite?referralCode=pYx7cNys&ref=techielass.com). 

I think however, it's important to understand what a GUI is doing though, so learning these commands is important and working through them. 

## 14 days of Git

Tomorrow will see me on Day 10 of my [14 days of Git](https://www.techielass.com/tag/14daysofgit) learning journey and I will be looking at merging! Be sure to [subscribe](https://www.techielass.com/newsletter) and join us for that step in the learning journey!

You can follow along here: [https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit?ref=techielass.com)