Skip to content

Instantly share code, notes, and snippets.

@vasi
Created September 25, 2013 04:53
Show Gist options
  • Save vasi/6695284 to your computer and use it in GitHub Desktop.
Save vasi/6695284 to your computer and use it in GitHub Desktop.
Git subset
[Olio:~/Desktop/mefi] vasi$ cd ..; rm -r mefi; clear
override r--r--r-- vasi/staff for mefi/.git/objects/25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99? ^C
[Olio:~/Desktop] vasi$ rm -rf mefi; clear
[Olio:~/Desktop] vasi$ git init mefi
Initialized empty Git repository in /Users/vasi/Desktop/mefi/.git/
[Olio:~/Desktop] vasi$ cd mefi
[Olio:~/Desktop/mefi] vasi$ # Create some files and commit them
[Olio:~/Desktop/mefi] vasi$ for i in foo bar iggy blah; do echo $i > $i; done
[Olio:~/Desktop/mefi] vasi$ git add *
[Olio:~/Desktop/mefi] vasi$ git ci -am 'Initial commit'
[master (root-commit) 3512eb6] Initial commit
4 files changed, 4 insertions(+)
create mode 100644 bar
create mode 100644 blah
create mode 100644 foo
create mode 100644 iggy
[Olio:~/Desktop/mefi] vasi$ cd ..; rm -rf mefi; clear
[Olio:~/Desktop] vasi$ git init mefi
Initialized empty Git repository in /Users/vasi/Desktop/mefi/.git/
[Olio:~/Desktop] vasi$ cd mefi
[Olio:~/Desktop/mefi] vasi$ # Create some files and commit them
[Olio:~/Desktop/mefi] vasi$ for i in foo bar iggy blah; do echo $i > $i; done
[Olio:~/Desktop/mefi] vasi$ ls
bar blah foo iggy
[Olio:~/Desktop/mefi] vasi$ cat iggy
iggy
[Olio:~/Desktop/mefi] vasi$ git add *
[Olio:~/Desktop/mefi] vasi$ git commit -am 'Initial commit'
[master (root-commit) ee49b16] Initial commit
4 files changed, 4 insertions(+)
create mode 100644 bar
create mode 100644 blah
create mode 100644 foo
create mode 100644 iggy
[Olio:~/Desktop/mefi] vasi$ git checkout -b subset # Create a new branch
Switched to a new branch 'subset'
[Olio:~/Desktop/mefi] vasi$ git rm foo blah
rm 'blah'
rm 'foo'
[Olio:~/Desktop/mefi] vasi$ git commit -am "Remove the files we don't want"
[subset c42af24] Remove the files we don't want
2 files changed, 2 deletions(-)
delete mode 100644 blah
delete mode 100644 foo
[Olio:~/Desktop/mefi] vasi$ # Now make sure master is current with respect to this branch
[Olio:~/Desktop/mefi] vasi$ git checkout master
Switched to branch 'master'
[Olio:~/Desktop/mefi] vasi$ git merge --no-commit --no-ff subset # Lets us make changes before committing the merge
Removing foo
Removing blah
Automatic merge went well; stopped before committing as requested
[Olio:~/Desktop/mefi] vasi$ git checkout HEAD foo blah # Restore the files we don't really want to remove from master
[Olio:~/Desktop/mefi] vasi$ ls
bar blah foo iggy
[Olio:~/Desktop/mefi] vasi$ git commit -am 'Make master current with subset branch'
[master e7dc287] Make master current with subset branch
[Olio:~/Desktop/mefi] vasi$ git log --pretty=oneline
e7dc2874b3fc3316be26fdec248ba6ed442c3fe4 Make master current with subset branch
c42af244ea2e18834af6f8b6d0c0d4602eeb54d5 Remove the files we don't want
ee49b16ed29c643705870a54d821280b74ac9593 Initial commit
[Olio:~/Desktop/mefi] vasi$
[Olio:~/Desktop/mefi] vasi$ # Now let's make a change to a file in our subset, and merge it to master
[Olio:~/Desktop/mefi] vasi$ git checkout subset
Switched to branch 'subset'
[Olio:~/Desktop/mefi] vasi$ ls
bar iggy
[Olio:~/Desktop/mefi] vasi$ cat iggy
iggy
[Olio:~/Desktop/mefi] vasi$ echo iggy2 >> iggy
[Olio:~/Desktop/mefi] vasi$ cat iggy
iggy
iggy2
[Olio:~/Desktop/mefi] vasi$ git commit -am 'Make a change to iggy'
[subset 4e276db] Make a change to iggy
1 file changed, 1 insertion(+)
[Olio:~/Desktop/mefi] vasi$ git checkout master
Switched to branch 'master'
[Olio:~/Desktop/mefi] vasi$ git merge --no-edit subset
Merge made by the 'recursive' strategy.
iggy | 1 +
1 file changed, 1 insertion(+)
[Olio:~/Desktop/mefi] vasi$ git log --pretty=onelineba251902a163dc4aa70aaf8220211dc8b5b3c3f7 Merge branch 'subset'
4e276db17b4ae2c61868ab7898e412211e3dafcc Make a change to iggy
e7dc2874b3fc3316be26fdec248ba6ed442c3fe4 Make master current with subset branch
c42af244ea2e18834af6f8b6d0c0d4602eeb54d5 Remove the files we don't want
ee49b16ed29c643705870a54d821280b74ac9593 Initial commit
[Olio:~/Desktop/mefi] vasi$ git status
# On branch master
nothing to commit, working directory clean
[Olio:~/Desktop/mefi] vasi$ cat iggy
iggy
iggy2
[Olio:~/Desktop/mefi] vasi$ ls
bar blah foo iggy
[Olio:~/Desktop/mefi] vasi$ git checkout subset
Switched to branch 'subset'
[Olio:~/Desktop/mefi] vasi$ ls
bar iggy
[Olio:~/Desktop/mefi] vasi$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment