9/27/2023 0 Comments Git undo commit file too bigUndo your commit but not your changesįor your next trick, we’re going to use another reset command but this time without the -hard. Using HEAD~1 just means “the commit before this one that I’m on now”. Where I used HEAD~1, you can also supply the git commit reference. Commit, make the safety branch, and breathe deeply before you rush in to this): git reset -hard HEAD~1 To undo the “fix” commit (that didn’t fix things at all), make your current branch point to the previous commit (if you run git reset -hard with uncommitted changes, they will be lost forever. I have the branch name in my prompt but git status will also include the current branch name. Pro-tip: Check which branch you’re on before you proceed. Here’s git log again so we can check what’s going on: $ git log -onelineħa6528c (HEAD -> awesome-feature, safety) Try to fix the problem That’s not the right solution, so I’m going to start by throwing away the last commit (that tries to fix the problem) completely. ![]() Here, it’s the second commit in the list that’s a problem – but I already tried to fix it by adding another commit. I also like to create an extra branch to point to the current HEAD commit so I don’t lose anything before I perform git surgery of any kind. Equally, if you included dependencies as this example does, and the problem is that they are too large to be accepted by GitHub: you still won’t be able to push because the intermediate commit has the large files in. If you accidentally committed your API credentials for example, they’re still present in git history. It’s pretty common to try to remove things that shouldn’t have been included in a repo by adding a commit to remove it but in lots of cases that doesn’t solve the problem. At this point, the project looks like this: $ git log -onelineħa6528c (HEAD -> awesome-feature) Try to fix the problem ![]() This is a real scenario (and it wasn’t actually my mistake this time but honestly, I’m confident fixing this thing because I’ve done it more than once!!), a developer unintentionally included node_modules/ in the work he was doing. Oops, I committed node_modules to my repo ![]() This post will walk you through how to fix the problem without losing any of your work. For example, accidentally including a node_modules directory in your otherwise excellent and useful commit. Good practice is good of course, but knowing how to undo a mess you created is better – and mistakes will happen. When I teach git I try to show loads of good practice about how to inspect what’s staged before commit, etc etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |