Today I Learned - Rocky Kev

TIL reverting git

POSTED ON:

TAGS:

Issue: you pushed to master instead of your branch... again.

I do it all the time because I suck.

Solution 1: Remove the history

This method re-writes history and rolls back your changes.

It's great if you push, realize your mess up, and quickly want to roll back. Highly recommended for those not in teams or if you own the project.

It's bad for those who do not allow force pushes.

How to:
Step 1: create a new branch.

Step 2: check out master.
git checkout master

Step 3: reset master by X commits.
git reset --hard HEAD~3

Step 4: check out your new branch again.

What happening?

Your branch is now ahead of master by X commits.
All those commit changes will move to that new branch.

Step 5: Force push.

Remember that

master <-- 3 commits behind
branch <-- currently has the new commits

git push origin master --force

Via https://stackoverflow.com/a/1628584

Solution 2: Revert

This method undo's the prior commit, and makes a change in history that you reverted.

If other people have already pulled the changes you want to revert, they will need to pull the new changes you pushed after the revert.

git revert commit12345
git push origin master

Related TILs

Tagged:

TIL the core-js spam trolling

This entire repo is a hilarious rabbit-hole when you combine this with everyone complaining about the console spam that the dev added to it asking if anyone can find him a job. One of my favorite PRs

TIL How many Fucks there are in Linux

As of today, in the Linux Repo, there are comments that contains the following mentions: 1,651 for hacks, 2,863 for workarounds, and 4,102 for fixme

TIL reverting git

Two solutions. One is re-write history and hide your mistakes. The other is showing the history, to remind you of your mistakes.