- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定这是否是 Git 支持的东西,但从理论上讲,它似乎对我有用。
我的工作流程经常涉及同时编辑多个分支中的文件。换句话说,我经常想在一个分支中打开几个文件,而我在另一个分支中编辑另一个文件的内容。
我对此的典型解决方案是进行两次结帐,但很遗憾我无法在它们之间共享分支和引用。我想要的是只有两个工作目录由同一个 .git 文件夹管理。
我知道本地 git clone 解决方案(默认情况下,它是硬链接(hard link)共享对象,以及 --shared 选项,它使用原始存储库设置备用对象存储),但这些解决方案只会减少磁盘空间使用,尤其是在 --shared 的情况下,似乎充满了危险。
有没有办法使用一个 .git 文件夹,并有两个工作目录由它支持?或者 Git 是硬编码的以便在任何时候都只 check out 一个工作目录?
最佳答案
Git 2.5 自 2015 年 7 月起提议替代 contrib/workdir/git-new-workdir
: git worktree
参见 commit 68a2e6a 的 Junio C Hamano ( gitster
) 。
release note mentions :
A replacement for
contrib/workdir/git-new-workdir
that does not rely on symbolic links and make sharing of objects and refs safer by making the borrowee and borrowers aware of each other.
git worktree add <path> [<branch>]
Create
<path>
and checkout<branch>
into it. The new working directoryis linked to the current repository, sharing everything except workingdirectory specific files such as HEAD, index, etc.Thegit worktree
section adds:A git repository can support multiple working trees, allowing you to check out more than one branch at a time.
Withgit worktree add
, a new working tree is associated with the repository.This new working tree is called a "linked working tree" as opposed to the "main working tree" prepared by "
git init
" or "git clone
".
A repository has one main working tree (if it's not a bare repository) and zero or more linked working trees.
details:
Each linked working tree has a private sub-directory in the repository's
$GIT_DIR/worktrees
directory.
The private sub-directory's name is usually the base name of the linked working tree's path, possibly appended with a number to make it unique.
For example, when$GIT_DIR=/path/main/.git
the commandgit worktree add /path/other/test-next next
creates:
- the linked working tree in
/path/other/test-next
and- also creates a
$GIT_DIR/worktrees/test-next
directory (or$GIT_DIR/worktrees/test-next1
iftest-next
is already taken).Within a linked working tree:
$GIT_DIR
is set to point to this private directory (e.g./path/main/.git/worktrees/test-next
in the example) and$GIT_COMMON_DIR
is set to point back to the main working tree's$GIT_DIR
(e.g./path/main/.git
).These settings are made in a
.git
file located at the top directory of the linked working tree.When you are done with a linked working tree you can simply delete it.
The working tree's administrative files in the repository will eventually be removed automatically (seegc.pruneworktreesexpire
ingit config
), or you can rungit worktree prune
in the main or any linked working tree toclean up any stale administrative files.
git worktree
"BUGS" 部分需要注意。
The support for submodules is incomplete.
It is NOT recommended to make multiple checkouts of a superproject.
rappazzo
)。
gitster
-- 在
commit a46dcfb 中 merge ,2015 年 10 月 26 日)
worktree
: add 'list
' command
'
git worktree list
' iterates through the worktree list, and outputsdetails of the worktree including the path to the worktree, the currentlychecked out revision and branch, and if the work tree is bare.$ git worktree list/path/to/bare-source (bare)/path/to/linked-worktree abcd1234 [master]/path/to/other-linked-worktree 1234abc (detached HEAD)
There is also porcelain format option available.
The porcelain format has a line per attribute.
- Attributes are listed with a label and value separated by a single space.
- Boolean attributes (like 'bare' and 'detached') are listed as a label only, and are only present if and only if the value is true.
- An empty line indicates the end of a worktree
$ git worktree list --porcelain
worktree /path/to/bare-source
bare
worktree /path/to/linked-worktree
HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
branch refs/heads/master
worktree /path/to/other-linked-worktree
HEAD 1234abc1234abc1234abc1234abc1234abc1234a
detached
gitdir
文件。
pclouds
)(2016 年 1 月 18 日)。
sunshineco
) 。
gitster
-- 在
commit d0a1cbc 中 merge ,2016 年 2 月 10 日)
If you move a linked working tree, you need to update the '
gitdir
' filein the entry's directory.
For example, if a linked working tree is moved to/newpath/test-next
and its.git
file points to/path/main/.git/worktrees/test-next
, then update/path/main/.git/worktrees/test-next/gitdir
to reference/newpath/test-next
instead.
When "
git worktree
" feature is in use, "git branch -d
" alloweddeletion of a branch that is checked out in another worktree.
rhenium
)(2016 年 3 月 29 日)。
sunshineco
) 。
gitster
-- 在
commit 4fca4e3 中 merge ,2016 年 4 月 13 日)
branch -d
: refuse deleting a branch which is currently checked out
When a branch is checked out by current working tree, deleting thebranch is forbidden.
However when the branch is checked out only by other working trees, deleting incorrectly succeeds.
Usefind_shared_symref()
to check if the branch is in use, not justcomparing with the current working tree's HEAD.
rhenium
)。
gitster
-- 在
commit 741a694 中 merge ,2016 年 4 月 18 日)
branch -m
: update all per-worktree HEADs
When renaming a branch, currently only the HEAD of current working treeis updated, but it must update HEADs of all working trees which point atthe old branch.
This is the current behavior, /path/to/wt's HEAD is not updated:
% git worktree list
/path/to 2c3c5f2 [master]
/path/to/wt 2c3c5f2 [oldname]
% git branch -m master master2
% git worktree list
/path/to 2c3c5f2 [master2]
/path/to/wt 2c3c5f2 [oldname]
% git branch -m oldname newname
% git worktree list
/path/to 2c3c5f2 [master2]
/path/to/wt 0000000 [oldname]This patch fixes this issue by updating all relevant worktree HEADswhen renaming a branch.
pclouds
) 。
sunshineco
) 在
Junio C Hamano -- gitster
-- 中 merge ,2016 年 7 月 28 日)
git worktree lock [--reason <string>] <worktree>
git worktree unlock <worktree>
If a linked working tree is stored on a portable device or network sharewhich is not always mounted, you can prevent its administrative files frombeing pruned by issuing the
git worktree lock
command, optionallyspecifying--reason
to explain why the working tree is locked.
<worktree>
: If the last path components in the working tree's path is unique among working trees, it can be used to identify worktrees.
For example if you only have to working trees at "/abc/def/ghi
" and "/abc/def/ggg
", then "ghi
" or "def/ghi
" is enough to point to the former working tree.
lock
选项 。
pclouds
) 。
dt
) 。
peff
) 在
Junio C Hamano -- gitster
-- 中 merge ,2017 年 4 月 26 日)
Allow to lock a worktree immediately after it's created.
This helps prevent a race between "git worktree add; git worktree lock
" and"git worktree prune
".
git worktree add' --lock
相当于
git worktree lock
之后的
git worktree add
,但没有竞争条件。
git worktree move
/
git worktree remove
:
commit e311597 。
--quiet
”选项来减少“
git worktree add
”
devzero2000
) 和
Duy Nguyen ( pclouds
)。
sunshineco
) 在
Junio C Hamano -- gitster
-- 中 merge ,2018 年 8 月 27 日)
worktree
: add--quiet
option
Add the '
--quiet
' option togit worktree
, as for the othergit
commands.
'add
' is the only command affected by it since all other commands, except 'list
', are currently silent by default.
git worktree add
”用于执行“使用 stat 查找可用名称”
mkdir
",这很容易发生竞争。
mkdir
并在循环中对
EEXIST
使用react得到修复。
hramrach
) 在
Junio C Hamano -- gitster
-- 中 merge ,2019 年 4 月 9 日)
worktree
: fixworktree add
race
Git runs a stat loop to find a worktree name that's available andthen does
mkdir
on the found name.
Turn it tomkdir
loop to avoid another invocation of worktree add finding the same free name and creating the directory first.
git branch -D
”免于删除当前检查的分支
jhowtan
) 在
Junio C Hamano -- gitster
-- 中 merge ,2019 年 5 月 8 日)
worktree
: updateis_bare
heuristics
When "
git branch -D <name>
" is run, Git usually first checks if thatbranch is currently checked out.
But this check is not performed if the Git directory of that repository is not at "<repo>/.git
", which is the case if that repository is a submodule that has its Git directory stored as "super/.git/modules/<repo>
", for example.
This results in the branch being deleted even though it is checked out.This is because
get_main_worktree()
inworktree.c
setsis_bare
on aworktree only using the heuristic that a repo is bare if the worktree'spath does not end in "/.git
", and not bare otherwise.
Thisis_bare
code was introduced in 92718b7 ("worktree
: add details to the worktree struct", 2015-10-08, Git v2.7.0-rc0), following apre-core.bare
heuristic.This patch does 2 things:
- Teach
get_main_worktree()
to useis_bare_repository()
instead, introduced in 7d1864c ("Introduce is_bare_repository() and core.bare configuration variable", 2007-01-07, Git v1.5.0-rc1) and updated in e90fdc3 ("Clean up work-tree handling", 2007-08-01, Git v1.5.3-rc4).
This solves the "git branch -D <name>
" problem described above.
However...- If a repository has
core.bare=1
but the "git
" command is being run from one of its secondary worktrees,is_bare_repository()
returns false (which is fine, since there is a worktree available).
And, treating the main worktree as non-bare when it is bare causes issues: for example, failure to delete a branch from a secondary worktree that is referred to by a main worktree's HEAD, even if that main worktree is bare.In order to avoid that, also check
core.bare
when settingis_bare
.
Ifcore.bare=1
, trust it, and otherwise, useis_bare_repository()
.
worktree
” API 可以更好地确定工作树路径。
sunshineco
) 在
Junio C Hamano -- gitster
-- 中 merge ,2020 年 8 月 10 日)
worktree
: drop bogus and unnecessary path mungingSigned-off-by: Eric Sunshine
The content of
.git/worktrees/<id>/gitdir
must be a path of the form "/path/to/worktree/.git
".
Any other content would be indicative of a corrupt "gitdir
" file.To determine the path of the worktree itself one merely strips the "
/.git
" suffix, and this is indeed how the worktree path was determined from inception.However, 5193490442 ("
worktree
: add a function to get worktree details", 2015-10-08, Git v2.7.0-rc0 -- merge listed in batch #7) extended the path manipulation in a mysterious way.
If it is unable to strip "/.git
" from the path, then it instead reports the current working directory as the linked worktree's path:if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
strbuf_reset(&worktree_path);
strbuf_add_absolute_path(&worktree_path, ".");
strbuf_strip_suffix(&worktree_path, "/.");
}This logic is clearly bogus; it can never be generally correct behavior. It materialized out of thin air in 5193490442 with neither explanation nor tests to illustrate a case in which it would be desirable.
It's possible that this logic was introduced to somehow deal with a corrupt "
gitdir
" file, so that it returns some sort of meaningful value, but returning the current working directory is not helpful. In fact, it is quite misleading (except in the one specific case when the current directory is the worktree whose "gitdir
" entry is corrupt).
Moreover, reporting the corrupt value to the user, rather than fibbing about it and hiding it outright, is more helpful since it may aid in diagnosing the problem.Therefore, drop this bogus path munging and restore the logic to the original behavior of merely stripping "
/.git
".
关于git - Git 有多个工作目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6270193/
我时不时地输入“git”,然后想到别的东西,然后输入例如“git checkout master”。当然,这给我留下了 $ git git checkout master git: 'git' is
我做到了 git 克隆 git://foo.git 光盘富 ...编辑文件.. 现在我想重新开始。我不在乎我已经做出的任何改变,但我不想再次克隆整个巨型 foo.git,只是丢失我所有的更改。我怎
我在我的电脑上开发代码,我的计算节点很少。 为了让我的程序保持同步,我决定使用 git。以前,我以一种单向模式使用它来“下推”从 PC 到计算节点的更改。但是时不时遇到计算节点特有的小bug,现场修复
虽然它似乎什么也没做,但它没有给出任何警告或错误消息。有什么想法吗? 最佳答案 来自 Git 源的注释: /* * Read a directory tree. We currently ignor
我知道如何为这样的 HTTPS 请求提供用户名和密码: git clone https://username:password@remote 但我想知道如何像这样向 Remote 提供用户名和密码:
Git GUI、Git Bash 和 Git CMD 之间有什么区别?我是初学者,为了进行安装,我发现自己通常同时使用 git bash 和 git CMD 最佳答案 Git CMD 就像使用 git
有人能告诉我git中文件索引被删除是什么意思吗?这些文件在我的 VS Code 中标记为红色,但我仍然可以修改文件并将更改推送到将反射(reflect)这些更改的远程存储库。我认为这一切都是在我使用命
我通过 git 子树将 GLFV 库添加到项目中,但出现此警告“看起来您的 git 安装或您的 git-subtree 安装已损坏”。还描述了几个原因,为什么这可能是: 如 git --exec-pa
我有需要外部 git 项目的 repo,但我不想使用子模块,因为我想在 github 上存档所有文件,所以我认为我只是将具有 git repo 的整个目录添加到 git 但它不t 添加里面的 .git
我有需要外部 git 项目的 repo,但我不想使用子模块,因为我想在 github 上存档所有文件,所以我认为我只是将具有 git repo 的整个目录添加到 git 但它不t 添加里面的 .git
我一直在阅读一篇文章,作者在其中指示:在现有存储库中创建一个新存储库,并想知道这是否是他忽略的错误。稍后我会与他核实。 这些是我要检查的条件: 将现有目录制作成仓库的条件,并且已经 checkin 主
我确实在不同的计算机上处理相同的项目,我想知道是否有一种方法可以跟踪该 .git 文件夹,这样我就不必在所有本地文件中重新配置配置文件。 我将所有工作推送到 bitbucket。 最佳答案 不,没
这个问题在这里已经有了答案: How does git store files? (3 个答案) 关闭 9 年前。 我为我的许多项目创建了一个远程存储库,所以它是我的push 的目的地。与 git
应该如何在 git 中查看文件内容的完整历史记录? 一个文件在 git 中的历史很短,存储库通过 git-svn 同步,但在 svn 中的历史很长。 git 中的历史记录到达文件移动的位置。要查看历史
我是confused here ... 如何对修改后的文件进行git commit,以及如何对新文件进行git commit? 还有,你如何在git中单独提交文件? 最佳答案 git 提交过程分为两个
正在搜索 throw SO 来寻找答案。遇到这个似乎没有给出任何答案的旧线程。重新触发此线程,希望有人知道! 有人能告诉我 git subtree 和 git filter-branch 的区别吗?为
我想知道是否有一种方法可以避免在每个 Git 命令的开头键入单词 git。 如果有一种方法可以在打开命令提示符进入 “Git 模式” 后只使用一次 git 命令就好了。 例如: git> 之后,我们键
当您修改工作目录中的文件时,git 会告诉您使用“git add”暂存。 当您向工作目录添加新文件时,git 会告诉您使用“git add”开始跟踪。 我对这两个概念有点困惑,因为我假设跟踪文件的更改
为什么 git://有效 $ git clone git://github.com/schacon/grit.git Cloning into 'grit'... ... Checking conne
我在以下沙箱中练习 git:https://learngitbranching.js.org/?NODEMO 我在两个单独的 session 中运行了两组命令。第一组命令顺序如下: git clone
我是一名优秀的程序员,十分优秀!