0

I want to step backwards, commit-by-commit, to find some css styling that I overwrote. I'm aware of git-bisect; I used it and it didn't help me find my old work.

I can get a table of commit hashes in historical order with this command:

$ git log --pretty=oneline | awk '{print $1;}'

f3791f1da33b9e92a3a5292a0542834aca0908ef
2720a9ec4bb5b4f7b4dd02b056c375e71a44be3c
a9bb455608db70c76545e5862b89a02f9e9f590d

So I just need to grab the first one, and do a git checkout with it. I've tried to use head, but I don't know enough about pass syntax to pass the output correctly to it:

$ $(git log --pretty=oneline | awk '{print $1;}') | head -1
bash: 0dcca7e530fca682552341718462af150fe27dc5: command not found

$ $(git log --pretty=oneline | awk '{print $1;}') > head -1
bash: 0dcca7e530fca682552341718462af150fe27dc5: command not found

How can I get the top of the output from my previous command

I'd like this as a one-liner, so that I can add it to my git aliases, but if it's not, that's fine; I'll make a bash function.

5
  • "git-bisect ; I used it and it didn't help me find my old work" -- git bisect is the right tool for the job. If you couldn't find the old code, then you didn't go back far enough. Commented Apr 25, 2018 at 16:54
  • @Patrick No, it was the wrong tool for the job. The piece I was looking for wasn't in a series of continual progress where it "broke" in one commit; instead, I was trying different strategies to achieve the styling, and committing each attempt as I decided to quit it. There was no "before" and "after". Commented Apr 25, 2018 at 16:57
  • You may be misunderstanding how git bisect works. If you have to search through commits for a specific change, then thats what it's for. It doesn't matter if something broke or not. Commented Apr 26, 2018 at 12:31
  • @patrick Git bisect ignores at least half the commits in the range you specify to it, and in most cases, more. I needed to look at all of those. So using it, you will overlook most of the commits in the range you specify. When I originally ran git-biset, I specified ~15 commits, and didn't hit the one I wanted. When I stepped backwards through each commit, the one I was looking for was HEAD~3. There's no "before" and "after" clue in any of the commits to tell me whether the code I was looking for was good or bad. Commented Apr 26, 2018 at 13:27
  • @Patrick long short, for bisect to work, you have to know whether the commit you're looking for is before or after the commit you're presently looking at. If you don't have that information, then git bisect is likely the wrong tool. Commented Apr 26, 2018 at 20:11

1 Answer 1

2

To check out the parent commit, run

git checkout HEAD~1

This can be repeated ad nauseam, until you reach the first commit.

If you need to explore different parents leading up to a merge, use ^ instead:

git checkout HEAD^2

will check out the second parent of the current merge commit.

See “Specifying revisions” in the git-rev-parse documentation for details.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.