how-to-manage-multiple-flutter-versions-with-git-worktrees-and-z
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHow to Manage Multiple Flutter Versions with Git Worktrees and ZSH
如何使用Git Worktrees和ZSH管理多个Flutter版本
If you have been using Flutter for any length of time then you probably have needed to use multiple flutter versions across multiple projects.
如果您使用Flutter有一段时间了,可能会遇到需要在多个项目中使用不同Flutter版本的情况。
过去我常用FVM(Flutter Version Management),它类似于JS领域的NVM(Node Version Manager)。
我想要一个仅依赖Git的解决方案,于是开始使用worktrees来管理Flutter频道。
Download the SDK
下载SDK
Check out the flutter repo in a known directory, in this case I will download it to :
~/Developer/git clone https://github.com/flutter/flutter ~/Developer/flutter在指定目录中检出Flutter仓库,本例中我将下载到目录:
~/Developer/git clone https://github.com/flutter/flutter ~/Developer/flutterAdd Flutter Channels
添加Flutter频道
Now we can add the branches we want to track:
cd ~/Developer/flutter
git checkout origin/dev
git worktree add ../flutter-stable stable
git worktree add ../flutter-beta beta
git worktree add ../flutter-master masterWe need to checkout the dev channel to allow us to create the worktree for the master branch. This will keep the directory separate so we can work on PRs and apply local changes.
flutterAfter this runs we should have 4 directories: , , and .
flutterflutter-masterflutter-betaflutter-stable现在我们可以添加想要跟踪的分支:
cd ~/Developer/flutter
git checkout origin/dev
git worktree add ../flutter-stable stable
git worktree add ../flutter-beta beta
git worktree add ../flutter-master master我们需要检出dev频道,以便为master分支创建工作树。这样可以将目录独立出来,方便我们处理PR和应用本地修改。
flutter执行完成后,我们会得到4个目录:, , 和。
flutterflutter-masterflutter-betaflutter-stableAdd ZSH Alias for each Channel
为每个频道添加ZSH别名
Now we need a way to reference each SDK on the fly with an alias in ZSH. Add the following to :
~/.zshrcalias flutter-master='~/Developer/flutter-master/bin/flutter'
alias dart-master='~/Developer/flutter-master/bin/dart'
alias flutter-beta='~/Developer/flutter-beta/bin/flutter'
alias dart-beta='~/Developer/flutter-beta/bin/dart'
alias flutter-stable='~/Developer/flutter-stable/bin/flutter'
alias dart-stable='~/Developer/flutter-stable/bin/dart'现在我们需要通过ZSH别名来快速引用各个SDK。将以下内容添加到中:
~/.zshrcalias flutter-master='~/Developer/flutter-master/bin/flutter'
alias dart-master='~/Developer/flutter-master/bin/dart'
alias flutter-beta='~/Developer/flutter-beta/bin/flutter'
alias dart-beta='~/Developer/flutter-beta/bin/dart'
alias flutter-stable='~/Developer/flutter-stable/bin/flutter'
alias dart-stable='~/Developer/flutter-stable/bin/dart'Conclusion
总结
After reopening the terminal, you can verify it is working by running (or add any channel we added above):
flutter-master doctor
dart-master --version
flutter-stable doctor
dart-stable --versionYou can update any of the channels by navigating to the directory of the worktree for the given channel and pulling changes like any other Git repo.
cd ~/Developer/flutter-master
git checkout origin/masterGit worktrees are just a way to checkout multiple branches as separate folders instead of needing to stash changes.
重启终端后,您可以通过运行以下命令验证是否配置成功(或使用我们添加的任意频道):
flutter-master doctor
dart-master --version
flutter-stable doctor
dart-stable --version您可以通过导航到对应频道工作树的目录,像操作其他Git仓库一样拉取变更来更新任意频道。
cd ~/Developer/flutter-master
git checkout origin/masterGit worktrees只是一种将多个分支检出为独立文件夹的方式,无需暂存变更。