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.
Essentially I want to push everything I have, but want to get rid of a whole lot of history.
Perhaps I would be better running in a branch for all my dev, then just merging back to master before committing... then the history for master will just be the commit I want.
I've tried running rebase:
git rebase –i HEAD~3
That went back 3 commits, and then I could delete a commit.
However ran into auto cherry-pick failed, and it got quite complex.
Any thoughts greatly appreciated... no big deal to can the history and start again if this gets too hard :-)
Normally when one does a push, they push everything at once. However, git does provide a way to push only one commit at a time. The caveat is that the single commit you want to push must be directly above the tip of the remote branch (the oldest of your local commits).
The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.
You can branch your current work, rewind the master, then cherry-pick the latest commit back to the master:
git branch secret git reset --hard HEAD~3 git cherry-pick secret
In pictures,
A--B--C--D--E (master)
after git branch secret
:
A--B--C--D--E (master, secret)
after git reset --hard HEAD~3
:
A--B (master) \ C--D--E (secret)
after git cherry-pick secret
:
A--B--E' (master) \ C--D--E (secret)
Finally, if you git checkout secret; git rebase master
, you can get:
A--B--E' (master) \ C--D (secret)
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.
cloned_branch
:SHA0: Implemented X, and Y. (author:authorA)
SHA1: Implemented Z. (author:authorA)
SHA2: Implemented W. (author:authorA)
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!
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)"
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With