Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ScottJWalter/cd9714bd82f1b0e33591421bb6bd202e to your computer and use it in GitHub Desktop.

Select an option

Save ScottJWalter/cd9714bd82f1b0e33591421bb6bd202e to your computer and use it in GitHub Desktop.

Revisions

  1. @jrussellsmyth jrussellsmyth revised this gist Dec 5, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-cherry-pick-forks.md
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ git remote add <name-for-other-fork> <other-fork-remote-address>
    ```bash
    git fetch <name-for-other-fork>
    ```
    4. Check create a new branch from the remote branch that you want to apply the commit to.
    4. Create a new branch from the remote branch that you want to apply the commit to.
    ```bash
    git checkout -b <cherry-pick-pr-branch-name> <name-for-other-fork>/<target-branch-name>
    ```
  2. @jrussellsmyth jrussellsmyth revised this gist Dec 5, 2024. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions git-cherry-pick-forks.md
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ When working with forks in Git, you may need to pull specific commits from one f
    ```bash
    git remote add <name-for-other-fork> <other-fork-remote-address>
    ```
    <name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository.
    <other-fork-remote-address> is the clone URL or SSH address of the other fork.
    `<name-for-other-fork>` is a symbolic name you will use to refer to the fork in your local repository.
    `<other-fork-remote-address>` is the clone URL or SSH address of the other fork.

    3. Fetch the commits from the other fork.
    ```bash
    @@ -63,8 +63,8 @@ If you want to create a pr to apply a commit(s) from your fork to another fork,
    ```bash
    git remote add <name-for-other-fork> <other-fork-remote-address>
    ```
    <name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository.
    <other-fork-remote-address> is the clone URL or SSH address of the other fork.
    `<name-for-other-fork>` is a symbolic name you will use to refer to the fork in your local repository.
    `<other-fork-remote-address>` is the clone URL or SSH address of the other fork.

    3. Fetch the commits from the other fork.
    ```bash
  3. @jrussellsmyth jrussellsmyth revised this gist Nov 15, 2024. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions git-cherry-pick-forks.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    # Cherry picking commits across forks
    When working with forks in Git, you may need to pull specific commits from one fork to another. This can be useful when you want to include a specific change from one fork to another without merging the entire branch.

    [From another fork to your fork](#from-another-fork-to-your-fork)

    [From your fork to another fork](#from-your-fork-to-another-fork)

    ## From another fork to your fork
    1. Identify the commit hash(es) of the commit(s) you want to cherry-pick. You can find this hash on GitHub or by using `git log` after fetching.

  4. @jrussellsmyth jrussellsmyth created this gist Nov 15, 2024.
    100 changes: 100 additions & 0 deletions git-cherry-pick-forks.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    # Cherry picking commits across forks
    When working with forks in Git, you may need to pull specific commits from one fork to another. This can be useful when you want to include a specific change from one fork to another without merging the entire branch.
    ## From another fork to your fork
    1. Identify the commit hash(es) of the commit(s) you want to cherry-pick. You can find this hash on GitHub or by using `git log` after fetching.

    2. Add the "other" fork as a remote to your local repository.
    ```bash
    git remote add <name-for-other-fork> <other-fork-remote-address>
    ```
    <name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository.
    <other-fork-remote-address> is the clone URL or SSH address of the other fork.

    3. Fetch the commits from the other fork.
    ```bash
    git fetch <name-for-other-fork>
    ```
    4. Check out the branch that you want to apply the commit to.
    ```bash
    git checkout <target-branch-name>
    ```
    5. Make a new branch to apply the cherry-picked commit(s) that can be used to create a PR
    ```bash
    git checkout -b <cherry-pick-pr-branch-name>
    ```

    6. Cherry-pick the commit(s) from the other fork.
    ```bash
    git cherry-pick <commit-hash>
    ```
    or several commits
    ```bash
    git cherry-pick <commit-hash1> <commit-hash2> ...
    ```
    or a commit range
    ```bash
    git cherry-pick <commit-hash1>..<commit-hashN>
    ```

    7. Resolve any conflicts that may arise during the cherry-pick process. Git will prompt you to resolve conflicts if they occur. After resolving conflicts, you will need to add the changes and continue the cherry-pick process.
    ```bash
    git add <conflict-file>
    git cherry-pick --continue
    ```

    8. Push the new branch with the cherry-picked commit(s) to your fork.
    ```bash
    git push origin <cherry-pick-pr-branch-name>
    ```

    9. Create a pull request from the new branch in your fork to the target branch in your fork.

    ## From your fork to another fork
    If you want to create a pr to apply a commit(s) from your fork to another fork, the steps are the same but we need to use the other fork as the target.

    1. Identify the commit hash(es) of the commit(s) you want to cherry-pick. You can find this hash on GitHub or by using `git log` after fetching.

    2. Add the "other" fork as a remote to your local repository.
    ```bash
    git remote add <name-for-other-fork> <other-fork-remote-address>
    ```
    <name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository.
    <other-fork-remote-address> is the clone URL or SSH address of the other fork.

    3. Fetch the commits from the other fork.
    ```bash
    git fetch <name-for-other-fork>
    ```
    4. Check create a new branch from the remote branch that you want to apply the commit to.
    ```bash
    git checkout -b <cherry-pick-pr-branch-name> <name-for-other-fork>/<target-branch-name>
    ```

    5. Cherry-pick the commit(s) from your fork.
    ```bash
    git cherry-pick <commit-hash>
    ```
    or several commits
    ```bash
    git cherry-pick <commit-hash1> <commit-hash2> ...
    ```
    or a commit range
    ```bash
    git cherry-pick <commit-hash1>..<commit-hashN>
    ```

    6. Resolve any conflicts that may arise during the cherry-pick process. Git will prompt you to resolve conflicts if they occur. After resolving conflicts, you will need to add the changes and continue the cherry-pick process.
    ```bash
    git add <conflict-file>
    git cherry-pick --continue
    ```

    7. Push the new branch with the cherry-picked commit(s) to the other fork.
    ```bash
    git push <name-for-other-fork> <cherry-pick-pr-branch-name>
    ```
    NOTE: it is very important that you include the other fork name and branch name in the push command to ensure that the branch is pushed to the correct fork. Suppress your instincts to use `origin` in this command.

    8. Create a pull request from the new branch in the other fork to the target branch in the other fork.