Skip to main content

What is Git Merge,Rebase,squash,cherrypick

 

Understanding Git Operations: Rebase, Merge, Squash, and Cherry-Pick

Git is a versatile tool for version control, allowing multiple developers to work on the same project without conflicts. Among its many features, Git provides several methods for integrating changes between branches, including rebase, merge, squash, and cherry-pick. Each of these methods serves a different purpose and is suited to different workflows. In this article, we'll explore the differences between these commands, complete with examples and log graphs to illustrate their effects.

1. Git Merge

Definition: Merging is the most common way to integrate changes from one Git branch into another. It combines the histories of the merged branches, preserving the context of each branch's changes relative to the main project.

Example: Suppose you have two branches: main and feature. The feature branch has diverged with additional commits that are not in main. If you want to incorporate these changes into main, you would perform a merge.

bash
git checkout main git merge feature

Log Graph:

markdown
* c8 (main) Merge branch 'feature' |\ | * c7 (feature) Add feature | * c6 Enhance feature * | c5 Edit README * | c4 Fix bug

2. Git Rebase

Definition: Rebasing is a process to move or "rebase" a series of commits to a new base commit. Rebase makes it easier to understand the history by creating a linear sequence of commits.




Example: If the feature branch is based on an outdated commit of main, you can rebase it onto the latest main branch commit.

bash
git checkout feature 
git rebase main

Log Graph:

markdown
* c9 (feature) Enhance feature 
* c8 Add feature 
* c7 (main) Fix bug 
* c6 Edit README

3. Git Squash

Definition: Squashing in Git is the process of combining multiple commits into a single commit. This is useful for keeping your history clean and understandable.

Example: Suppose you have made several commits in the feature branch that should be combined into one before merging into main.

bash
git checkout feature 
git rebase -i main

In the interactive rebase screen, you can choose to squash the commits into one.

Log Graph:

scss
* c5 (feature) Implement feature (squashed from c2, c3, c4) 
* c1 (main) Initial commit

4. Git Cherry-Pick

Definition: Cherry-picking in Git allows you to select a specific commit from one branch and apply it onto another branch. This is useful for applying bug fixes or other single-commit changes without merging a whole branch.

Example: You want to apply commit c3 from the feature branch to the main branch.

bash
git checkout main git cherry-pick c3

Log Graph:

markdown
* c6 (main) Apply feature enhancement from feature branch 
* c5 Fix bug 
* c4 Edit README

Comments

Popular posts from this blog

DevOps Vs DevSecOps

   DevOps and DevSecOps are two methodologies that have gained traction in the IT industry for streamlining software development and deployment. However, their approach to security and operations differs, making each suitable for different types of projects and organizational needs. Let's explore DevOps versus DevSecOps with a real-time example, focusing on their distinctions, integration, and practical applications. DevOps: The Foundation DevOps is a cultural and professional movement that emphasizes collaboration and communication between software developers and other IT professionals while automating the process of software delivery and infrastructure changes. It aims to shorten the development life cycle and provide continuous delivery with high software quality. Core Principles: Continuous Integration and Continuous Deployment (CI/CD): Automate building, testing, and deployment of applications. Collaboration: Breaking down silos between teams (developers, IT operations...

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines is a robust way to automate deployment processes and integrate continuous integration and deployment (CI/CD) practices into your workflow. This guide will walk you through the setup of an Azure DevOps pipeline to deploy a Node.js application from GitHub or Azure Repos to Azure App Services. Prerequisites Before you begin, ensure you have the following: An Azure account. You can sign up for a free account here . A GitHub or Azure Repos account with your Node.js project. An Azure DevOps account. Create one here if you don't have it. Step 1: Prepare Your Node.js Application Make sure your Node.js application is ready and includes a package.json file in the root. This file is crucial as it contains dependency information and scripts needed for your application. Step 2: Create an Azure Web App Log into Azure Portal: Visit https://portal.azure.com . Create a Web App: Click on "Create a resource". ...

Git Cheat Sheet

  Git Cheat Sheet Category Command Description Setup git config --global user.name "[name]" Set a name that will be attached to your commits and tags. git config --global user.email "[email]" Set an email that will be attached to your commits and tags. git init Initialize a new git repository in the current directory. git clone [url] Clone a repository into a new directory. Stage & Snapshot git status Show modified files in the working directory, staged for your next commit. git add [file] Add a file as it looks now to your next commit (stage). git reset [file] Unstage a file while retaining the changes in the working directory. git diff Show diff of what is changed but not staged. git diff --staged Diff of what is staged but not yet committed. git commit -m "[message]" Commit your staged content as a new commit snapshot. Branch & Merge git branch List all of the branches in your repo. git branch [name] Create a new branch at the current commit. gi...