Thursday, August 4, 2022
HomeData ScienceWhat Do You Do When Git Goes Fallacious? | by Sophia Yang...

What Do You Do When Git Goes Fallacious? | by Sophia Yang | Aug, 2022


Learn how to safely revert newest commit

Picture by Jim Wilson on Unsplash

Have you ever been in a state of affairs when your git went horribly fallacious and it’s essential to revert desperately? I discover myself on this state of affairs on occasion and other people appear to run into the identical points as nicely. Many new Git customers could really feel hopeless once they first encounter this. So on this article, I’m going to point out you what to do when git goes fallacious and the way do you repair it safely.

Lengthy story quick, I forgot to make use of a department for my code and dedicated my modifications with out first doing git pull to fetch all of the modifications from the distant repository. The result’s:

  • git push bought rejected.
  • git pull doesn’t work both.

There are some good strategies within the “trace” above. However generally what we wish to do is simply undo our newest commit. And on the similar time, we should still need the modifications we did from our newest commit and don’t wish to lose them. There are a number of methods to do that. Here’s what I do:

  • I verify my git log, which reveals all my commit historical past:
  • I then copy and previous my newest commit ID. I do the git diff commit_id > patchto save lots of the diff of the most recent decide to a file. This step is essential. This enables us to save lots of the modifications of our newest decide to a separate file.
  • I then do a git reset --hard HEAD^ to revert to the earlier commit. Be aware that it is a hazard zone. You’ll lose your newest commit.
  • Now once we verify git log once more, we’re certainly on the earlier commit:
  • Then lastly we’re in a position to do a git pull
  • What will we wish to do if we nonetheless wish to apply the modifications of our newest commit? Do not forget that we have now saved the diff within the patch file. Right here we will run patch -p1 < patch to use these modifications. I did a dry run patch -p1 --dry-run < patch first simply to ensure every thing works.
  • Now we will see the file modifications in git diff and we will use git commit -a to commit all of the modifications and git push to efficiently push the modifications.

In all, listed here are the git instructions I did to unravel the battle and save my file modifications:

# get the id of the lastest commit
git log
# save the diff from the most recent decide to a patch file
git diff 13fd7bc9219ee10f64b803303e6d90f94bb6e219 > patch
# revert to the earlier commit
git reset — onerous HEAD^
# affirm that we're on the earlier commit
git log
# fetch modifications from repo
git pull
# apply file modifications from the trail file
patch -p1 — dry-run < patch
patch -p1 < patch
# verify git diff on the file modifications
git diff
# commit modifications
git commit -a
# push modifications
git push

Let me know what you normally do on this state of affairs and when you have any strategies. Be happy to share with me your enjoyable experiences with git! 🙂

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments