在git中,是否可以创建一个存储库,将存储库推到远程存储库,在另一台计算机上检索存储库,并应用存储库?
或者我的选择是:
创建补丁并将补丁复制到另一台计算机,或者 创建一个小分支并将未完成的工作提交给该分支?
在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的原始答案下添加一条评论)
其他回答
我将混合上面的2个答案,只是为了分享我是如何用zsh shell来做的。
非常感谢@Scott Weldon和@sehe对这个问题的回答!我从他们身上学到了很多!!由于这个问题,我还广泛地学习了shell脚本编写!
一个关于代码功能的非常简单的解释
请参考以上的答案链接,以更好地(推荐)理解:
代码的作用:
Stash goes from Machine 1 --> to remote --> to Machine 2
机器1:
1. Push the stash to remote (using for loop for multiple stashes)
机器2:
1. Check stash(es) in remote. (This is just to check whether there are previous stashes in remote. If there are, you must delete them if you don't want them in you stash list. Command for deleting remote stashes are given in bonus)
2. Fetch the stash(es) to a local ref folder named "ref/stashes" in your .git folder in your local repository. (Its like downloading the stashes on your PC)
3. Convert the fetched stash(es) to proper data. (Its like extracting or installing the stashes on your PC)
奖金:
1. Check stash(es) in remote
2. Delete stash(es) from remote
代码:
在机器1上:
1. git push origin $(for sha in $(git rev-list -g stash); do echo $sha:"refs/stashes/$(git rev-parse --short $sha)"; done)
在机器2上:
1. git ls-remote
2. git fetch origin "refs/stashes/*":"refs/stashes/*"
3. for sha in $(git rev-list --no-walk --reverse --glob='refs/stashes/*'); do git stash store --message "$(git show --no-patch --format=format:%s $sha)" $sha; done
奖金:
1. git ls-remote
2. git push origin :refs/stashes/<stashFile-1> :refs/stashes/<stashFile-2>
以上代码用于多个存储,也可以用于一个存储。只需确保您的远程ref/stashes文件夹只有您在本地回购中想要的存储。
似乎有一个非常巧妙的技巧来解决这个问题。你可以使用git diff >文件。Diff(并提交文件),然后使用git apply file恢复更改。Diff(来自任何地方)来实现相同的结果。
这里也解释了这一点。
在其他答案的基础上,如果你在GitHub这样的平台上有一个公共存储库,但不希望你正在进行的更改是公共的,你可以创建一个私有存储库并将其添加为远程存储库。
要同步更改:提交,推到私有远程上的分支,拉到目标设备,并执行软/混合重置。
我会选择第二种方法,尽管不知道为什么不能提交给main/feature分支。也有可能进行“樱桃挑选”。
这是不可能得到它通过fetch或这样,镜像refspec是fetch = +refs/*:refs/*,即使stash是refs/stash,它不会被发送。显式的refs/stash:refs/stash也没有效果!
这只会令人困惑,因为它不会获取所有的存储,只有最新的一个;存储列表是ref refs/stashes的reflog。