git push: Push all commits except the last one

GitPush

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

A more general approach that works to push up to a certain commit, is to specify the commit hash.

git push <remote> <commit hash>:<branch>

For example, if you have these commits:
111111 <-- first commit
222222
333333
444444
555555
666666 <-- last commit

git push origin 555555:master

..Will push all but your last commit to your remote master branch, and

git push origin 333333:myOtherBranch  

..Will push commits up to and including 333333 to your remote branch myOtherBranch

Solution 3 - Git

Another possibility is to

git reset --soft HEAD^

to uncommit your most recent commit and move the changes to staged. Then you can

git push

and it will only push the remaining commits. This way you can see what will be pushed (via git log) before pushing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpython dudeView Question on Stackoverflow
Solution 1 - GitKL-7View Answer on Stackoverflow
Solution 2 - GitSherylHohmanView Answer on Stackoverflow
Solution 3 - GitJarett MillardView Answer on Stackoverflow