I have made bad commits in the github and i want to do the following things:
Is it possible to do this?
The key command is:
git checkout -B master aSHA1
If you know where you want to reset your master branch, that is the way to do it in one line.
From git checkout
man page:
-B <new_branch>
Creates the branch
<new_branch>
and start it at<start_point>
;
if it already exists, then reset it to<start_point>
. This is equivalent to running "git branch
" with "-f
";
Then you can force push it:
git push --force
That will reset origin/master
(the master branch on the GitHub side) to your old SHA1.
Note:
With Git 2.40 (Q1 2023), "checkout -b/-B
" is clarified, and explains how "git branch [-f]
"(man) is similar, but different in the documentation.
See commit fedb8ea (19 Jan 2023) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit 06f2b5f, 30 Jan 2023)
checkout
: document-b
/-B
to highlight the differences from "git branch
"
The existing text read as if "
git checkout -b/-B name
"(man) were equivalent to "git branch [-f] name
"(man), which clearly was not what we wanted to say.
git checkout
now includes in its man page:
Creates the branch
<new-branch>
, start it at<start-point>
; if it already exists, then reset it to<start-point>
.
And then check the resulting branch out.
This is equivalent to running "git branch
" with "-f
" followed by "git checkout
" of that branch;