How to work with branches in Git?
Branches are the foundation of Git. They allow parallel development without interfering with the main line.
Creating a branch: git branch <name> creates a new branch but doesn't switch to it. The command git checkout -b <name> (or git switch -c <name> in newer Git versions) creates a branch and immediately switches to it. To create a branch from a specific commit: git checkout -b <name> <commit-hash>.
Viewing branches: git branch shows local branches, git branch -a shows all including remote. The current branch is marked with an asterisk. The command git branch -v shows the last commit of each branch.
Switching: git checkout <name> or git switch <name>. To switch to a remote branch: git checkout -b <name> origin/<name>.
Deleting a branch: git branch -d <name> deletes a branch, but only if it's merged with the current one. The command git branch -D <name> deletes forcibly, even if there are unmerged changes. To delete a remote branch: git push origin --delete <name>.
Merging: switch to the target branch (git checkout main), then git merge <feature-branch>. If there are no conflicts, Git will create a merge commit or do a fast-forward.
Renaming: git branch -m <new-name> renames the current branch. For remote: rename locally, then git push origin -u <new-name> and delete the old remote branch.
Good practice: name branches by scheme like feature/short-description, bugfix/issue-number, hotfix/description. This helps the team understand the branch's purpose.