Abhishek Somani

About Abhishek Somani

Abhishek is working as a senior java developer in a product start up .He has worked on various java related enterprise applications and frameworks. He loves to explore new technologies

Git : How to add commit in between old commits

I have a Git repository and need to rewrite my local history by inserting a new commit in between old commits.

More specifically my sitatution is like this:
 
 
 
 
 
 
 

AB—BC—CD—EF   MASTER

and I wanted to come up with something like this:

AB—BC—SA—CD—EF   MASTER

Where SA is my new commit i.e to be inserted b/w commit BC & CD.

Well, it isn’t actually an extreme or difficult case.It’s actually a very simple procedure to follow:

$ git checkout master
$ git checkout -b temp BC 
$ git add
$ git commit # your changes that will be SA

Now your Repo will look like this :

AB—BC—SA  temp
      \
      CD—EF MASTER

After this repository layout it’s rather simple to transform it into a single sequence of commits:

$ git rebase temp master

You may get few conflicts that you need to resolve .

Now you are all Done !!!

TAGGING COMMITS

You will notice that your SHA keys are modified and tag doesn’t appear above commit BC to make sure your tags are in line follow the steps:

$ git tag -l #to list all your tags.

For each tag type the following command,

$ git show TAG_NAME

to see the details of the old commit.

Make note of the subject line, date, and hash of the old commit.

Page through git log looking for that subject line and date. Make note of the hash of the new commit when you find it.

$ git tag --force TAG_NAME NEW_COMMIT_HASH #to update the tag.

Hope that you have not mistaken one commit for another with a similar subject line and date.

Thanks to Santosh Mohanty for writing this post .

git-logo
Post Comments And Suggestions !!!

Reference: Git : How to add commit in between old commits from our JCG partner Abhishek Somani at the Java, J2EE, Server blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Git : How to add commit in between old commits"

  1. Kanagaraj says:

    Interactive rebase is the right way to go.

Leave a Reply


1 × four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close