# [[Branching in Git]]
![[Branching in Git.svg]]
# What's a branch?
Branches are one of [[Git]]'s defining features. A branch is a line of work that deviates from the main codebase at an identifiable point. Branches allow different parts of the code to be worked on in parallel. If multiple developers worked on the same code simultaneously on the same branch, too many breaking changes might be introduced. To prevent this, a branch takes a snapshot of the "safe" code and allows a developer to test their changes without affecting anybody else's work.
# Working with branches
## Create new branch
To create a new branch on the remote repo, do so within the GitHub web UI.
![[github-new-branch-ui.png]]
To create a new branch locally:
```shell
git branch newbranchname
```
## Use branch
`git checkout branchname`
## Create new branch and use it in one command
^1b2f9b
`git checkout -b newbranchname`
## Delete local branch
`git branch -d branchname`
In some cases, your branch might not be able to be deleted because it comtains commits that hasn't yet been merged into any other branches. To go ahead and do it anyway, you can use this command:
`git branch -D branchname`
## Delete remote branch
The following command will delete both the local branch as well as the remote branch:
```bash
git push origin --delete branchname
```
## Which branch am I using?
`git branch`
## Pulling a PR branch locally
If you want to test out a [[Pull Request]] locally:
Create a new branch locally that has the same name as the PR branch: `git checkout -b feat/fixes`
Pull changes from the branch:
`git pull --set-upstream origin feat/fixes`
## Set local branch to track remote branch
If you've already created a branch on the remote repo, you can set up your local branch to track the branch on the remote repo by running this command:
```shell
git branch --set-upstream-to=origin/newbranch newbranch
```
Then, test it out by doing a:
```shell
git pull
```
That should return `Already up to date` if both branches are empty.
%%
# Excalidraw Data
## Text Elements
## Drawing
```json
{
"type": "excalidraw",
"version": 2,
"source": "https://github.com/zsviczian/obsidian-excalidraw-plugin/releases/tag/2.1.4",
"elements": [
{
"id": "4y8R7iOA",
"type": "text",
"x": 118.49495565891266,
"y": -333.44393157958984,
"width": 3.8599853515625,
"height": 24,
"angle": 0,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 1,
"opacity": 100,
"groupIds": [],
"frameId": null,
"roundness": null,
"seed": 967149026,
"version": 2,
"versionNonce": 939059582,
"isDeleted": true,
"boundElements": null,
"updated": 1713723615080,
"link": null,
"locked": false,
"text": "",
"rawText": "",
"fontSize": 20,
"fontFamily": 4,
"textAlign": "left",
"verticalAlign": "top",
"containerId": null,
"originalText": "",
"lineHeight": 1.2
}
],
"appState": {
"theme": "dark",
"viewBackgroundColor": "#ffffff",
"currentItemStrokeColor": "#1e1e1e",
"currentItemBackgroundColor": "transparent",
"currentItemFillStyle": "solid",
"currentItemStrokeWidth": 2,
"currentItemStrokeStyle": "solid",
"currentItemRoughness": 1,
"currentItemOpacity": 100,
"currentItemFontFamily": 4,
"currentItemFontSize": 20,
"currentItemTextAlign": "left",
"currentItemStartArrowhead": null,
"currentItemEndArrowhead": "arrow",
"scrollX": 583.2388916015625,
"scrollY": 573.6323852539062,
"zoom": {
"value": 1
},
"currentItemRoundness": "round",
"gridSize": null,
"gridColor": {
"Bold": "#C9C9C9FF",
"Regular": "#EDEDEDFF"
},
"currentStrokeOptions": null,
"previousGridSize": null,
"frameRendering": {
"enabled": true,
"clip": true,
"name": true,
"outline": true
}
},
"files": {}
}
```
%%