Push to master commit done on a separate head

I would like to redo the changes from the previous commit.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git log
commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016
Author: Maxim Veksler <maxim@vekslers.org>
Date:   Sun Dec 28 09:12:17 2014 +0200

    Imagine a Supercomputer Helping You Fix Bugs Faster

commit 54561ed320633e72bb35a7ab668a9996e6ffca8f
Author: Maxim Veksler <maxim@vekslers.org>
Date:   Sun Dec 28 08:57:25 2014 +0200

    highcharts tweaks

commit d57144cbd004b3b9e893e8d50d7077634824ce9a
Author: Genady Okrain <genady@okrain.com>
Date:   Mon Dec 22 18:46:21 2014 +0200

    sash api

      

So I broke it down to 7f9dd753d39fd65b4272af713ef9c07a9f84f016 and decided to unwind it from 54561ed320633e72bb35a7ab668a9996e6ffca8f

Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout 54561ed320633e72bb35a7ab668a9996e6ffca8f
Note: checking out '54561ed320633e72bb35a7ab668a9996e6ffca8f'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 54561ed... highcharts tweaks

      

So far so good, now let's make a commit.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git commit -a -m'Back to not fucking it up'
[detached HEAD b5cb3e4] Back to not fucking it up

      

But now I need to push this back to master and upsteam. Here comes the dirty part ...

Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout HEAD
Maxims-MacBook-Air:hellodebug.com maximveksler$ git status
HEAD detached from 54561ed
nothing to commit, working directory clean
Maxims-MacBook-Air:hellodebug.com maximveksler$ git push
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>

Maxims-MacBook-Air:hellodebug.com maximveksler$ git push origin HEAD:master
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.
To git@github.com:maximveksler/hellodebug.com.git
 ! [rejected]        HEAD -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:maximveksler/hellodebug.com.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

      

That's right ... so now I'm stuck. Try a few more picky things.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git up
Fetching origin
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
You're not currently on a branch. I'm exiting in case you're in the middle of something.

      

git won't talk to us, that's cool. Anyway, I really don't need it.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:

  e9e202c no border
  b5cb3e4 Back to not fucking it up

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch new_branch_name e9e202c

Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

      

What? Not! Come back, I love you.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

      

Lost! Forever and ever? I hope no.

Maxims-MacBook-Air:hellodebug.com maximveksler$ git reflog
7f9dd75 HEAD@{0}: checkout: moving from e9e202c4703939064e099b735ceeb6b78dc7933a to master
e9e202c HEAD@{1}: commit: no border
b5cb3e4 HEAD@{2}: commit: Back to not fucking it up
54561ed HEAD@{3}: checkout: moving from master to 54561ed320633e72bb35a7ab668a9996e6ffca8f
7f9dd75 HEAD@{4}: commit: Imagine a Supercomputer Helping You Fix Bugs Faster
54561ed HEAD@{5}: commit: highcharts tweaks

      

OK, so they're out in the wild somewhere, but how do I get them back? It's too early to say goodbye.

HELP?

+3


source to share


4 answers


If the fix is ​​reasonably quick to do, I would do it again, but in a different way, starting with your bad commit

git reset --hard HEAD^ # brings your entire master branch and working copy back to where it worked
git reset origin/master # brings your HEAD pointer back to origin/master but leaves your working copy with the working code
# fix code
git commit -a -m'try to not fuck it up'
git push

      

Another way would be to revert the bad commit to make it clear that it was bad, then make fixes after that



git revert HEAD^
# fix code
git commit -a -m'better code this time'
git push # pushes two commits, the revert and the new code

      

If you want to keep the code that you already have on a separate head, you should be able to have cherry-pick

it in both of the above threads instead of fixing the code, simply git cherry-pick b5cb3e4

.

Good luck.

+2


source


To push changes made on a separate head to the origin / master (or another branch) try:

git push origin HEAD:master

      



If you have an alert, make sure you are up to date (you can try to reschedule it first). Although, if you're fixing commits or and you're sure your changes are the latest, you can force push ( -f

/ --force

). This is not recommended unless you know what you are doing (eg rebasing).

+1


source


Since you have already pushed commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016

, I would recommend doing git revert

.

Check your master first and do

git revert 7f9dd753d39fd65b4272af713ef9c07a9f84f016

      

This creates a new commit on top master

that reverts the changes that were entered in 7f9dd753

.

Now check out the fix you made as a new branch (you can see the commit id in the reflog e9e202c HEAD@{1}: commit: no border

)

git checkout -b myFix e9e202c 

      

Restore changes myFix

to master

.

git rebase master

      

Go back to master

and quickly move it to the branch myFix

.

git checkout master
git merge myFix

      

Now you can delete the branch myFix

and push master

.

git branch -D myFix
git push origin master

      

0


source


You can merge the master

upstream branch using strategy -s ours

(or -s theirs

when you are on the main server). This way you do not destroy history (for example, git reset --hard

or redirect operations), but you keep the working tree.

This method is especially useful when one git revert

is missing (for example, when merging between good and upstream).

0


source







All Articles