Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - only push up the most recent commit to github

Tags:

git

github

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 :-)

like image 321
Dave Mateer Avatar asked Feb 16 '10 22:02

Dave Mateer


People also ask

Does git push only push last commit?

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).

How do I ignore the last commit?

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.


2 Answers

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) 
like image 140
Greg Hewgill Avatar answered Sep 18 '22 19:09

Greg Hewgill


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)

like image 21
Paschalis Avatar answered Sep 22 '22 19:09

Paschalis