Categories
Working with Feature Branches in Git
Feature branches act as a container for new functionality you’re adding or changes you’re making to a project while keeping your main branch untouched. They help keep your modifications organized and separated from the main working code base (usually the “main” or “master” branch) until you’re ready to integrate them.
Create a New Feature Branch, Make Changes, Commit, Push
- Create a New Feature Branch:
- Command:
git checkout -b <branch-name>
- Explanation: This command creates a new branch and switches you to it.
<branch-name>
should be descriptive, based on the feature you’re developing, e.g.,add-user-authentication
. - Example:
git checkout -b add-user-authentication
- Tip: If you have already made changes to your code (created the feature) before creating the branch, that’s fine. Nothing happens to your code at this point. I usually just create whatever feature I need and then, afterwards, run the
git checkout -b <branch-name>
command and follow the steps below.
- Command:
- Make Changes and Commit:
- Command:
git commit <file-name> -m "<descriptive-message>"
- Explanation: After making your changes in the new feature branch, you need to commit them. This command stages your changes (
<file-name>
refers to the files you’ve modified, e.g.,file.php
) and commits them with a message describing what you’ve done. - Example:
git commit file.php -m "Add user authentication methods"
- Command:
- Push Feature Branch to Remote Repository:
- Command:
git push -u origin <branch-name>
- Explanation: This command pushes your feature branch to the remote repository (like GitHub or GitLab) so it can be accessed by others, reviewed, or stored as a backup. The
-u
flag sets the upstream, meaning future pushes on this branch can just usegit push
without additional arguments. - Example:
git push -u origin add-user-authentication
- Command:
Pull Requests, Fetching Updates, Branch Cleanup
- Pull Requests: Once your feature is complete and tested, you might open a “pull request” (or “merge request”) on platforms like GitHub, GitLab, or Bitbucket. This action seeks permission to merge your feature branch into the main codebase and invites team members to review your code.
- Fetching Updates: If the main branch has been updated by others while you were working on your feature, you’ll want to pull those changes into your feature branch before merging. You can do this by switching to your main branch (
git checkout main
), pulling the latest changes (git pull
), and then merging them into your feature branch (git checkout <branch-name>
followed bygit merge main
). - Branch Cleanup: After your feature branch has been merged, it’s a good practice to delete the feature branch from the remote repository (
git push origin --delete <branch-name>
) and your local repository (git branch -d <branch-name>
).
Other Useful Commands
- List All Branches:
- Command:
git branch
orgit branch --list
- Explanation: Running
git branch
without arguments lists all branches in your local repository. The branch marked with an asterisk (*) is the branch you’re currently on. This doesn’t show branches on the remote repository; for that, you would usegit branch -r
.
- Command:
- Switching Branches:
- Command:
git checkout <branch-name>
- Explanation: This command switches you from the branch you’re currently on to the branch specified by
<branch-name>
. Ensure you’ve committed or stashed changes in your current branch before switching, or you might face conflicts. - Example: If you’re on the
develop
branch and want to switch back to themain
branch, you would typegit checkout main
.
- Command:
- Check Current Branch:
- Command:
git branch --show-current
orgit symbolic-ref --short HEAD
- Explanation: The
git branch --show-current
command (available since Git version 2.22) displays the name of the current branch. Alternatively,git symbolic-ref --short HEAD
provides the same information; these commands are helpful if you’re scripting or need the branch name for automation.
- Command:
- Creating a New Branch and Switching to It:
- Command:
git checkout -b <new-branch-name>
- Explanation: This combines the branch creation and checkout steps. It creates a new branch and switches you to it in one command.
- Example:
git checkout -b new-feature
- Command:
- Stashing Changes Before Switching Branches:
- Command:
git stash
- Explanation: If you’re working on a branch but need to switch to another branch without committing the current state of your work (for example, if it’s incomplete), you can use
git stash
to save your changes temporarily. You can come back to them later by usinggit stash pop
after you’ve switched back to your original branch.
- Command:
- Pulling the Latest Changes for a Branch:
- Command:
git pull origin <branch-name>
- Explanation: Before you switch to a branch you plan to work on, especially if others are also working on the same repository, you might want to pull the latest changes from the remote repository to ensure you’re working on the most recent version of the branch.
- Example:
git pull origin main
- Command:
Create a new branch on top of another
Creating a new branch based on another branch (other than the one you’re currently on) comes handy when you want to extend or add features to an existing feature.
Here’s how you do it:
- Switch to the Base Branch:
- Command:
git checkout <base-branch-name>
- Explanation: First, you need to switch to the branch that will serve as the base for your new feature branch. Replace
<base-branch-name>
with the name of the branch you want your new branch to be based on. This could be any existing branch likedevelop
,feature-x
, etc. - Example:
git checkout anotherbranch
- This command switches you to the
anotherbranch
branch.
- This command switches you to the
- Command:
- Pull the Latest Changes:
- Command:
git pull origin <base-branch-name>
- Explanation: It’s a good practice to ensure you’re working with the most up-to-date version of the branch. This command pulls the latest changes from the specified branch in the remote repository (assuming you’re collaborating with others and they might have pushed changes).
- Example:
git pull origin anotherbranch
- This ensures that your local
anotherbranch
is up-to-date withanotherbranch
on the remote repository (origin).
- This ensures that your local
- Command:
- Create a New Feature Branch Based on Base Branch:
- Command:
git checkout -b <new-feature-branch>
- Explanation: Now that you’re on the updated base branch, you can create a new branch. This will be your feature branch, and it will contain all the commits from the base branch at the point in time when you create it.
- Example:
git checkout -b feature
- This creates a new branch called
feature
based on the current state ofanotherbranch
.
- This creates a new branch called
- Command:
- Verify Your Current Branch:
- Command:
git branch --show-current
- Explanation: After creating your new branch, it’s often good to double-check you’re on the correct branch before you start making changes. This command will display the branch you’re currently on, which should be your new feature branch.
- Example: It should return
feature
, indicating that you’re now on thefeature
branch.
- Command:
Now, you can start working on your new feature. The new branch contains the history of the base branch, plus any changes you’re about to make.