I'm still trying to learn the (basic?) finer points of Git and have managed to get myself into trouble. I realized that I made some mistakes on HEAD, checked out an older commit and started coding from there. When I attempt a push I'm told that my current commit is behind and I need to merge with HEAD. Git recommends "git pull". However, HEAD has the code I want to ignore. How do I solve this problem? Thanks so much for the help.
Flowchart:
-------- HEAD (bad) ---------------------- + (behind conflict, requires \ / merge with HEAD, which is \------- Current commit (good) ----/ bad and needs to be ignored)
To jump back to a previous commit, first find the commit's hash using git log . This places you at commit 789abcd . You can now make new commits on top of this old commit without affecting the branch your head is on. Any changes can be made into a proper branch using either branch or checkout -b .
To create a branch from some previous commit, you can use the git-branch command. This creates a new branch, branchname which whose head points to specified commit-id . For example, the following creates a develop branch from the specified commit hash.
Here is what you can do:
git checkout <branch-to-modify-head> git reset --hard <commit-hash-id-to-put-as-head> git push -f
If you don't force the push, git will throw this error: Updates were rejected because the tip of your current branch is behind.
Note that this will tamper your git history, so another way of doing this is revert each commit you don't want. That way you retain your history:
git revert commit-id
Cheers
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