-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Git worktree lesson #385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eytanohana
wants to merge
4
commits into
eficode-academy:master
Choose a base branch
from
eytanohana:git-worktree-lesson
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Git worktree lesson #385
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,6 @@ git_attributes | |
| *~ | ||
| *.swp | ||
| **/exercise | ||
| **/exercise-* | ||
| **/remote | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Git Kata: Worktrees | ||
|
|
||
| Git worktrees allow you to check out multiple branches of the same repository simultaneously in different directories. | ||
| This is useful when you need to work on multiple features or branches at the same time without constantly switching branches | ||
| or stashing changes. | ||
|
|
||
| ## Setup: | ||
|
|
||
| 1. Run `source setup.sh` (or `.\setup.ps1` in PowerShell) | ||
|
|
||
| ## The task | ||
|
|
||
| You're working on a feature branch and need to make some changes. Instead of switching branches (which might require stashing changes), you'll use a worktree to work on the feature branch while keeping your main worktree on `master` clean. | ||
|
|
||
| 1. Explore the repository | ||
| 1. What branch are you currently on? | ||
| 2. What branches exist in this repository? | ||
| 3. Use `git log --oneline --graph --all` to see the commit history | ||
| >*Notice that you have a `master` branch and a `feature` branch with different commits* | ||
|
|
||
| 2. Create a worktree for the feature branch | ||
| 1. Use `git worktree add ../exercise-feature feature` to create a new worktree for the `feature` branch | ||
| 2. Check where the new worktree was created (it should be at `../exercise-feature`, a sibling to the `exercise` directory) | ||
| 3. Use `git worktree list` to see all worktrees | ||
| 4. Navigate to the new worktree directory (`cd ../exercise-feature`) and verify you're on the `feature` branch | ||
|
|
||
| 3. Make changes in the worktree | ||
| 1. While in the `exercise-feature` directory, create a new file called `new-feature.txt` with some content | ||
| 2. Add and commit this file to the `feature` branch | ||
| 3. Make another change - modify `feature.txt` and commit that change as well | ||
| 4. Use `git log --oneline` to see your new commits on the `feature` branch | ||
|
|
||
| 4. Merge the feature branch into master | ||
| 1. Navigate back to the main `exercise` directory (`cd ../exercise`) | ||
| 2. Verify you're on the `master` branch | ||
| 3. Merge the `feature` branch into `master` using `git merge feature` | ||
| 4. Use `git log --oneline --graph` to see how the branches have been merged | ||
| 5. Notice that you can see the commits you made in the worktree are now part of `master` | ||
|
|
||
| 5. Remove the worktree | ||
| 1. Make sure you're in the `exercise` directory (the main worktree) | ||
| 2. Use `git worktree remove ../exercise-feature` to remove the worktree | ||
| 3. Verify it's been removed with `git worktree list` | ||
| 4. Notice that the directory has been cleaned up automatically | ||
| 5. The `feature` branch still exists - you can verify this with `git branch` | ||
|
|
||
| ## Useful commands | ||
|
|
||
| - `git worktree add <path> <branch>` - Create a new worktree (typically as a sibling directory, e.g., `../exercise-feature`) | ||
| - `git worktree list` - List all worktrees | ||
| - `git worktree remove <path>` - Remove a worktree | ||
| - `git worktree prune` - Clean up stale worktree references | ||
| - `git merge <branch>` - Merge a branch into the current branch | ||
| - `git branch` - List branches | ||
| - `git log --oneline --graph --all` - View commit history | ||
| - `git status` - Check repository status | ||
|
|
||
| > **Note:** Worktrees are created as sibling directories to the main repository (e.g., `exercise-feature` next to `exercise`). The parent git repository's `.gitignore` includes patterns to ignore these worktree directories, keeping the exercise self-contained. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| . ..\utils\make-exercise-repo.ps1 | ||
|
|
||
| # Create initial commit on master | ||
| Set-Content -Value "Initial content" -Path README.txt | ||
| git add README.txt | ||
| git commit -m "Initial commit on master" | ||
|
|
||
| # Create feature branch with some commits | ||
| git switch -c feature | ||
| Set-Content -Value "Feature work" -Path feature.txt | ||
| git add feature.txt | ||
| git commit -m "Add feature.txt" | ||
|
|
||
| Add-Content -Value "More feature work" -Path feature.txt | ||
| git add feature.txt | ||
| git commit -m "Update feature.txt" | ||
|
|
||
| # Go back to master | ||
| git switch master |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Include utils | ||
| source ../utils/utils.sh | ||
|
|
||
| kata="worktree" | ||
| make-exercise-repo | ||
| config-local-username | ||
|
|
||
| # Create initial commit on master | ||
| echo "Initial content" > README.txt | ||
| git add README.txt | ||
| git commit -m "Initial commit on master" | ||
|
|
||
| # Create feature branch with some commits | ||
| git switch -c feature | ||
| echo "Feature work" > feature.txt | ||
| git add feature.txt | ||
| git commit -m "Add feature.txt" | ||
|
|
||
| echo "More feature work" >> feature.txt | ||
| git add feature.txt | ||
| git commit -m "Update feature.txt" | ||
|
|
||
| # Go back to master | ||
| git switch master | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.