![]() ![]() Hard reset would have done me no good of course, as that discards all the changes. Soft reset allows me to keep the changes staged for commit, a minor convenience compared to the default mixed mode, which retains changes as un-staged, so I’d have to git add them again. Instead of the hash, I could have also used the head position ( and the result would have been the same. I chose soft reset, to preserve the changes I made, as I would like to commit them later properly to my fix branch. We can thus pick where to go back easily: $ ~/ git reset -soft d1b3026ĭ1b3026 is the short hash reference to the state before the amend happened. ![]() A new entry is created whenever a branch update happens, including branch changes, merges, resets, and commits, even tagging amends and using the stash. The reflog contains way more useful information than git log. ![]() It will tell us what happened exactly in our local repo: $ ~/ git reflogĭ0d4939 (HEAD -> master) commit (amend): This is a very important featureĭ1b3026 (feature/very-important-feature) merge feature/very-important-feature: Fast-forwardĨ5da11f (fix/minor-fix-to-cool-feature) checkout: moving from feature/very-important-feature to masterĭ1b3026 (feature/very-important-feature) commit: This is a very important featureĨ5da11f (fix/minor-fix-to-cool-feature) checkout: moving from master to feature/very-important-featureĨ5da11f (fix/minor-fix-to-cool-feature) merge fix/minor-fix-to-cool-feature: Fast-forwardĢd87747 (feature/my-cool-feature) checkout: moving from fix/minor-fix-to-cool-feature to masterĨ5da11f (fix/minor-fix-to-cool-feature) commit: Resolved this minor issue with the coolest feature everĢd87747 (feature/my-cool-feature) checkout: moving from master to fix/minor-fix-to-cool-featureĢd87747 (feature/my-cool-feature) merge feature/my-cool-feature: Fast-forwardĪdbfc15 checkout: moving from feature/my-cool-feature to masterĢd87747 (feature/my-cool-feature) commit: Implemented this great feature to my appĪdbfc15 checkout: moving from master to feature/my-cool-featureĪdbfc15 commit (initial): initial commit First of all, we should forget about git log – it does not contain enough information for us to go with, and check the git reflog instead. It turns out it’s not that complicated as it may sound at first. How can one fix such a mistake, and have the merge commit restored? I sat there scratching my head for a while. But it turned out I was on our development branch, so I amended the last successfully merged commit on it. As it was pretty late and I was eager to head home, I chose to just amend the commit to what I assumed was my other commit and force push it to origin. I had a commit already on my fix branch pushed to the remote, and we had a policy to only submit one commit to pull-request, so I was looking at a history rewrite already. I un-stashed my changes with git stash apply to continue with an important fix I was working on for the next day. We did some code reviews before, where I changed branches back-and-forth and kind of lost track of where I have ended up. Let me share my horror-story about amending:Īt one evening, I was sitting in the office completely alone, as all of my colleagues left for home already. This way you can potentially edit other peoples’ work, and even push directly to a branch, skipping pull request and code review. If you amend a commit that has already been pushed to the remote, you may only use force-push – git push -f – to submit your changes. $ git commit -amend -no-editĪmending is usually simple, but if you aren’t careful, it can be a source of some headache, as this operation alters the git history. Sometimes you end up in a situation when you need to amend a commit to add further changes to it, either because you forgot some important things, or due to company git policies.
0 Comments
Leave a Reply. |