Wednesday, December 25, 2024

Git Operations: A Comprehensive Guide | github

 

Git Operations: A Comprehensive Guide

Git is an essential tool for version control, widely used by developers. In this guide, we'll walk through common Git operations that every developer should know. Bookmark this page as we’ll continue adding more operations in the future.


Combine All Pushed Commits into One Method 1

To clean up your commit history and combine multiple commits into a single one, follow these steps:


1. Count the total commits on the live branch

command: git log 
description: this will show all the commits on git you just need to count where is your branch name

2. Rebase the last X commits:

command: git rebase -i HEAD~6

description: instead of 6 you need to write a number of commits that you found in step 1

3. Edit the commits A terminal console will appear with all the commits labeled "pick."

  • Press i to enter insert mode.
  • Replace the word pick with squash for all but not for the first commit.
  • 4. Save and exit:

    Press Esc, then type :wq and hit enter.

    5. Push the changes:

    git push origin your-branch-name --force 

    Combine All Pushed Commits into One Method.2(Recomanded)

    command: git log 
    description: this will show all the commits on git you just need to count where is your branch name

    • git reset --soft head~(number of commits)
    • git stash
    • git checkout master and delete your old branch
    • git pull in master
    • git checkout -b branch with same name that we deleted
    • git stash apply (then solve conflict if any)
    • git push with --force

    How to Fetch a Remote Branch to Local

    If you need to fetch a remote branch and work on it locally, follow these steps:

    Step No 01: Fetch the branch: 

    git fetch origin
    Step No 02: Check out the branch:
    git checkout branch-name-that-you-need-to-checkout
     

    How to Rename a Branch Locally

    Sometimes you might want to rename a local branch.Here's how you can do it:

    Step No 01: Checkout the branch you want to rename
    git checkout branch-name
    Step No 02: Rename the branch git branch -m new-branch-name
    Step No 03: Delete the old branch from the remote git push origin --delete old-branch-name
    Step No 04: Push the renamed branch to the remote git push origin new-branch-name
    Step No 05: Set the upstream for the new branch git push --set-upstream origin new-branch-name

    Previous Post
    Next Post

    post written by:

    0 comments: