在git中,是否可以创建一个存储库,将存储库推到远程存储库,在另一台计算机上检索存储库,并应用存储库?

或者我的选择是:

创建补丁并将补丁复制到另一台计算机,或者 创建一个小分支并将未完成的工作提交给该分支?


当前回答

请参阅下面两个一行程序,我成功地使用它们将所有存储从多个源回购推到远程主机上的单个目标回购。

这是基于sehe的原始答案,结合Alan Krueger的评论,Keith Robertson的评论,Sebastian Schrader的评论,并添加远程主机:dir。

# on each src_host:repo_dir
git send-pack <dst_host>:<dst_repo_dir> $(for sha in $(git rev-list -g stash); do echo $sha:refs/heads/stash_$sha; done)

# on the dst_host:repo_dir
for a in $(git rev-list --no-walk --glob='refs/heads/stash_*' | tac); do git checkout $a && git reset HEAD^ && git update-ref --create-reflog -m "$(git show -s --format=%B $a)" refs/stash $a && git restore . && git branch -D stash_$a; done; git checkout master

(我还没有足够的声望点,否则我会在sehe的原始答案下添加一条评论)

其他回答

请参阅下面两个一行程序,我成功地使用它们将所有存储从多个源回购推到远程主机上的单个目标回购。

这是基于sehe的原始答案,结合Alan Krueger的评论,Keith Robertson的评论,Sebastian Schrader的评论,并添加远程主机:dir。

# on each src_host:repo_dir
git send-pack <dst_host>:<dst_repo_dir> $(for sha in $(git rev-list -g stash); do echo $sha:refs/heads/stash_$sha; done)

# on the dst_host:repo_dir
for a in $(git rev-list --no-walk --glob='refs/heads/stash_*' | tac); do git checkout $a && git reset HEAD^ && git update-ref --create-reflog -m "$(git show -s --format=%B $a)" refs/stash $a && git restore . && git branch -D stash_$a; done; git checkout master

(我还没有足够的声望点,否则我会在sehe的原始答案下添加一条评论)

在其他答案的基础上,如果你在GitHub这样的平台上有一个公共存储库,但不希望你正在进行的更改是公共的,你可以创建一个私有存储库并将其添加为远程存储库。

要同步更改:提交,推到私有远程上的分支,拉到目标设备,并执行软/混合重置。

有点晚了,但这个答案可能会帮助到一些人。我之所以想知道这些,是因为我想在同一时间点在另一台电脑上发布正在开发的功能/bug/任何东西。

对我有效的是提交我正在进行的代码(在我独自处理的分支中)。当我到达另一台计算机时,执行拉取,然后撤销提交:

git reset --soft HEAD^

继续像以前那样工作,所有正在进行的更改都在那里,没有提交,也没有分期。

希望能有所帮助。

下面的代码不适用于隐藏,但适用于工作目录中未提交的更改。它创建一个分支,自动提交所有当前的更改,并推送到远程:

commit_and_push_ ( ) {
    # This will:
    #  1. checkout a new branch stash-XXX
    #  2. commit the current changes in that branch
    #  3. push the branch to the remote
    local locbr=${1:-autostash-XXX}
    git checkout -b $locbr
    git add .
    git commit -a -m "Automatically created commit"
    git push origin $locbr
    echo "Autocommitted changes in branch $locbr ..."
}

使用:

commit_and_push_ my-temp-branch
commit_and_push_

我会选择第二种方法,尽管不知道为什么不能提交给main/feature分支。也有可能进行“樱桃挑选”。