简体   繁体   中英

git equivalent of 'hg share'?

Is there any way to share the history of a repository in git without having to clone the whole folder again? Something equivalent to Mercurial's

hg share project project-branch

https://www.mercurial-scm.org/wiki/ShareExtension

If I'm understanding the documentation for hg share , the closest Git equivalent would be git worktree , a new feature in version 2.5:

Manage multiple working trees attached to the same repository.

A git repository can support multiple working trees, allowing you to check out more than one branch at a time. With git 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.

I believe it's still in beta, and there is an explicit warning not to use it with submodules.

Running git worktree add ../some/path/ branch from a repository creates a new working copy with branch checked out at ../some/path/ .

A longer overview of this new command can be found on GitHub's blog .

was too long to comment, so new answer:

This flow I find totally normal in hg - while in git it is not possible, not with worktree not with any other command as it seems(?)

3 bash-4.2# cd m/
3 bash-4.2# hg init
3 bash-4.2# cd ..
3 bash-4.2# hg share m pm1
updating working directory
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
3 bash-4.2# hg share m pm2
updating working directory
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
3 bash-4.2# cd pm1/
3 bash-4.2# touch foo
3 bash-4.2# hg add foo
3 bash-4.2# hg commit -m test foo
3 bash-4.2# cd ../pm2/
3 bash-4.2# hg log
changeset:   0:d5e4a426dca0
tag:         tip
user:        xxx
date:        Wed Apr 10 15:32:32 2019 +0200
summary:     test

3 bash-4.2# ls
3 bash-4.2# hg summary
parent: -1:000000000000  (no revision checked out)
branch: default
commit: (clean)
update: 1 new changesets (update)     # <--------------- ! :-)
3 bash-4.2# hg up
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
3 bash-4.2# ls
foo

In git they do not support that flow. It allows the same thing only checked out ONCE - and their justification (unclean worktrees) does not convince me very much since in hg it is also possible, as you can see.

Also in hg, to update the shared one you, well, update it (hg pull).

In git you have to say:

git fetch origin +refs/heads/*:refs/heads/* --prune

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM