0

In my master branch yesterday different people made their own commits. Last 2 commits are wrong, is there a way to checkout to master at an specific commit?

Something like

git checkout master -some_commit

enter image description here

pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • 1
    Possible duplicate of [Go to particular revision](https://stackoverflow.com/questions/7539130/go-to-particular-revision) – esqew Oct 11 '19 at 15:47

3 Answers3

3

Yes you can! You can directly go to any commit you like by passing the commit hash as an argument

git checkout COMMIT_HASH

You can find the commit hash by simply looking at the git log.

Keep in mind that this will most likely leave you in a detached HEAD state.

Edit as per the discussion in the comments: what you can do is to go to your desired commit and from that detached HEAD state create a new branch. Go to your new branch and cherry-pickthe two wrong commits. At this point they are safe. You can safely revert back your master branch to the correct commit you want and then add the fixes you want. After all this, you are free to merge the other branch.

  1. Checkout desired commit
  2. Create new branch
  3. cherry-pickthe two commits in the new branch
  4. Revert master to desired commit.
  5. Optional: merge branch
mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • Yes, now how can I commit the changes on this HEAD to actual branch? – pmiranda Oct 11 '19 at 16:05
  • I'm sorry can you ask again. Do you want the changes at this commit you went to to be moev to some other branch? – mnestorov Oct 11 '19 at 16:08
  • I went to this hash (a yesterday commit as I show in the image), I want to fix things there, and use them in the latest commit (of today). Should I make a new branch with the fixes, then checkout to actual branch, then merge with the new one, and push the actual branch? in that case the actual branch will still have the code from the previous 2 commits. – pmiranda Oct 11 '19 at 16:10
  • Sorry for the many questions, do you want to keep the wrong commits or leave them in master or put them in a separate branch – mnestorov Oct 11 '19 at 16:17
  • $git-bugs git:(master) `git checkout e54b73f0c49a4bbc3428feee86633fc26c212b22` – xgqfrms Feb 11 '20 at 06:14
1

The correct answer depends on what your ultimate goal is, and whether these commits also exist in a remote repository.

If all you want to do is see what the project looks like before those commits were applied, then

git checkout bad-commit-id^

This checks out a different "branch", and later you can checkout master again.

If your goal is to change your repository such that those commits essentially no longer exist, you can do

git reset --hard bad-commit-id^

or

git revert bad-commit-id

The former erases all commits from bad-commit-id to HEAD. The latter just reverts the single bad commit by adding a new commit to HEAD.

Which one you would choose largely depends on if the commits exist on a remote repository. Using reset will cause problems when you try to push, either for you or for somebody else who has cloned the repository.

If it's a shared repository and you are a not-very-sophisticated git user, I would recommend using git revert. If you know what you're doing, then git reset also has its place, but you really do need to understand the ramifications of doing that.

tl;dr - git revert is quite safe, while git reset is a dangerous, but powerful tool.

Rudedog
  • 4,323
  • 1
  • 23
  • 34
0

You want git reset --hard HEAD~2

the HEAD~X syntax refers to the commit X commits previous

orangejulius
  • 989
  • 1
  • 10
  • 23
  • 1
    This is a bad idea if anyone else has already pulled those commits into a local repository. – chepner Oct 11 '19 at 15:56
  • hey, i didn't say to use `git push` :P The command I posted only modifies their local git copy. What happens after that is up to them. – orangejulius Oct 11 '19 at 16:00
  • It's not clear from the question if this is the OP's private repository, or the shared repository that others will pull from. – chepner Oct 11 '19 at 16:05