This content originally appeared on DEV Community and was authored by Wycliffe A. Onyango
Managing Branches and Merging in Git
In this exercise, I worked on the official project repository located at /opt/official.git
. The objective was to create a new branch, add a file, merge changes back into the master branch, and push everything to the remote repository.
This task ensured proper branching workflows, safe integration of new changes, and clean synchronization with the remote repository. Below are the detailed steps I followed to complete the exercise.
Note: I used sudo
for this exercise since the respective directories were owned by root
rather than the user natasha
on the storage server. Alternatively, chown
could be used to change ownership.
1. Navigate to the Cloned Repository
The project repository had already been cloned under /usr/src/kodekloudrepos/official
. I moved into this directory to begin the work:
cd /usr/src/kodekloudrepos/official
2. Ensure Master Branch is Up to Date
Before creating a new branch, I switched to the master branch and updated it with the latest changes from origin:
sudo git checkout master
sudo git pull origin master
3. Create and Switch to New Branch
A new branch named xfusion was created from master:
sudo git checkout -b xfusion
4. Copy Required File into Repository
The provided index.html
file, located at /tmp/index.html
, was copied into the repository root directory:
sudo cp /tmp/index.html .
5. Stage and Commit the File
The file was staged and committed into the new branch:
sudo git add index.html
sudo git commit -m "Add index.html file"
6. Push the New Branch to Origin
The xfusion
branch was pushed to the remote repository to keep origin updated:
sudo git push origin xfusion
7. Merge the Branch into Master
I switched back to master and merged the changes from xfusion
:
sudo git checkout master
sudo git merge xfusion
8. Push Master to Origin
Finally, the updated master branch was pushed back to the origin:
sudo git push origin master
Outcome
- The
index.html
file is now part of the repository in both the xfusion branch and the master branch. - Both branches were successfully pushed to the remote origin.
- The task validated Git best practices: creating feature branches, committing changes safely, and merging them back into the main branch.
This content originally appeared on DEV Community and was authored by Wycliffe A. Onyango