Git Branches Tutorial
Git branching is an essential feature that enables developers to work on separate features, fixes, or experiments without affecting the main codebase. This tutorial covers Git branches, including creating, switching, merging, and deleting branches in a Git project.
Prerequisites
-
Git: Ensure Git is installed on your system.
git --version -
Basic Knowledge of Git: Familiarity with basic Git commands like
git init,git add, andgit commitis helpful.
1. Understanding Branches
In Git, branches represent different lines of development. The main branch is usually called main (or master in older projects). Creating a new branch allows you to make changes independently of the main branch.
2. Creating a New Branch
To create a new branch in Git, use the git branch command:
git branch <branch-name>
Example
git branch feature-branch
This command creates a branch named feature-branch based on the current state of your code.
3. Switching to a Branch
To switch to a branch, use the git checkout command:
git checkout <branch-name>
Alternatively, you can create and switch to a new branch in one command:
git checkout -b <branch-name>
Example
git checkout -b new-feature
This command creates a new-feature branch and checks it out.
4. Viewing Branches
To view all branches in your repository, use:
git branch
The active branch will be highlighted with an asterisk (*).
5. Merging Branches
Once you've completed work on a branch, you may want to merge it into the main branch.
Step 1: Switch to the Main Branch
First, switch to the main branch (or any other branch you want to merge into):
git checkout main
Step 2: Merge the Feature Branch
Use the git merge command to merge your feature branch into the main branch:
git merge <branch-name>
Example
git merge new-feature
This command merges the changes from new-feature into the main branch.
6. Resolving Merge Conflicts
If Git detects conflicting changes between branches, a merge conflict occurs. To resolve merge conflicts:
-
Open the files with conflicts in a text editor.
-
Look for conflict markers (
<<<<<<,======,>>>>>>) to see the changes. -
Edit the file to keep the desired changes.
-
Add the resolved files to staging:
git add <file-name> -
Commit the merge:
git commit -m "Resolve merge conflicts"
7. Deleting a Branch
Once a branch is merged, you can delete it to keep your repository clean.
Delete a Local Branch
To delete a branch locally, use:
git branch -d <branch-name>
Use the -D option to force delete a branch if it has unmerged changes:
git branch -D <branch-name>
Delete a Remote Branch
To delete a branch from the remote repository:
git push origin --delete <branch-name>
8. Summary of Branch Commands
| Command | Description |
|---|---|
git branch <branch-name> | Create a new branch |
git checkout <branch-name> | Switch to a branch |
git checkout -b <branch-name> | Create and switch to a new branch |
git branch | List all branches |
git merge <branch-name> | Merge a branch into the current branch |
git branch -d <branch-name> | Delete a local branch |
git push origin --delete <branch-name> | Delete a remote branch |
Summary
This tutorial introduced the basics of Git branches, including:
- Creating, switching, and viewing branches.
- Merging branches and resolving conflicts.
- Deleting branches locally and remotely.
Using branches in Git allows for isolated development, making collaboration and code management more effective.
Content Review
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewer
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.