preface

Git Worktree allows you to work in multiple branches at the same time without affecting each other. However, the directory where the worktree is created is not under the current project. I always feel that the created worktree does not belong to the current project. This is very uncomfortable for me, and today I will show you an advanced usage to solve this pain point

Prepare knowledge

You need to know a little bit about Bare Repo before using advanced usage, and we’ll start with commands you’re familiar with

git init
git clone https://github.com/FraserYu/amend-crash-demo.git
Copy the code

These two commands will produce a non-bare repO, the kind of repo we do our daily work in, where you can add/commit/pull/push

Generating a bare repO is also as simple as adding the –bare parameter to the two commands above

git init --bare
git clone --bare https://github.com/FraserYu/amend-crash-demo.git
Copy the code

Execute the two clone commands and look at the file contents to see the difference

  1. bare repoContains only Git related information, not our actual project files (.java/.js/.py), andnon-bare repoIt contains all of our information
  2. The Bare repo name is given by default.gitSuffix, which proves the first point
  3. Bare Repo does not exist.gitFolder, which also causes it to not be likenon-bare repoasadd/commit/pull/push

Seeing this, you might feel that bare Repo is just a Git shell folder with nothing to show for it. Because bare repo has these features (you can’t change it), it prevents the contents of the repO from getting messed up and can therefore be used to make a private, centralized repO, as one illustration explains:

If you are interested, try out the process locally by following these commands:

user@server:$~ git init --bare name_to_repo.git user@machine1:$~ git clone user@server:/path/to/repo/name_to_repo.git . user@machine1:$~ cd name_to_repo user@machine1:$~ touch README user@machine1:$~ echo "Hello world" >> README user@machine1:$~ git add README user@machine1:$~ git commit -m "Adding a README" user@machine1:$~ git push origin master  user@server:$~ ls /path/to/repo/name_to_repo.git/ branches/ config description HEAD hooks/ info/ objects/ refs/ user@machine2:$~ git clone user@server:/path/to/repo/name_to_repo.git . user@machine2:$~ ls name_to_repo.git/ README user@machine2:$~ cat README Hello worldCopy the code

The result: No matter how hard you push a file under Machine1, bare Repo has no files you push, only Git information. But when Machine2 clone the latest repO, it will see machine1 push files, which is where Git’s magic lies

At this point, Bare Repo is done explaining. Next, take advantage of bare Repo’s features to optimize working on multiple branches at once by following up on Git Worktree

Git Worktree

First, create and CD a separate folder for your project (amend-crash-demo here) in the directory of your choice

mkdir amend-crash-demo
cd amend-crash-demo
Copy the code

Next, clone the project code as bare and place the contents in the.bare folder:

git clone --bare [email protected]:FraserYu/amend-crash-demo.git .bare
Copy the code

Git Worktree: git Worktree: git Worktree: git Worktree: git Worktree: git Worktree: git Worktree

echo "gitdir: ./.bare" > .git
Copy the code

Then we need to edit the.bare/config file and modify the [remote “Origin “] content to be the same as below (i.e. add line 6), which ensures that we create the workTree toggle branch that displays the correct branch name

vim .bare/config

# -----------------------------------------------
 [remote "origin"]
       url = [email protected]:FraserYu/amend-crash-demo.git
       fetch = +refs/heads/*:refs/remotes/origin/*
Copy the code

Next we can create the workTree. First we need to create the worktree for the main branch, because the commit-ish that the HEAD of the main branch points to is the same as the commit-ish that you used to create other worktrees

git worktree add main

# --------------------------------
Preparing worktree (checking out 'main')
HEAD is now at 82b8711 add main file
Copy the code

Usually we don’t work directly on the main branch, but instead create other types of branches and continue to create a worktree called Feature/jira234-Feature3

git worktree add -b "feature/JIRA234-feature3" feature3

# ------------------------------------------------
Preparing worktree (new branch 'feature/JIRA234-feature3')
HEAD is now at 82b8711 add main file
Copy the code

If you look at the contents of the current folder, you’ll see only two folders, main and Feature3 (because.bare and.git are hidden folders/files). Isn’t that refreshing?

ls -l

# -------------------------------------------
total 0
drwxr-xr-x  10  rgyb  staff  320 Nov 23 21:44 feature3
drwxr-xr-x  10  rgyb  staff  320 Nov 23 21:36 main


ls -al
# -------------------------------------------
total 8
drwxr-xr-x   6  rgyb  staff  192 Nov 23 21:44  .
drwxr-xr-x   3  rgyb  staff   96 Nov 23 21:14   ..
drwxr-xr-x  12  rgyb  staff  384 Nov 23 21:36  .bare
-rw-r--r--   1  rgyb  staff   16 Nov 23 21:29   .git
drwxr-xr-x  10  rgyb  staff  320 Nov 23 21:44  feature3
drwxr-xr-x  10  rgyb  staff  320 Nov 23 21:36  main
Copy the code

Now we can add/commit/pull/push on our branches without affecting each other

echo "feature3 development" > feature3.yaml
git add feature3.yaml

git commit -m "feat: [JIRA234-feature3] feature3 development"
# ------------------------------------------------------------------
[feature/JIRA234-feature3 aeaac94] feat: [JIRA234-feature3] feature3 development
 1 file changed, 1 insertion(+)
 create mode 100644 feature3.yaml

git push --set-upstream origin feature/JIRA234-feature3
Copy the code

Multi-branch collaborative development is no longer a problem with the four commands of worktree in the previous article:

git worktree add
git worktree list
# ------------------------------------------------------------------------------------------------
/Users/rgyb/Documents/projects/amend-crash-demo/.bare        (bare)
/Users/rgyb/Documents/projects/amend-crash-demo/feature3   aeaac94 [feature/JIRA234-feature3]
/Users/rgyb/Documents/projects/amend-crash-demo/main        82b8711 [main]

git worktree remove
git worktree prune
Copy the code

conclusion

By taking advantage of Bare Repo’s features, we can cleanly manage all worktrees under the current project directory, with multiple branches for collaborative development, like this:

. └ ─ ─ amend - crash - demo ├ ─ ─ feature3 │ ├ ─ ─ the README. Md │ ├ ─ ─ config. The yaml │ ├ ─ ─ feat1. TXT │ ├ ─ ─ feature3. Yaml │ ├ ─ ─ File1. Yaml │ ├ ─ ─ hotfix. Yaml │ ├ ─ ─. Main properties │ └ ─ ─ the main, yaml └ ─ ─ the main ├ ─ ─ the README. Md ├ ─ ─ config. The yaml ├ ─ ─ feat1. TXT └── ├─ ├── ├.org.org.exdirectories, 15 filesCopy the code

If you have an OCD, this is a great idea.

If you want to get a better understanding of the process, you’ll need to look at Git files while using the commands in this article

What’s the problem, message board

reference

  1. www.blopig.com/blog/2017/0…
  2. Lists. MCS. Anl. Gov/pipermail/p…
  3. www.saintsjd.com/2011/01/wha…
  4. Infrequently.org/2021/07/wor…
  5. Morgan.cugerone.com/blog/how-to…

Personal blog: https://dayarch.top

Day arch a soldier | original