Pushing and Pulling Changes with GitHub
Learn how to push local commits to a remote GitHub repository (`git push`) and pull changes from GitHub to your local machine (`git pull`).
Learn Git and GitHub: Branches
What are Branches?
In Git, a branch is essentially a lightweight movable pointer to a commit. Think of it as a parallel timeline for your project. Branches allow you to work on new features, bug fixes, or experiments without affecting the main codebase (often called the main
or master
branch).
Imagine your project's history as a tree. The main
branch is the trunk. You can create branches (smaller limbs) off the trunk to work on different features, and then merge those branches back into the trunk when they're ready. This keeps the main codebase stable and allows for parallel development.
Using branches is crucial for:
- Feature Development: Isolating new features to prevent breaking the main code.
- Bug Fixing: Working on bug fixes without disrupting ongoing development.
- Experimentation: Trying out new ideas without risking the stability of the project.
- Collaboration: Allowing multiple developers to work on different aspects of the project simultaneously.
How to Create and Use Branches
Creating a Branch
To create a new branch in Git, use the git branch
command followed by the name you want to give to your branch. It's good practice to name your branches descriptively, reflecting what you're working on.
git branch my-new-feature
This command creates a new branch named my-new-feature
, pointing to the same commit as your current branch. It does not switch you to that branch.
Switching Between Branches
To switch to a different branch, use the git checkout
command.
git checkout my-new-feature
This command switches your working directory to the my-new-feature
branch. Any changes you make will now be tracked on this branch.
Creating and Switching in One Step
The git checkout -b
command combines creating and switching to a new branch.
git checkout -b my-new-feature
This is a shorthand way to create and immediately switch to the new branch.
Making Changes and Committing
Once you're on your branch, make your changes, add them to the staging area, and commit them as usual:
git add .
git commit -m "Implemented the initial version of my new feature"
These changes are now recorded on the my-new-feature
branch and will not affect the main
branch until you merge them.
Listing Branches
To see a list of all your branches, use the git branch
command.
git branch
The branch you are currently on will be marked with an asterisk (*).
Merging Branches
When you're finished working on your branch and want to incorporate the changes into the main
branch (or another branch), you need to merge it. First, switch to the target branch (the one you want to merge into):
git checkout main
Then, use the git merge
command followed by the name of the branch you want to merge.
git merge my-new-feature
This will attempt to merge the changes from my-new-feature
into the main
branch. If there are no conflicts, Git will automatically perform the merge. If there are conflicts, you'll need to resolve them manually (see below).
Resolving Merge Conflicts
Merge conflicts occur when Git cannot automatically determine how to combine changes from two branches. This usually happens when the same lines of code have been modified differently in both branches.
When a conflict occurs, Git will insert special markers into the affected files:
<<<<<<< HEAD
// Changes from the current branch (main)
=======
// Changes from the branch being merged (my-new-feature)
>>>>>>> my-new-feature
You need to manually edit the file, decide which changes to keep (or combine them), and remove the conflict markers. Once you've resolved the conflicts, add the modified file to the staging area and commit the changes.
git add .
git commit -m "Resolved merge conflicts"
Deleting a Branch
Once a branch has been merged and you no longer need it, you can delete it. It's good practice to keep your repository clean.
git branch -d my-new-feature
If the branch hasn't been fully merged, Git will prevent you from deleting it unless you force the deletion:
git branch -D my-new-feature
The uppercase -D
forces the deletion, but be careful using it – you might lose work if you haven't properly merged or backed up your changes.