# Git Branches
## Notes
Branches can be considered as different copies of your files.
Often, we have one main version of the files (aka "master"/"main" branch), and each branch that attempts to change some files without overwriting the files in the main version. This offers a safe space to experiment, without messing up with files for other people (or yourself).
For example, developing a new feature can be done in a different branch, that allows you to break things or make intermediate changes without causing harm to other users.
While a branch is a safe place to change and explore, keep in mind that the ultimate goal of a branch is to be merged back into the master, once you have completed developing your feature, and made it compatible with the current version of the master. This is often happens in the (Jump:: [[Git Remotes|remote]]) version of the branch.
basic terminology:
1. Main/Master - considered as the main branch, the one that contains the "official" version of your code base
2. HEAD - is the terminology for the latest commit (or most updated version) of current branch
branching commands:
1. git branch - see list of available branches, with an Asterix next to active one.
2. *git branch my_branch_name* - creates a new branch based on the current "head" location
3. *git switch my_branch_name* - switch to a different branch
4. *git switch -c my_branch_name* - create a new branch and immediately switch to it
5. *git branch -d my_branch_name*: cant delete the branch you are currently on. To delete a branch that wasn't merged yet, use "-D" instead of "-d"
6. *git branch -M new_branch_name*: to rename the branch you are currently on
#### Switching
when you want to **switch branches with uncommit changes**, these changes will either move with you to the different branch, or you will get conflicts.
solution: stashing
*git stash* - will take all committed and uncommitted changes in current branch and save them, reverting the working directory back to the last commit
*git stash pop* - load back the *most recent* stashed changes and apply them to the current working directory.
the stash is not particular per branch, you can technically take those changes and apply them in a different branch.
when working with multiple stashes:
you can access a specific stash by:
*git stash list*
*git stash apply stash@{stash_num}*
to delete a stash:
*git stash drop stash@{stash_num}* or *git stash clear* (to delete everything)
note that when using "git stash pop" it automatically removes the stash.
#### Merging
To combine two branches together, you need to:
1. switch to the branch you will merge to (probably master)
2. *git merge branch_name*
there are "fast forward merges" where the master branch hasn't change since the new branch has been creating (only adding new commits to master).
merging a branch doesn't delete it.
In cases that are not "fast forward", i.e changes to the master happened during working on a branch, this will generate a "merge commit", which will include changes from both branches.
A more aesthetic yet dangerous way to merge is to use (Jump:: [[Git Rebasing]]), although it is advised to **avoid doing rebasing**
A common best practice is that no one works directly on the master, and every feature has a dedicated branch. Before merging a branch into master, it is common to do a *pull request*, which means that someone else has to go over the branch's changes and see whether they need modifications before combining them into the master.
## Overview
🔼Topic:: [[Git (MOC)]]
◀Origin:: [[Git and GitHub (course)]]
🔗Link:: [documentation](https://git-scm.com/docs/git-branch)