Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push an intermediate commit during interactive rebase in git?

Tags:

git

I have a local branch set to track the remote origin/master and it was created by

"git checkout -b mybranch origin/master"

Now after that, I have two separate local commits on the same local branch, and commit2 is depends on commit1

Now I want to push the changes include in the commit1 to the remote branch, what's the right way to do that?

I have run "git rebase -i" and change the commit1 to edit, and git stop at commit 1 when rebasing, which is what I want.

But if I try to do something like "git push", I got error message saying

"fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use

git push origin HEAD:<name-of-remote-branch>

",

Now assume the hash of my commit1 is "abce", what's the right way to push this change to remote origin/master?

like image 407
Daniel Gao Avatar asked Feb 15 '26 01:02

Daniel Gao


2 Answers

git status tells you how can you cancel the rebase in your working copy. (probably git rebase --abort)

And then you can push specific commits like this:

git push <remotename> <commit SHA>:<remotebranchname>
like image 172
Lajos Veres Avatar answered Feb 16 '26 18:02

Lajos Veres


Although Lajos's answer is correct, you can do it even easeier - error msg gave you everything :)

pick xxxxxx 
exec git push <remotename> HEAD:<remotebranchname>
pick xxxxxy
exec git push <remotename> HEAD:<remotebranchname>
pick xxxxxz

This way you are always pushing HEAD so you don't have to copy/paste SHA arround.

like image 43
watbywbarif Avatar answered Feb 16 '26 18:02

watbywbarif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!