Insert Commits between Previous Commits
05 March 2015
Git is amazing, it seems that impossible is nothing to it. Unlike most traditional version control systems, git allows us to modify commit history rather easily. In this post, I would like to demo how to insert a commit.
What I want
Suppose I have the following commit history:
####### - Commit 2: first change for B ####### - Commit 1: first change for A
What I want is:
####### - Commit 2: first change for B ####### - Commit 3: second change for A ####### - Commit 1: first change for A
You might argue that it doesn't make much sense. But I just want to keep the log "well-organized".
How
Anyway, in git, it is a piece of cake. If you are interested, here is how:
- Create a branch based on designated commit.
- Modify files and commit as intended.
- Rebase master to the branch.
- Delete the branch.
- If you like, you can even squash the new commit to previously one.
An example
Below is a detailed example.
- I want to insert a commit between c700f4f and d0ba359 (the second and third
commits).
$ git lg o d749713 - (HEAD, master) 2015-03-05-org-html-max-width: add <lgfang@15 minutes ago> o f3f2f47 - 2014-05-01-wake-on-lan: add <lgfang@47 minutes ago> o e828726 - 2015-03-04-csh-cmd-not-found: add <lgfang@47 minutes ago> o 770231c - 2014-01-24-emacs-log-reading: add <lgfang@50 minutes ago> o 4580b9b - 2015-02-28-blog-a-new-start: add <lgfang@51 minutes ago> o ce4e3d0 - emacs-code-notes, tmux, tmux-intro, git: add <lgfang@54 minutes ago> o c700f4f - add framework for notes <lgfang@64 minutes ago> o d0ba359 - Update user information <lgfang@86 minutes ago> o 8ea1791 - Init clone <lgfang@5 days ago>
- Create a temporary branch "insert"
$ git branch insert d0ba359 $ git co insert Switched to branch 'insert'
- Modify and commit
- Rebase master branch to the "insert"
$ git rebase insert master First, rewinding head to replay your work on top of it... Applying: add framework for notes Applying: emacs-code-notes, tmux, tmux-intro, git: add ... $ git lg o a8f2f6c - (HEAD, master) 2015-03-05-org-html-max-width: add <lgfang@21 seconds ago> ... o 0814805 - add framework for notes <lgfang@27 seconds ago> o dfcc1ab - (insert) index.md: add <br/> <lgfang@65 seconds ago> o d0ba359 - Update user information <lgfang@2 hours ago> o 8ea1791 - Init clone <lgfang@5 days ago>
NOTE: the "rebase" brings me to the master branch.
- Delete the temporary branch
$ git branch -d insert Deleted branch insert (was dfcc1ab).
- Squash
$ git rebase -i 8ea1791 ...
In the editor, change the new commit to "s"
pick d0ba359 Update user information s dfcc1ab index.md: add space <--- change pick to s pick 0814805 add framework for notes ... pick a8f2f6c 2015-03-05-org-html-max-width: add
$ git lg o 1988d1d - (HEAD, master) 2015-03-05-org-html-max-width: add <lgfang@5 seconds ago> ... o 20c8a30 - emacs-code-notes, tmux, tmux-intro, git: add <lgfang@9 seconds ago> o df570e0 - add framework for notes <lgfang@9 seconds ago> o 4c06346 - Update user information <lgfang@17 seconds ago> o 8ea1791 - Init clone <lgfang@5 days ago>
blog comments powered by Disqus