
Scenario 1
You created a new feature branch from the master branch and continued to add new code to that branch for the feature

At the same time, new functionalities or changes were made in the master branch — commits c2 and c3. Now you want those changes to be included in the feature branch as well.

You can do this with either git merge or git rebase
If you use git merge, it will create a merge commit that combines the changes from both branches. This preserves the history of both branches, showing where they diverged and were merged

If you use git rebase, it will change the base of your feature branch to the latest commit on master and apply your changes from there onward. This essentially creates a linear history, eliminating merge commits.

Scenario 2
You have finished developing the feature and now you want to merge the changes back to master branch

If you use git merge in this case, Git will create a new merge commit that includes the history of both branches. If you do this with multiple feature branches, it can make the history look messy, with details on where the branches diverged and were merged.

If you use git rebase and fast forward merge. git rebase will move our feature branches changes to the tip of the master branch and will perform a fast forward merge. This is also used to create a clean linear history

To summarize
Git Merge
- Method: Creates a new commit that combines the changes from both branches.
- Result: Preserves the history of both branches, creating a merge commit that shows where the branches diverged and were merged.
- Use Cases:
- When you want to keep a clear record of the merge history.
- When you’re collaborating with others and want to avoid overwriting their work.
Git Rebase
- Method: Replays the commits from one branch onto another, effectively rewriting the history.
- Result: Creates a linear history, eliminating merge commits.
- Use Cases:
- When you want to clean up your project’s history by removing unnecessary merge commits.
- When you’re working on a feature branch and want to update it with the latest changes from the main branch before merging.

Comments
Very useful!! Thanks for sharing.