Sunday, November 28, 2021

git-worktree

 


 Since 2015 git added a command git-worktree. This command could be used  when you have a repository  and each branch represent an old Versions of the code, one feature_branch or simple using the main branch.  This command generate a new tree, and you can avoid doing checkout on branches every  time you have to do an urgent bug fix or edit without doing changes on the main tree locally. 

Advantages.

  •     the Time spent by you IDE to reindex the switched branch, so you save a little of Time If you use some jetbrains IDE or eclipse.
  •     No need to fetch ,if you don't want to.
  •     By each tree you Will have a new project in the IDE .
  •     This command use a new directory un your file system, AND use the same .git/config file.
  •     You could compare the code by tree, branches, and directories.


In the old fashion way you Will have to clone each repo in diferents directories, and check or set manually  same config on .git/config,  each directory Will be a project in you IDE. In this way you only could compare by directory and by branch.

Another way was use git stash to save what are you working on, and do the checkout to the branch version  with the bug on production, this way you have a Lot of context switch (to make an hotfix) , cos you Will need to do the reindex on you IDE (in case you use eclipse or some jetbrains IDE ), and later checkout to your branch_feature, unstash, AND again reindex, and always don't forget to clean cache!

Some examples:


(base) ┌─[j3nnn1@caracas] - [~/git/tools] - [Sun Nov 28, 13:26]
└─[$] <git:(master)> git worktree add /home/j3nnn1/git/tools2
Preparing worktree (new branch 'tools2')
Updating files: 100% (2887/2887), done.
HEAD is now at db7534a Update paquetes.txt


When hotfix is ready and the fix it’s in production:


└─[$] <git:(master)> git worktree remove /home/j3nnn1/git/tools2


$ git worktree add <path> <branch>


With -d  throwaway working tree


git worktree prune 


To clean up any stale administrative files.


git worktree lock


To not delete, remove, or prune when gc.worktreePruneExpire passes.


List all trees


└─[$] <git:(master)> git worktree list
/home/j3nnn1/git/tools   db7534a [master]
/home/j3nnn1/git/tools2  db7534a [tools2]


 

No comments:

Post a Comment