Skip to content

Instantly share code, notes, and snippets.

@kilip
Last active September 26, 2024 01:42
Show Gist options
  • Select an option

  • Save kilip/e333952c30a73f077a868da7c3859c4e to your computer and use it in GitHub Desktop.

Select an option

Save kilip/e333952c30a73f077a868da7c3859c4e to your computer and use it in GitHub Desktop.

Revisions

  1. kilip revised this gist Sep 26, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions git-join-commit.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    ---

    ## Git Merge Commits Tutorial
    In this tutorial we will merge several commits into one single commit

    ### 1. Change to Your Working Directory

  2. kilip revised this gist Sep 26, 2024. 1 changed file with 46 additions and 19 deletions.
    65 changes: 46 additions & 19 deletions git-join-commit.md
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,60 @@
    Change to your working directory
    ---

    ## Git Merge Commits Tutorial

    ### 1. Change to Your Working Directory

    Navigate to your project directory:

    ```sh
    cd path/to/project
    ```

    list your commit:
    ### 2. List Your Commits

    View the commit history in a concise format:

    ```sh
    git log --oneline
    ```

    this will display output like:
    ```sh
    This command will display output like:

    ```
    d5eb363 (HEAD) wip
    323b361 wip
    066d50c wip
    f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    defad49 (upstream/main, origin/main, origin/HEAD, main) Merge pull request #13 from kilip/strapi
    ```
    Count the commits you want to merge from `(HEAD)` commit.
    For example, if we want to merge commit from commit from `feat(strapi-bridge)` to `(HEAD) wip`
    then the count should be **4**.

    start rebase by using ```git rebase -i HEAD~4``` where 4 is commit count in the example above:
    ### 3. Count Commits to Merge

    Identify how many commits you want to merge starting from `(HEAD)`. For instance, if you wish to merge from `feat(strapi-bridge)` to `(HEAD) wip`, count the commits, which in this example would be **4**.

    ### 4. Start the Rebase

    Initiate an interactive rebase with the following command:

    ```sh
    git rebase -i HEAD~4
    ```

    This command will opening your git editor, with output like this:
    ```sh
    This opens your Git editor, displaying something like:

    ```
    pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    pick 24a1c6b wip
    pick 8816256 wip
    pick b0aa58b wip
    # Rebase defad49..d5eb363 onto defad49 (4 commands)
    ....
    ```

    Change ```pick``` to ```s``` (squash):
    ### 5. Modify the Commit Commands

    Change `pick` to `s` (squash) for the commits you want to combine:

    ```sh
    pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    s 24a1c6b wip
    @@ -45,8 +63,12 @@ s b0aa58b wip
    # Rebase defad49..d5eb363 onto defad49 (4 commands)
    ```
    Save your changes which will open your git editor with output like this:
    ```sh

    ### 6. Save Changes

    After saving, your editor will show a message like this:

    ```
    # This is a combination of 4 commits.
    # This is the 1st commit message:
    feat(strapi-bridge): integrated openapi-fetch :rocket:
    @@ -59,20 +81,25 @@ wip
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    ...etc
    ```
    Remove or keep the commit message, for example if I want to remove all wip message:
    ```sh

    ### 7. Edit the Commit Message

    Decide whether to keep or remove the commit messages. For example, to remove all `wip` messages, you would edit it to:

    ```
    # This is a combination of 4 commits.
    # This is the 1st commit message:
    feat(strapi-bridge): integrated openapi-fetch :rocket:
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    ...etc
    ```

    Save your changes, and **force push** your commits to remote repository:
    ### 8. Finalize and Push Changes

    Save your changes and force push your commits to the remote repository:

    ```sh
    git push origin main --force
    ```
  3. kilip created this gist Sep 26, 2024.
    78 changes: 78 additions & 0 deletions git-join-commit.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    Change to your working directory
    ```sh
    cd path/to/project
    ```

    list your commit:
    ```sh
    git log --oneline
    ```

    this will display output like:
    ```sh
    d5eb363 (HEAD) wip
    323b361 wip
    066d50c wip
    f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    defad49 (upstream/main, origin/main, origin/HEAD, main) Merge pull request #13 from kilip/strapi
    ```
    Count the commits you want to merge from `(HEAD)` commit.
    For example, if we want to merge commit from commit from `feat(strapi-bridge)` to `(HEAD) wip`
    then the count should be **4**.

    start rebase by using ```git rebase -i HEAD~4``` where 4 is commit count in the example above:
    ```sh
    git rebase -i HEAD~4
    ```

    This command will opening your git editor, with output like this:
    ```sh
    pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    pick 24a1c6b wip
    pick 8816256 wip
    pick b0aa58b wip

    # Rebase defad49..d5eb363 onto defad49 (4 commands)
    ....
    ```

    Change ```pick``` to ```s``` (squash):
    ```sh
    pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
    s 24a1c6b wip
    s 8816256 wip
    s b0aa58b wip

    # Rebase defad49..d5eb363 onto defad49 (4 commands)
    ```
    Save your changes which will open your git editor with output like this:
    ```sh
    # This is a combination of 4 commits.
    # This is the 1st commit message:
    feat(strapi-bridge): integrated openapi-fetch :rocket:
    # This is the commit message #2:
    wip
    # This is the commit message #3:
    wip
    # This is the commit message #4:
    wip

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    ...etc
    ```
    Remove or keep the commit message, for example if I want to remove all wip message:
    ```sh
    # This is a combination of 4 commits.
    # This is the 1st commit message:
    feat(strapi-bridge): integrated openapi-fetch :rocket:

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    ...etc
    ```

    Save your changes, and **force push** your commits to remote repository:
    ```sh
    git push origin main --force
    ```