Search code examples
gitgit-commitgit-rebasegit-cherry-pick

Git: How to cherry-pick a commit without all previous history


I want to add some specific changes from a specific commit in my current repo to my upstream repo.

Running something like this: git push upstream <commit SHA>:<remotebranchname>

adds the commit plus all the previous changes

Running something like

git checkout -b new-branch 
git pull <remote> <upstream branch> branch is
git cherry-pick <commit hash>
git push <remote> new-branch

Also writes all the previous changes.

I just want to write the specific changes of that commit into the upstream repo, so it does not include the changes made by the previous commits in my current repo, which are not in the upstream.

There's lots of info on StackOverflow about cherry-pick and rebase but none answers this very specific question.


Solution

  • You can create the local branch out of the remote branch in upstream repo and then cherry pick.

    Assuming "upstream" is the name of your upstream remote, you can do the following:

    git fetch upstream
    git checkout -b new-branch upstream/<upstream branch>
    git cherry-pick <commit hash>
    git push upstream new-branch
    

    You can check the name of the upstream repo using

    git remote -v