Git - only push up the most recent commit to github

前端 未结 3 736
臣服心动
臣服心动 2020-12-08 07:17

On my local git repo I\'ve got many commits, which include \'secret\' connection strings :-)

I don\'t want this history on github when I push it there.

Essen

3条回答
  •  眼角桃花
    2020-12-08 07:58

    Scenario

    You cloned a project and made a ridiculous amount of commits for a new feature, and the time has come to open source it or share it for review. And you don't want anyone to see your commit mess!

    The original branch was cloned_branch, your messy commits were on dev branch, and you want to publish your clean work on the public_branch branch.

    Assume the following commits:

    On branch cloned_branch:

    SHA0: Implemented X, and Y. (author:authorA)
    SHA1: Implemented Z. (author:authorA)
    SHA2: Implemented W. (author:authorA)

    You created dev branch and pushed some commits:

    SHA3: trying to implement Q feature to work..
    SHA4: shit, I doesn't work
    SHA5: messed up even more! :(
    SHA6: GOT IT WORKING!!!!
    SHA7: cleanup!

    Put all these changes in a single commit in public_branch:

    git checkout SHA2 -b public_branch      # new branch at `authorA`'s last commit
    git merge --squash SHA7        # squash commits up to your last one
    git commit -m "Implemented Q (author:me)"
    

    Result:

    Branches cloned_branch and dev remain as before, whilepublic_branch branch contains:

    SHA0: Implemented X, and Y. (author:authorA)
    SHA1: Implemented Z. (author:authorA)
    SHA2: Implemented W. (author:authorA)
    SHA8: Implemented Q. (author:me)

提交回复
热议问题