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!

Git-Review troubleshoot :- Build failed due to merge conflict

This error is caused due to merge conflicts between your working branch and master.
Background:
* The master branch have changed while you were busy working on your branch.
* You made some \unacceptable\ changes.
How to:

git-rebase
git-rebase

* Checkout to the master branch.
$ git checkout master
* Hard reset to your original master branch
$ git fetch --all
$ git reset --hard origin/master
[optional]
$ git pull origin master
* Checkout to the working branch
$ git review -d
* Rebase the patch with master
$ git rebase master
# The conflicts will be listed. The easiest way to fix a conflict is to checkout that file with the master. Eg: for a file bar.foo causing problems – do :
$ git checkout master bar.foo
REMEMBER:- you need to add the files after you have made the changes via
$ git add bar.foo
# Once you have fixed all the errors, continue with the rebase process.
$ git rebase --continue
* Amend git commit- This would make sure the new PS would not be sent as a new Change, but as a new Patch set.
$ git commit --amend
* Push the code
$ git review -R

PS:- If you hit with an error while rebasing, always delete the rebase files
$ rm .git/rebase-apply