Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push: Push all commits except the last one

Tags:

git

push

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.

like image 639
python dude Avatar asked Jan 16 '12 11:01

python dude


People also ask

Does Push push all commits?

No, git push only pushes commits from current local branch to remote branch that you specified in command.

How do I push all commits to a remote branch?

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.

Do I need to push after each commit?

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.


3 Answers

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.

like image 51
KL-7 Avatar answered Oct 08 '22 17:10

KL-7


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

like image 38
SherylHohman Avatar answered Oct 08 '22 17:10

SherylHohman


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.

like image 12
Jarett Millard Avatar answered Oct 08 '22 16:10

Jarett Millard