git push: Push all commits except the last one

Issue

Is there a way to push all my local commits to the remote repository except the most recent one? I’d like to keep the last one locally just in case I need to do an amend.

Solution

Try this (assuming you’re working with master branch and your remote is called origin):

git push origin HEAD^:master

HEAD^ points to the commit before the last one in the current branch (the last commit can be referred as HEAD) so this command pushes this commit (with all previous commits) to remote origin/master branch.

In case you’re interested you can find more information about specifying revisions in this man page.

Update: I doubt that’s the case, but anyway, you should be careful with that command if your last commit is merge. With merge commit in the HEAD HEAD^ refers to the first parent of that commit, HEAD^2 – to its second parent, etc.

Answered By – KL-7

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published