# Branches

### Formatting

* Branch naming should be clear and use slashes e.g. routing/campground
* So you can easily search for all branches of that type: `git branch --list "routing/*"`

<table data-full-width="true"><thead><tr><th>Description</th><th>Commands</th></tr></thead><tbody><tr><td><p>List all branches</p><ul><li><code>q</code> key to exit list</li><li><code>-avv</code> more info in list (also shows hidden branches)</li></ul></td><td><p><code>git branch</code></p><p><code>git branch -avv</code></p></td></tr><tr><td>Create a new branch</td><td><code>git branch &#x3C;BRANCH_NAME></code></td></tr><tr><td>Create a new branch and checkout at the same time</td><td><code>git checkout -b &#x3C;BRANCH_NAME></code></td></tr><tr><td><p>Change to a branch</p><ul><li><code>switch</code> also works for changing branches</li></ul></td><td><p><code>git checkout &#x3C;BRANCH_NAME></code></p><p><code>git switch &#x3C;BRANCH_NAME></code></p></td></tr><tr><td><p>Checkout a specific commit rather than a whole branch</p><ul><li>This moves HEAD to the specific commit</li></ul></td><td><code>git checkout &#x3C;COMMIT_HASH></code></td></tr><tr><td><p>Relative references</p><ul><li>Move up ancestors from a specific reference point</li><li><code>^</code> moves to the first parent of the ref e.g.<code>main^</code> and <code>^^</code> to the second parent, etc.</li><li><code>^2</code> moves to the second parent if the current commit has two or more parents to move back towards</li><li>Use <code>HEAD^</code> to travel backwards as the relative reference moves with you</li><li><code>~</code> moves a specified number of commits backwards e.g. <code>HEAD~4</code></li><li>I think <code>~</code> without a number is the same as <code>^</code></li><li>The modifiers can also be chained together in a single command!</li></ul></td><td><p><code>git checkout &#x3C;REF>^</code></p><p><code>git checkout HEAD^</code></p><p><code>git checkout HEAD^2</code></p><p><code>git checkout HEAD~4</code></p><p><code>git checkout HEAD~2^2~2</code></p></td></tr><tr><td><p>Moving a branch (Branch forcing) to a new location</p><ul><li><code>-f</code> forces the command to happen</li></ul></td><td><code>git branch -f &#x3C;BRANCH_TO_MOVE> &#x3C;LOCATION_TO_MOVE></code></td></tr><tr><td><p>Merge two branches</p><ul><li>The branch name used should be the one you are not currently in</li></ul></td><td><code>git merge &#x3C;BRANCH_NAME></code></td></tr><tr><td>Delete a branch</td><td><code>git branch -d &#x3C;BRANCH_NAME></code></td></tr><tr><td>Delete branch on Remote</td><td><code>git push &#x3C;REMOTE_NAME> --delete &#x3C;BRANCH_NAME></code></td></tr><tr><td>List of changes that will be merged into the current branch</td><td><code>git log ..&#x3C;BRANCH_NAME></code></td></tr><tr><td><p>Formatting branch names</p><ul><li>Branch naming should be clear and use slashes e.g. <code>routing/campground</code></li><li>So you can easily search for all branches of that type</li></ul></td><td><code>git branch --list "routing/*"</code></td></tr><tr><td><p>Remote tracking</p><ul><li>You can make any arbitrary branch track o/main, and if you do so, that branch will have the same implied push destination and merge target as main</li><li>This means you can run git push on a branch named <code>totallyNotMain</code> and have your work pushed to the <code>main</code> branch on the remote!</li><li>There are two ways to set this property. The first is to checkout a new branch by using a remote branch as the specified ref</li><li>Another way to set remote tracking on a branch is to simply use the <code>git branch -u</code> option</li><li><p></p><ul><li><code>&#x3C;BRANCH_NAME></code> is not needed if you are already on the branch you want to use as the remote tracking reference</li></ul></li></ul></td><td><p><code>git checkout -b &#x3C;NEW_BRANCH_NAME> &#x3C;REMOTE_REF></code></p><p><code>git checkout -b totallyNotMain origin/main</code><br><br><code>git branch -u &#x3C;REMOTE_REF> &#x3C;BRANCH_NAME></code></p><p><code>git branch -u origin/main foo</code></p></td></tr><tr><td><p>Deleting an ambiguous local branch</p><ul><li>If you've made a mistake and somehow created a local branch with the same name as a remote branch, you can either rename the local branch or delete it in the local file system</li></ul></td><td><code>rm -r .git/refs/heads/&#x3C;BRANCH_TO_DELETE></code></td></tr></tbody></table>
