git: merge a single commit

Sometimes, you have one commit you want to get into production, but it's located after other changes that you're not ready to merge in yet. How can you get that single git commit into a different branch? First, you have to know the SHA of the commit you want:
  git checkout branch-with-commit-on-it
          git log
Highlight and copy the SHA of the commit you want to grab.
  git checkout master
        
          # -n => don't commit, just merge changes so we can review and commit ourself

          git cherry-pick -n [The commits SHA-1 Hash]
        
          # review

          git diff cached
        
          # commit if all is well

          git commit -a -m merge SHA1 ..."
If you're feeling confident, you can skip the -n and merge the single commit in directly and save a minute. Note, this isn't a merge, so it's possible you could have some conflicts down the road when you merge the original commit into this branch. You're creating a brand new commit object.

Established 2005 · Databasically © 2016