Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an older commit HEAD in Git?

Tags:

git

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) 
like image 489
user1449855 Avatar asked Dec 19 '12 15:12

user1449855


People also ask

How do you commit an old commit?

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 .

How do you make an old commit a new branch?

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.


1 Answers

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

like image 113
radtek Avatar answered Oct 16 '22 01:10

radtek