Guide to recover from accidental commits and rebase in git

Written by hackernoon-archives | Published 2019/03/26
Tech Story Tags: git | developer | github | javascript | development

TLDRvia the TL;DR App

REBASE can be Helpful

In Git, the rebase command integrates changes from one branch into another. It is an alternative to the better known “merge” command.

You heard it’s scary right? You can create or recreate HISTORY! 🌗

People ask you to stay away from git rebase but, it can be very useful at times. This articles covers a good use of REBASE command contrary to the definition that is taken from the official git documentation.

A good use of git REBASE

Let’s talk about a very common scenario which I think all developers may have encountered or will encounter one day.

Suppose you are using the same machine for the company as well as for your personal projects and when you use two different accounts for your GitHub, you configure your local git like this:

git config —global user.name your namegit config — global user.email you@companydomain

So I did the same, and was also contributing to my personal projects.

The Black Day

One fine day, I just had a look at one of my projects Commit History on GitHub and I just skipped few heart beats on seeing the below message😧

The commits were showing “unknown” userÂ đŸ˜«

Since my email was different, GitHub was not able to identify me and on my profile I was not getting the counts of my contributions (the commits were gone, pull requests and issue creations were still there)

I was in distress and I wanted to somehow undo my mistake, I had to re-write history ⏳

REBASE at Rescue!

Then, after lots of efforts I got this useful command 😍, my life saviour:

git rebase -ip commit_hash

Let me explain to you how to use this properly otherwise, you might lose your tracking history_._

Rebase gives you the facility to edit all the commit history but after that, it may overwrite time and date of previous commit message with the current time and date.

So in this article, I am going to explain each and every step while using this command.

  1. To get all hash ids of commit history.

    $ git log $ git rebase -ip caaf71

  • caaf71 is commit id of git history. it should be minimum 4digits.

commit history

  • Pick last commit id if you want to rebase whole commit history.
  • After entering the above command you will get below command prompt.

vim file

2. Now, edit this file in vim editor by pressing “i” .This will go in edit mode then edit things and replace pick to edit then save it by pressing Esc, “:x” and then Enter.

Some useful vim commands are given below. Refer them if you are new to vim.

i  - insert mode 
wq - Save and quit
q! - Quit without saving

3. Now you can easily change username or email accordingly by using this command.

git commit --amend --author=”username <[email protected]>”--no-edit

Here — no-edit plays a very big role if you are using this then it won’t affect other things like commit time.

4. git rebase --continue

5. Repeat 3rd and 4th steps until you get this message “Successfully rebased and updated refs/heads/develop.”6. Finally, just push the final changes from the local machine to GitHub. it will update there also your present email and username.

Okay, there is a HACK!

This is hectic doing REBASE all the time when you have lots of commits very difficult to play with those.

Instead of REBASE I would suggest configure your local machine repo initially with two commands, only then start working on that.

git config user.email <your email address>
git config user.name <username>

Thank You 💝

I hope you like my writing, I love to learn & explore JavaScript. I am living in Noida, India and working as Software Engineer at Wesence.

You can visit my website: TheJSGirl, follow me on Github & Twitter


Published by HackerNoon on 2019/03/26