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.
No, git push only pushes commits from current local branch to remote branch that you specified in command.
In order to push a Git branch to remote, you need to execute the “git push” command and specify the remote as well as the branch name to be pushed. If you are not already on the branch that you want to push, you can execute the “git checkout” command to switch to your branch.
Typically pushing and pulling a few times a day is sufficient. Like @earlonrails said, more frequent pushes means less likelihood of conflicting changes but typically it isn't that big a deal. Think of it this way, by committing to your local repository you are basically saying "I trust this code. It is complete.
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.
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 commit222222
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
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.
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