'Git: How do I checkout a commit and merge it back into my branch?

I've made a series of commits in my branch. However, the last 3 or so were mistakes - I want to 'revert' my branch to a previous commit.

Googling the issue I understand I must use git log to get the ID of the commit I want to return to, then git checkout <id>. But I cannot find information beyond that.

It seems when I checkout I am no longer within my original branch. How do I merge back into my original branch, thus undoing the changes in the previous erroneous commits?



Solution 1:[1]

If you simply want to add a new commit that restores the file states,

First, remove all files using rm (be careful not to remove .git/). You can also use git rm -f, but pay attention to .gitignore changes.

Then, fetch all files from a commit (get the commit SHA using git log):

git checkout 1234567 \*

Here the asterisk is escaped to prevent shell expansion. Git will do the expansion. If you're using a Unix shell, you can also use single quotes:

git checkout 1234567 '*'

Commit the fetched files:

git add -A
git commit

VoilĂ !

Solution 2:[2]

when your you create a branch from another are created two lines works which could take lines different then in the branch current can not view the commits other branches, as are independent, but if you're done your work in that branch suppose is called "dev" then if you want to run the your main branch (master) only you should confirm your stagin area marge your branches something like this: firts verify your current branch with git branch it show the Name of current HEAD Them go to master git checkout master and finally marge branches git marge dev (dev = name_of_your_branch) and you can show all logs of two branches with git log --graph

Solution 3:[3]

The most foolproof way I have found is simply to clone the repository again so that you have a second copy of the repository in another folder. Let's say you have done that and have folders MyRepo and MyRepo2, both of which point to the latest version in the desired branch of the repository.

Now check out the specific good commit in MyRepo2, then use a file compare tool to see what files have changed between MyRepo and MyRepo2. You can then copy the changed files from MyRepo2 to MyRepo, check in and push.

The above can be done in one line as outlined in this excellent answer to a similar question.

git diff --binary HEAD commit_sha_you_want_to_revert_to | git apply

Solution 4:[4]

If you want to completely remove the last to commit and the working directory to coincide with last but two commit (HEAD~~):

git reset --hard HEAD~~

But if you don't want to modify commit history (because you want to keep wrong commits or you already have pushed it to common repository), just revert to earlier state but keep wrong commits, then iBug's method with checkout is better. The problem with your solution is that 'git checkout doesn't move the branch pointer only the head pointer so get to a detached head state. If you check out individual files and not commits then your pointers remains at their original place, so you don't need to merge afterwards. (some explanation for my code: the above command moves not just the HEAD pointer, but the master branch pointer two step back to HEAD~~, and updates the working directory (and index) to coincide with commit HEAD~~

HEAD : current commit HEAD~ : previous commit HEAD~~ HEAD~~~..etc)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iBug
Solution 2 juancarlos
Solution 3
Solution 4