0

How do I get a fresh copy of a commit from say 5 previous commits ago?

Background:

I have a situation where some code I was working on which was working correctly but when checked out didn't work. Not sure what happened with the commit, but it could be a problem with the installation of a bower package.

I need to go back to the previous commit and try and redo the work and get it committed so that it works properly when the next person updates their code.

I've tried doing:

git checkout -b <branchname>

However this seems to have reverted back to the previous commit but still seems to have merged some files together. I need to be able to take a fresh copy of the previous commit.

Also as a side issue, when committing a bower package should I just add the package to the bower .json install file? So that when the next person checks it out the bower install runs for them?

user1532669
  • 2,288
  • 4
  • 36
  • 72
  • 1
    Ask the bower question separately. – mmtauqir Nov 22 '14 at 15:01
  • Are you looking to amend the broken commit (i.e. replace it with a different commit that doesn't have the problem)? That's possible with Git, but you should only do it if the commit in question hasn't been pushed yet. If it has, just fix the bug with an additional commit to HEAD and push that too. – Wyzard Nov 22 '14 at 15:04
  • 2
    `git checkout HEAD~5` will take you back 5 commits. But what you really want is `git bisect`. You will need to learn quite a bit more about how git works before you will find it useful, but when you understand it you will wonder how you ever lived without it. – William Pursell Nov 22 '14 at 15:24
  • 1
    Also, `man git-reflog` – William Pursell Nov 22 '14 at 15:30
  • possible duplicate of [Revert to a previous Git commit](http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit) – Andrew C Nov 22 '14 at 17:13
  • @WilliamPursell You should submit an answer for that. – Justin Howard Nov 22 '14 at 19:05

3 Answers3

2
git checkout <previous commit hash>

should work.

mmtauqir
  • 8,499
  • 9
  • 34
  • 42
1

To get the working tree to the state it was 5 commits ago on the current branch:

git checkout HEAD~5

To move to branch foo 5 commits ago:

git checkout foo~5

Either will put you in detached head state. If this bothers you, create a new branch.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • if there are a few unattached heads/unnamed branches dangling around when I create the new branch to attach the head to, how does git know which unattached head to use? Is it just the last one created? I hope that makes sense :) – user1532669 Nov 23 '14 at 23:28
  • `git branch` will create a branch at the current HEAD. – William Pursell Nov 24 '14 at 04:57
-1

As I understand, you don't know the commit_id which has problem.

For that you can revert each and every branch one by one and test the code.

To know what are all the changes in that commit, you can use

git log -p

To revert the changes of one commit, you can use

git revert commit_id

For reference, you can check link Revert multiple git commits

Community
  • 1
  • 1
Santhosh Tangudu
  • 759
  • 9
  • 19