Preface
The previous article Git Worktree Advanced Use good overall response, which is completely an obscene technique that can be used in daily development. In the micro-service environment, we usually have multiple repo, advanced usage is good, but each repo is configured according to advanced usage, it is still more troublesome, don’t you see that some students have spoken out
The speaker is interested, the listener is interested, then write a script
Git Worktree script
I am not very good at writing bash scripts, I worktree.sh
and wrote a 061a96516974fb, which completely executed the entire process of the previous article
#!/bin/bash -e
repo=$1
dir="${repo##*/}"
dir="${dir%.*}"
echo $dir
branch=$2
defaultBranch="${branch:-main}"
mkdir -p $dir
cd $dir
git clone --bare $repo .bare
echo "gitdir: ./.bare" > .git
echo " fetch = +refs/heads/*:refs/remotes/origin/*" >> ./.bare/config
git worktree add $defaultBranch
This script receives two parameters
- The first parameter is repo ,
https | ssh
Both methods are available - The second parameter is branch . The default master branch name of different repo may be different. Github now changes the master branch name from master to
main
, so the default value here ismain
The script creates a repo folder with the same name by default
Save worktree.sh in a certain location of the disk directory and authorize (maximum permissions)
chmod -R 777 worktree.sh
Next, test the effect
If the default master branch name is develop
, we only need to add one parameter:
../worktree.sh git@github.com:FraserYu/amend-crash-demo.git develop
Obviously it is not a good way to find the directory of the executable file every time. We need to give an alias and know the environment variables, so that we can use it globally. Open the .zshrc
or .bashprofile
file according to our computer situation (the former I used)
Create an alias gwt
inside (it feels that the name is high-end):
alias gwt='/Users/rgyb/Documents/projects/personal/worktree.sh'
Then refresh the file
source ~/.zshrc
Let's look at the effect again:
At this point, using worktree should be very simple, right?
Summarize
If you have various pain points of multi-branch switching, learn to use git worktree, those problems will no longer exist. A full set of services are provided, and the script is placed
https://github.com/FraserYu/script.git, have any questions, please leave a message
Sun Gong Yibing| Original
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。