Skip to content
Tags

, ,

Insert a commit in the past (Git)

09/04/2012

Another note about Git: how I can easily insert a commit in the past or before another commit. Of course, all such changes to Git’s history must take place only in the local (non-publish) history or repositories.

I had a Git repository and needed to rewrite my local history by inserting a new commit somewhere in the past.

More specifically, my situation was like this:

    A---B---C---D---E---F        master

and I wanted to come up with something like this:

    A---B---K---C---D---E---F    master

Of course, commits after K would be different regarding their SHA name, but they will still be the same commits in content after finishing the operation.

Well, it isn’t actually an extreme or difficult case. It’s actually a very simple procedure to follow, but at first it didn’t sounded like such one in my mind. So, after I accomplished my purpose, I decided to leave a note here, since I’m sure I’m going to need it again in the future. The steps I followed are basically similar to the followings: First, I created a new branch at commit B and inserted the new commit:

    $ git checkout -b temp B
    $ git add [changes]
    $ git commit

The repository now looks something like this:

    A---B---K               temp
         \
          C---D---E---F     master

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

    $ git rebase temp master

There might be, of course, some conflicts to be resolved. But, the final step will bring us to the desired result.

Advertisements
4 Comments
  1. simplest and easiest walkthru. thanks for this!

  2. lyzkov permalink

    It could be done simplier:

    git commit -m K
    git rebase -i B

    and then change commit order on the list.

    • Thank you, lyzkov, for your suggestion!

      It is true that some times this approach just works! Especially when you’d like to add a new file in the past or change a couple of lines somewhere else.

      The problem is that we usually add staff as the time advances. If I perform another commit (K) at the head of the branch, I will also have the lines added at the previous commits (C, D, E, F) in it. When I’ll try to re-position it in the past, there will be too many conflicts because of the additions in [C, D, E, F] that will appear at their place before actually committed!

      • lyzkov permalink

        Now I see that it makes sense. I didn’t take it into account. In my case stuff that I tried to insert in K depended on B and it had nothing to do with newer commits.

        Thank you for clarifying it!

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: