diff --git a/README.md b/README.md index 84f75eb..d9bc8b9 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1094 TILs and counting..._ +_1095 TILs and counting..._ --- @@ -232,6 +232,7 @@ _1094 TILs and counting..._ - [Clone A Repo Locally From .git](git/clone-a-repo-locally-from-git.md) - [Configure Global gitignore File](git/configure-global-gitignore-file.md) - [Configuring The Pager](git/configuring-the-pager.md) +- [Copy A File From Another Branch](git/copy-a-file-from-another-branch.md) - [Create A New Branch With Git Switch](git/create-a-new-branch-with-git-switch.md) - [Delete All Untracked Files](git/delete-all-untracked-files.md) - [Determine The Hash Id For A Blob](git/determine-the-hash-id-for-a-blob.md) diff --git a/git/copy-a-file-from-another-branch.md b/git/copy-a-file-from-another-branch.md new file mode 100644 index 0000000..119ef62 --- /dev/null +++ b/git/copy-a-file-from-another-branch.md @@ -0,0 +1,31 @@ +# Copy A File From Another Branch + +After doing some work on a large feature branch, I wanted to split out some of +the work into a separate smaller branch to create an initial PR. There were +several files on the feature branch that I knew needed to be part of this +smaller branch. + +To bootstrap this smaller branch, I need to check it out from main and then +copy over the needed files. + +First, let's create a fresh branch: + +```bash +$ git branch --show-current +large-feature-branch + +$ git checkout main +$ git checkout -b new-smaller-branch +``` + +Now I can start pulling over the files I care about: + +```bash +$ git checkout large-feature-branch some/file/ICareAbout.js +``` + +This form of `git-checkout` looks on the specified branch for the specified +file and copies that over to the index. + +Note: this will copy over any local, working tree changes that you've made to +the named file.