[git] Reverting an accidental commit/amend using reset

Alright. So this one might sound a special case, but there are chances that something like this scenario might turn up too, while playing with git.

Scenario:

You are working on top of a commit de7504f : My original commit, made some changes, and you really wanted to make it into a new commit, but accidently you gave git commit --amend and you are now in the edit commit message of de7504f.

Once saved, you can see in git log that you are in a new commit-id: 7645040: My original commit and amended change.

This can be equivalent to typing git commit, typing in the commit message, saving it – and later thinking – “God, I shouldn’t’ve committed it”.

Solution:

The idea is to run git reset and it would just take you back to a point where you have the changes staged, but not committed yet. To be more human, this is the place after you have made your changes, and typed in git add too.

You can get the commit-id straight from git log, but for my above scenario, since it was an amend, it might not just work. For that, we got git reflog

git reflog

it would list out commit id of every single change you did including amend, rebase, chgeckout, reset etc.

7645040 HEAD@{0}: commit (amend): My original commit and amended change
de7504f HEAD@{1}: commit: My original commit

Yay! Now you need to just run

git reset 7645040 
git status

and you are back in history where you have git add -ed the files, but not done git commit --amend yet. You can add those to a separate commit, or stash it or do whatever.

If you pass git reset 7645040 --hard, all changes which you did after 7645040 would be just thrown off, so use it with caution.

References: https://git-scm.com/docs/git-reset

Rewriting Git history, correcting a previous commit in your PR

This blog post should be dedicated to my mentor jaragundeΒ at Igalia, as he had been the one pointing me out to go back again and again to that wrong PR, I made, and correct it. The first time I did, it sounded a bit confusing, and here you go!

Scenario:
You are working on an XYZ Pull Request having a series of commits namely 234234, 11233 and the latest one being 5514134. Your mentor tells you there is a mistake in 11233, and you want to go back to that commit, and correct the same.

Alright, so lets assume you are in the branch myfaultybranch with the git log showing up 5514134 at the top.

Steps:

  1. git stash – lets make sure there are no debris left!
  2. git rebase -i 11233^ – make sure you put the ^ at the end, otherwise the following steps would fail miserably.
  3. on the screen that come up, you will have the other commits too listed, looking like
    pick 11233 [#222] Your faulty commit message here
    pick 234234 [#222] This commit has no problem
    

    Edit the first line, from pick to edit so that it looks like:

    edit 11233 [#222] Your faulty commit message here
    pick 234234 [#222] This commit has no problem
    
  4. Save, and you are in a state where you are one step before committing 11233. Yay! Now make all your changes you want, correct all the mistakes at your ease!
  5. Once set, git add and git commit --amend and modify the commit message too, if you want
  6. All set, and you can now execute git rebase --continue and you will be back in your commit 5514134. Yay!
  7. Push your changes to your origin via a git push -f myfaultybranch
  8. In case you screw up in between, please do git reset --hard 5514134 and you will be reverted back πŸ™‚

Hope it helps! Lets rewrite history! Found something wrong ? Please do comment!

Remove and commit a composer dependency in vendor/

Reverting a composer dependency addition directly via ‘Revert Change’ from gerrit would break composer, as we experienced after merging https://gerrit.wikimedia.org/r/#/c/188547/. The patch was to revert Plancake dependency due to compatibility issues in HHVM. I was un-aware of the fact that removing a dependency from composer.json would actually remove the dependency references from the vendor/ directory. Summing up – the correct way of producing a removal of a composer dependency would be:

  1. Remove the dependency from composer.json
  2. run composer update
  3. git add – -all
  4. git commit

A backport to an earlier branch can be produced with the following steps:

git pull --rebase
git checkout -b 'backportTo15' orgin/wmf/1.25wmf15
vi composer.json //remove dependecny
composer update
git add --all
git commt

If you screw up your backport in between – this would be an easy fix :

cd vendor
git pull --rebase
git review -d <change_id>
git checkout origin/wmf/1.25wmf15 . // would reset all files to wmf/1.25wmf15
vi composer.json // and remove the un-wanted dependency
composer update
git add --all 
git commit --amend
git review -R

Some moments from wikimedia-dev πŸ™‚
[17:09:38] tonythomas: You’re meant to get composer to update itself. Otherwise it makes a huge mess.
[17:11:28] tonythomas: Yeah, composer and git aren’t friends. 😦

[SOLVED] Git complains ‘server certificate verification failed’!

Just met with this error, and here is the hack !
Problem:
* You are in a public wifi, and usually connect SSL via a Certificate issued by the authority.
* The certificate file is Downloads/CA_Certificate.cer
Solution:
* Global git fix

git config --global http.sslcainfo Downloads/CA_Certificate.cer

That would do ! do git pull now.
Happy hacking!