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

或者我的选择是:

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


当前回答

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

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

其他回答

我将混合上面的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文件夹只有您在本地回购中想要的存储。

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

这是不可能得到它通过fetch或这样,镜像refspec是fetch = +refs/*:refs/*,即使stash是refs/stash,它不会被发送。显式的refs/stash:refs/stash也没有效果!

这只会令人困惑,因为它不会获取所有的存储,只有最新的一个;存储列表是ref refs/stashes的reflog。

我有点晚了,但我相信我找到了一些适合我的东西,如果你的情况相同或相似,它可能也适用于你。

我在它自己的分支里做一个特性。分支不会合并到master中并推送,直到它完成或者我已经提交了我觉得可以向公众展示的内容。所以当我想要将非阶段性的更改转移到另一台计算机时,我所做的是:

Make a commit, with a commit message like "[non-commit] FOR TRANSFER ONLY", featuring the content you want transfered. Login to the other computer. Then do: git pull ssh+git://<username>@<domain>/path/to/project/ rb:lb The URL might differ for you if you access your repository in a different way. This will pull changes from that URL from the remote branch "rb" into the local branch "lb". Note that I have an ssh server running on my own computer, and am able to access the repository that way. git reset HEAD^ (implies --mixed) This resets the HEAD to point to the state before the "[non-commit]" commit.

从git-reset (1): "——mixed:重置索引,但不重置工作树(即,更改的文件被保留,但不标记为提交)[…]"

因此,您最终将对文件进行更改,但不需要提交到master,也不需要存储。

然而,这将需要你git重置——在你做了“[非提交]”的存储库中硬HEAD^,因为那个提交是垃圾。

注意:我刚刚用24小时的时间重写了这个答案:) 在我的shell历史中,整个过程就是三个简单的语句。不过,为了方便起见,我已经将它们解压缩了。

这样,我希望你能看到我是怎么做的,而不是盲目地复制/粘贴东西。


这里是一步一步的。

假设源在~/OLDREPO中包含存储。创建一个TEST克隆,不包含任何存储:

cd ~/OLDREPO
git clone . /tmp/TEST

将所有的存储作为临时分支:

git send-pack /tmp/TEST $(for sha in $(git rev-list -g stash); \
    do echo $sha:refs/heads/stash_$sha; done)

在接收端循环转换回存储:

cd /tmp/TEST/
for a in $(git rev-list --no-walk --glob='refs/heads/stash_*'); 
do 
    git checkout $a && 
    git reset HEAD^ && 
    git stash save "$(git log --format='%s' -1 HEAD@{1})"
done

如果愿意,清理临时分支

git branch -D $(git branch|cut -c3-|grep ^stash_)

做一个git藏匿清单,你会像这样:

stash@{0}: On (no branch): On testing: openmp import
stash@{1}: On (no branch): On testing: zfsrc
stash@{2}: On (no branch): WIP on sehe: 7006283 fixed wrong path to binary in debianized init script (reported as part of issue
stash@{3}: On (no branch): WIP on debian-collab: c5c8037 zfs_pool_alert should be installed by default
stash@{4}: On (no branch): WIP on xattrs: 3972694 removed braindead leftover -O0 flag
stash@{5}: On (no branch): WIP on testing: 3972694 removed braindead leftover -O0 flag
stash@{6}: On (no branch): WIP on testing: db9f77e fuse_unmount_all could be starved for the mtx lock
stash@{7}: On (no branch): WIP on xattrs: db9f77e fuse_unmount_all could be starved for the mtx lock
stash@{8}: On (no branch): WIP on testing: 28716d4 fixed implicit declaration of stat64
stash@{9}: On (no branch): WIP on emmanuel: bee6660 avoid unrelated changes

在最初的存储库中,看起来是一样的

stash@{0}: WIP on emmanuel: bee6660 avoid unrelated changes
stash@{1}: WIP on testing: 28716d4 fixed implicit declaration of stat64
stash@{2}: WIP on xattrs: db9f77e fuse_unmount_all could be starved for the mtx lock
stash@{3}: WIP on testing: db9f77e fuse_unmount_all could be starved for the mtx lock
stash@{4}: WIP on testing: 3972694 removed braindead leftover -O0 flag
stash@{5}: WIP on xattrs: 3972694 removed braindead leftover -O0 flag
stash@{6}: WIP on debian-collab: c5c8037 zfs_pool_alert should be installed by default
stash@{7}: WIP on sehe: 7006283 fixed wrong path to binary in debianized init script (reported as part of issue #57)
stash@{8}: On testing: zfsrc
stash@{9}: On testing: openmp import