Step-by-Step Guide to Creating and Merging Feature Branches in Git
"Step-by-Step Guide to Creating and Merging Feature Branches in Git"
Start by switching to the master branch and pulling the latest changes to ensure it's up-to-date. Create a new feature branch from the master branch using git checkout -b branch-name, and switch to it. Make your changes, then stage and commit them with meaningful messages. Push the new branch to the remote repository using git push -u origin branch-name. Finally, create a Pull Request to merge the changes into the master branch, and optionally delete the feature branch after merging.
Flow and Commands to Create and Work with a New Branch
- Start by Checking Out the Master (Main) Branch
Ensure you’re on the master (or main) branch and it's up-to-date.
Command:
git checkout master # Switch to the master branch
git pull origin master # Pull the latest changes from the remote master branch
- Create a New Branch and Switch to It
Use the git checkout -b command to create a new branch and switch to it in one step.
Command:
git checkout -b feature/your-branch-name
Example:
git checkout -b feature/add-login
What this does:
Creates a new branch called feature/add-login from the master branch.
Automatically switches you to the new branch.
- Verify That You Are on the New Branch
Use the git branch command to see which branch you’re currently working on.
Command:
git branch
The branch with the * is your current branch.
- Work on Your Changes
Make changes to the files in your project and test your code locally.
- Stage and Commit Your Changes
Once your changes are ready, stage and commit them.
Commands:
git add . # Stage all changes
git commit -m "Your commit message here"
Example:
git commit -m "Added user login functionality"
- Push the New Branch to the Remote Repository
Push the newly created branch to the remote repository so others can access it.
Command:
git push -u origin feature/your-branch-name
Example:
git push -u origin feature/add-login
What this does:
Pushes the branch to the remote repository.
Sets up tracking between your local branch and the remote branch for future updates.
- Create a Pull Request
Go to your Git hosting platform (e.g., GitHub, GitLab, Bitbucket).
Open a Pull Request (PR) from feature/your-branch-name into master (or main).
Add a description and submit the PR for review.
- Merge the Branch into Master
After your PR is approved, merge it into the master branch.
This can be done via the platform’s interface or the command line.
- Delete the Feature Branch (Optional)
After the branch is merged, you can delete it locally and remotely to keep the repository clean.
Commands:
git branch -d feature/your-branch-name # Delete the local branch
git push origin --delete feature/your-branch-name # Delete the remote branch
Full Command Flow Example
Here’s how the commands might look for a real use case:
git checkout master # Switch to master branch
git pull origin master # Pull the latest changes from master
git checkout -b feature/add-login # Create and switch to the new branch
Make your changes in the code
git add . # Stage all changes
git commit -m "Added login functionality" # Commit the changes
git push -u origin feature/add-login # Push the new branch to remote
Source: View source