Skip to content

Instantly share code, notes, and snippets.

@gerjantd
Created April 26, 2012 10:49
Show Gist options
  • Save gerjantd/2498743 to your computer and use it in GitHub Desktop.
Save gerjantd/2498743 to your computer and use it in GitHub Desktop.
Git: split a previous commit
If you accidentally combine multiple changes into one commit, here's how to split it. You will need this if you wish to merge the changes seperately; cherry-pick operates on commits.
admin@STAMMETJEVM ~/My Documents/git/gerjantd
$ git clone https://[email protected]/gerjantd/foo.git
Cloning into 'foo'...
remote: Counting objects: 301, done.
remote: Compressing objects: 100% (178/178), done.
remote: Total 301 (delta 59), reused 301 (delta 59)
Receiving objects: 100% (301/301), 152.94 KiB, done.
Resolving deltas: 100% (59/59), done.
admin@STAMMETJEVM ~/My Documents/git/gerjantd
$ cd foo
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ vi BUGFIX1
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ vi BUGFIX2
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git add BUGFIX*
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git commit -m "Added fixes for #1 and #2"
[master 288a29c] Added fixes for #1 and #2
2 files changed, 2 insertions(+)
create mode 100644 BUGFIX1
create mode 100644 BUGFIX2
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git log --pretty=oneline
288a29cfc6ba88b601ec754afcf545ed496007b1 Added fixes for #1 and #2
228662ad192fae2b4736b8bba3d7566fc201a74c Redirecting
ebf4597bc8e3fab57f14a82bc9c74b0514369ded Exception Handling
098ba1a7797b7059ca5e2fc94079175f3f5ef951 Added attribute conversion-service to e
c39530548de00f1cf8fc73c5aeeab137888b9cfc File Upload
106e18acdf0179fc671e76394cd77a049bde9f95 Forms
0b8e4a6e44ccc3b95c04438f3e1d516b970b1f28 Validation
e3547e2e8a4fb6efca4acfcc97e8b38f15468e37 Type Conversion
0882c79582ddd9f19fd23f623a2fa260fee0e6da Rendering Views
1a7d3f1256682e8ba92700af6529aed37ba08664 Message Converters
cc7bec7bc9637b53dc233f08d7d7b4ba0c00c84b Generating Responses
ec4a987008a9d41a580f66928c63ca382f089985 Obtaining Request Data
4c5e5b8c86aaa8f337c532da497506fd9c938999 Mapping Requests
f1476c3e8e3f2b320becb1c089f9062d25230f3c Added .gitignore
c5869a7b7783cebfeeda6b5506b2e7ff7964f4ba The simplest possible @Controller
e86446d53912b6341ce9fb480852b006b820cb6a Updated README
1568978e30a11245569558f8c3d253dbcf0817b3 Updated README
a82c291cb151b94b61cf9c4b9bbb50498a16c5c6 first commit
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git branch splitme 288a29c
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git rebase -i splitme^
[in vi, replace 'pick' by 'edit', then :x to save and drop back to shell]
Stopped at 288a29c... Added fixes for #1 and #2
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master|REBASE-i)
$ git reset HEAD^ BUGFIX2
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master|REBASE-i)
$ git commit -m "Added fix for #1" --amend
[detached HEAD 721bcd8] Added fix for #1
1 file changed, 1 insertion(+)
create mode 100644 BUGFIX1
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master|REBASE-i)
$ git checkout splitme .
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master|REBASE-i)
$ git commit -m "Added fix for #2"
[detached HEAD 9453de8] Added fix for #2
1 file changed, 1 insertion(+)
create mode 100644 BUGFIX2
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master|REBASE-i)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git branch -D splitme
Deleted branch splitme (was 288a29c).
admin@STAMMETJEVM ~/My Documents/git/gerjantd/foo (master)
$ git log --pretty=oneline
9453de80e2618d807a43862d2e4028e576995321 Added fix for #2
721bcd8cff74d404442a4aa60e09767f11a7c508 Added fix for #1
228662ad192fae2b4736b8bba3d7566fc201a74c Redirecting
ebf4597bc8e3fab57f14a82bc9c74b0514369ded Exception Handling
098ba1a7797b7059ca5e2fc94079175f3f5ef951 Added attribute conversion-service to e
c39530548de00f1cf8fc73c5aeeab137888b9cfc File Upload
106e18acdf0179fc671e76394cd77a049bde9f95 Forms
0b8e4a6e44ccc3b95c04438f3e1d516b970b1f28 Validation
e3547e2e8a4fb6efca4acfcc97e8b38f15468e37 Type Conversion
0882c79582ddd9f19fd23f623a2fa260fee0e6da Rendering Views
1a7d3f1256682e8ba92700af6529aed37ba08664 Message Converters
cc7bec7bc9637b53dc233f08d7d7b4ba0c00c84b Generating Responses
ec4a987008a9d41a580f66928c63ca382f089985 Obtaining Request Data
4c5e5b8c86aaa8f337c532da497506fd9c938999 Mapping Requests
f1476c3e8e3f2b320becb1c089f9062d25230f3c Added .gitignore
c5869a7b7783cebfeeda6b5506b2e7ff7964f4ba The simplest possible @Controller
e86446d53912b6341ce9fb480852b006b820cb6a Updated README
1568978e30a11245569558f8c3d253dbcf0817b3 Updated README
a82c291cb151b94b61cf9c4b9bbb50498a16c5c6 first commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment