Chapter 3: Understanding Git Branches and Branching Workflows
Understand Git branches from scratch with clear examples. Create, switch, and merge branches without breaking your code.

Software Engineer
What does Git branching means in general?
In simple terms, Git branch is like making a copy of the current working code, which we can call v1. Instead of changing the original code directly in v1 on the same branch, we create a new branch from the existing one, and all the existing code is automatically copied to the new branch. You can then modify the code in this copied version as needed. If everything works as expected after testing, you can deploy this version as v2 in the new branch. Finally, you merge this new branch with the existing one. This is the concept of branch workflow.
- By default master is the default branch in git.
Listing Branches
git branch: to see all the list of branches.
Note: If we don't make an initial commit, then default branch will not be shown.
Note: whenever we create a new branch from existing branch, then only committed files will be cloned in new branch.
Create new branch
git branch branchname: to create the new branch.
Switching the branch
git checkout branchname: to switch any specific branch
Merging branches
note: to merge any branch, you need to checkout to default branch that is master branch, then only you can merge.
note: you are creating files on server, so whatever file you created in any specific branch, same file will be replicated in all branches irrespective of on which branch, you’ve created it.
only once you commited that file, then that file will not replicate it on all branches, it will exist only on that specific branch.
git merge branchname: combines two branches and keeps all commits exactly as they were, adding one extra merge commit.
git rebase branchname: moves your current branch’s commits on top of another branch without creating a merge commit.
Difference between rebase and merge:git merge keeps all commits and adds a merge commit by showing the branch history.git rebase rewrites your commits on top of another branch, keeping history linear with no merge commit.
Create and Checkout branch in same command
git checkout -b branchname: to create new branch and checkout to that branch with same command.
Delete branch
Note: We cannot delete a branch while we are currently on that branch.
git branch -d branchname: to delete any branch
Difference between git branch -d branchname and git branch -D branchname:
git branch -d branchname safely deletes a branch only if it’s fully merged, while git branch -D branchname force deletes it even if it’s not merged.




