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

或者我的选择是:

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


当前回答

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

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_

其他回答

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

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_

我将简单地创建一个新的stash分支,并在不需要该分支时删除该分支。

在机器1上:

git add . // Add work-in-progress job
git checkout -b stash-branch // Create and checkout to stash-branch
git commit -m 'WIP: job description' // Commit message
git push origin stash-branch // Push to remote

在机器2上:

git pull origin stash-branch // Pull the stash-branch
git checkout master // Checkout to working branch
git rebase stash-branch // Rebase the stash-branch
git reset --soft // Equivalent to stash!!
git branch -d stash-branch // Delete branch when not needed from local
git push -d origin stash-branch // Delete branch when not needed from remote

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

我在它自己的分支里做一个特性。分支不会合并到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^,因为那个提交是垃圾。

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

似乎有一个非常巧妙的技巧来解决这个问题。你可以使用git diff >文件。Diff(并提交文件),然后使用git apply file恢复更改。Diff(来自任何地方)来实现相同的结果。

这里也解释了这一点。