0

I am a sole developer for a web application. Every day I use a scheduled task to add a new commit and push to the master branch (I.e. git add . & git commit -m "scheduled commit" & git push)

I use this mainly for keeping backups/version history each day in case I ever need to revert.

I am having a major problem with my codebase that I can't find the error for and so would like to revert back 20 commits from the remote master branch yet keep the history of the commits so that I can slowly add back the code ensuring the error does not return.

I have done git reset --hard {hash} which has put the header back 20 commits. How can I now merge this as a new commit?

Git pull/push will fast-forward these extra 20 commits. I don't want to do this. I want the code exactly as it was 20 commits ago as a new commit and as the latest commit.

git checkout doesn't work as it says I am behind 20 commits.

git checkout {hash} puts me in headless mode - I am not quite sure what this means.

masterg174
  • 143
  • 9
  • You could create a branch from 20 commits ago and work through that branch to find out where the error is. ```git branch branchname ``` – Keith John Hutchison Jun 22 '21 at 02:59
  • It's not "headless" mode, but rather "detached HEAD" mode. That just means that you are looking at one specific commit from history, without being on some branch. (Commits can be on many branches, or no branch at all, so there's nothing special about this. It's just that if you make a *new* commit in this mode, it will be on no branch at all, so it will be difficult or impossible to find later, and in time, will be deleted.) – torek Jun 22 '21 at 03:35

2 Answers2

0

I am having a major problem with my codebase that I can't find the error for and so would like to revert back 20 commits from the remote master branch yet keep the history of the commits so that I can slowly add back the code ensuring the error does not return.

Use git bisect to do this much quicker. Write a test program that demonstrates the problem, and that exits nonzero when the problem is present; have the program exit zero when the problem is not present. Then:

  • Check out the branch tip and run git bisect start, then run git bisect bad to mark the commit as bad.
  • Pick some known-good commit to mark as good, such as the one 20 commits back. (If you're not sure if that one is good, check it out and run your test.) Assuming you're quite certain that the commit about 20 commits ago is good, run git bisect good HEAD~20.
  • Now, using your test program to diagnose good vs bad commits, run git bisect run test_program arguments-if-any.... Git will automatically zero in on the first bad commit.

For more details, see the git bisect documentation. If it's hard to automate the test, you can still use git bisect and just test each commit manually. Instead of having to test about 20 revisions at worst, though, you'll only have to test about log220, or about 5 revisions.

(When the bisect is done, you will be in detached-HEAD mode on the bad commit. Use git show to view the commit itself. Use git bisect reset to get back to the branch-tip, out of detached-HEAD mode.)

torek
  • 448,244
  • 59
  • 642
  • 775
0

I found my issue in the end. It actually wasn't a problem with the codebase. My web application is a Laravel 8 project.

The problem was with one of the modules/vendor packages.

Somehow the Intervention Image package had become corrupted (maybe an extra tab or white space) causing images using the method to appear blank.

Restoring an older commit wouldn't have fixed this as the vendor folder is not included on github (Git ignore).

Luckily I had Windows previous versions enabled so I could rename the current project folder, restore a backup from a few weeks ago and then do a git pull of the latest commit. As the vendor folder is not impacted, the latest code was pulled down and the issue did not reoccur.

I would still like to know how to go restore a previous commit as the latest commit though in case I need to in the future.

masterg174
  • 143
  • 9
  • See [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/q/4114095/1256452) for the answer to your auxiliary question. – torek Jun 22 '21 at 08:37