Git and GitHub – Merge and Pull (Part-13)

Merge and Pull in Git

Merge

Merge is a command used to combine the changes from one branch into another. It is typically used to integrate changes from a feature branch back into the main branch (e.g., main or master).

  • Merging a Branch
git checkout main
git merge my-feature-branch
  • This command switches to the main branch and merges the changes from my-feature-branch into main.

Fetch + Merge = Pull

Pull is a command that combines fetch and merge. It is used to update your local branch with changes from a remote branch.\\

Fetch:

  • The git fetch command downloads commits, files, and refs from a remote repository into your local repository. It updates your remote-tracking branches but does not merge changes into your local branches.
git fetch origin

Merge:

  • After fetching, you can merge the changes into your local branch.
git merge origin/main

Pull:

  • The git pull command is a combination of fetch and merge. It fetches changes from the remote branch and automatically merges them into your current local branch.
git pull origin main

Example Workflow

Scenario: Working with Local and Remote Branches

Clone a Repository:

    git clone https://github.com/your-username/your-repo.git
    cd your-repo
    

    Create and Switch to a New Local Branch:

    git checkout -b my-feature-branch
    

    Make Changes and Commit:

    # Make changes to your files
    git add .
    git commit -m "Add new feature"
    

    Push the Local Branch to Remote:

    git push origin my-feature-branch
    

    Create a Pull Request on GitHub:

    • Go to your repository on GitHub.
    • Create a pull request to merge my-feature-branch into main.

    Fetch and Merge Changes from Remote:

    git fetch origin
    git checkout main
    git merge origin/main
    

      Alternatively, Pull Changes from Remote:

      git pull origin main
      
      • Merge: Combines changes from one branch into another.
      • Fetch: Downloads changes from a remote repository without merging them.
      • Pull: Combines fetch and merge to update your local branch with changes from a remote branch.

      Related Posts

      Git and GitHub – GitHub Overview (Part-15)

      1. Github Description: 2. Org (Organization) Description: Example: 3. Team Description: Example: 4. Auth (Authentication) Description: Examples: OAuth Authentication: Personal Access Tokens: SSH Key Authentication: 5. Action…

      Read More

      Git and GitHub – Git Log Explanation (Part-14)

      Git Log Explanation Explanation of Each Part 1. Commit Hash 2. References (Branches and Tags) 3. Commit Message Simplified Breakdown of Each Line Summary The git log…

      Read More

      Git and GitHub – Types of branch (Part-12)

      Local and Remote Branches in Git Local Branch A local branch is a branch that exists in your local Git repository. You create and manage local branches…

      Read More

      Git and GitHub – Git Undoing, Conflict Resolution, Cherry Picking, Stash (Part-11)

      Undoing changes in Git can be approached in several ways, depending on what you need to achieve. Here, we’ll cover the most common scenarios and commands for…

      Read More

      Git and GitHub – Understanding Git Concepts (Part-10)

      Understanding Git Concepts 1. Git Branching and Merging Branching allows you to create a separate line of development. It’s useful for working on different features or fixes…

      Read More

      Git and GitHub – How to push/pull and clone files between Local machine and GitHub (Part-9)

      Git and GitHub: Step-by-Step Guide Let’s explore how to interact with Git and GitHub, focusing on pushing, pulling, and cloning files between your local machine and GitHub….

      Read More
      guest
      0 Comments
      Oldest
      Newest Most Voted
      0
      Would love your thoughts, please comment.x
      ()
      x