是否可以将 git stash 推送到远程存储库?

发布于 2024-08-07 03:52:27 字数 146 浏览 10 评论 0原文

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

或者我的选择是:

  • 创建补丁并将补丁复制到另一台计算机,或者
  • 创建一个次要分支并将不完整的工作提交到该分支?

In git, is it possible to create a stash, push the stash to a remote repository, retrieve the stash on another computer, and apply the stash?

Or are my options:

  • Create a patch and copy the patch to the other computer, or
  • Create a minor branch and commit the incomplete work to that branch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

一杯敬自由 2024-08-14 03:52:27

注意:我刚刚用 24 小时的 git-fu 重写了这个答案:)
在我的 shell 历史中,整个 shebang 现在是三行单行代码。不过,为了您的方便,我已将它们解压缩。

这样,我希望您能够看到我是如何做事的,而不必只是盲目地复制/粘贴内容。


这是一步一步的。

假设源位于包含存储的 ~/OLDREPO 中。创建一个不包含存储的测试克隆:

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

Note: I've just rewritten this answer with 24 hours more git-fu under my belt :)
In my shell history, the whole shebang is now three one-liners. However, I've uncondensed them for your convenience.

This way, I hope you will be able to see how I did things, instead of just having to blindly copy/paste stuff.


Here is step by step.

Assume is source in ~/OLDREPO containing stashes. Create a TEST clone containing no stashes:

cd ~/OLDREPO
git clone . /tmp/TEST

Push all the stashes as temp branches:

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

Loop on the receiving end to transform back into stashes:

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

Cleanup your temporary branches if you will

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

Do a git stash list and you will something like this:

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

On the original repository, the same looked like

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
梦一生花开无言 2024-08-14 03:52:27

不可能通过 fetch 等方式获取它,镜像 refspec 是 fetch = +refs/*:refs/*,即使 stash 是 refs/stash 它也不会不被发送。显式的 refs/stash:refs/stash 也没有效果!

无论如何,这只会令人困惑,因为这不会获取所有的藏品,只能获取最新的藏品;存储列表是引用refs/stashesreflog

It's not possible to get it via fetch or so, the mirror refspec is fetch = +refs/*:refs/*, and even though stash is refs/stash it doesn't get sent. An explicit refs/stash:refs/stash has no effect either!

It would only be confusing anyway since that wouldn't fetch all stashes, only the latest one; the list of stashes is the reflog of the ref refs/stashes.

挽清梦 2024-08-14 03:52:27

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

我正在其自己的分支中开发一项功能。该分支不会合并到 master 中并推送,直到它完成或者我已经做出了我愿意向公众展示的承诺。因此,当我想要将非暂存更改传输到另一台计算机时,我要做的是:

  • 进行提交,并带有提交消息
    例如“[non-commit] FOR TRANSFER ONLY”,其中包含您要传输的内容。
  • 登录另一台计算机。
  • 然后执行:

    git pull ssh+git://<用户名>@<域>/path/to/project/ rb:lb

    如果您以不同的方式访问存储库,则 URL 可能会有所不同。这会将来自该 URL 的更改从远程分支“rb”拉入本地分支“lb”。请注意,我有一个在自己的计算机上运行的 ssh 服务器,并且可以通过这种方式访问​​存储库。

  • git reset HEAD^(隐含--mixed

    这会将 HEAD 重置为指向“[non-commit]”提交之前的状态。

来自 git-reset(1):
--mixed:重置索引但不重置工作树(即保留更改的文件但不标记提交)[...]”

因此您将对文件进行的更改位于最后,但没有提交到 master,也不需要存储。

然而,这将要求您在进行“[非提交]”的存储库中进行git reset --hard HEAD^,因为该提交是垃圾。

I'm a little late to the party, but I believe I found something that works for me regarding this and it might for you too if your circumstances are the same or similar.

I'm working on a feature in its own branch. The branch isn't merged into master and pushed until its finished or I've made commits that I feel comfortable showing to the public. So what I do when I want to transfer non-staged changes to another computer is:

  • 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.

From git-reset(1):
"--mixed: Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) [...]"

So you will have your changes to the files in the end, but no commits are made to master and no need for a stash.

This will however require you to git reset --hard HEAD^ in the repository in which you made the "[non-commit]", since that commit is garbage.

我不咬妳我踢妳 2024-08-14 03:52:27

虽然有点晚了,但这个答案可能会对某人有所帮助。我想知道这一点,因为我希望能够推送正在进行的功能/错误/任何内容,并在另一台计算机上从同一点工作。

对我有用的是提交我正在进行的代码(在我单独工作的分支中)。当我到达另一台计算机时,进行拉取,然后撤消提交:

git reset --soft HEAD^

继续按原样工作,所有正在进行的更改都在那里,未提交且未暂存。

希望有帮助。

It's a little late, but this answer might help someone. I wanted to know this because I wanted to be able to push an in-progress feature/bug/whatever and work from the same point on another computer.

What works for me is to commit my in-progress code (in a branch that I'm working on alone). When I get to my other computer, do a pull, then undo the commit with:

git reset --soft HEAD^

Continue working as you were, with all your in-progress changes there, uncommitted, and unstaged.

Hope it helps.

婴鹅 2024-08-14 03:52:27

当前接受的答案在技术上是正确的,您不能直接告诉 Git 将所有存储推送到远程,然后将所有内容拉到另一台计算机上的本地存储中。

虽然当前最高投票的答案应该有效,但我不喜欢它创建一堆临时分支,并且它需要手动检查存储提交并将其保存为存储,这可能会导致诸如 此评论提到,并导致重复的On(无分支):在测试中:。当然一定有更好的方法!

因此,虽然您不能直接推送存储,但存储只是一次提交(实际上是两次提交),并且根据 git push 手册页,您可以推送提交:

通常是您想要推送的分支的名称,但它可以是任意任意的“SHA-1 表达式”...

我选择将存储推送到 refs/stashes/* 这样我就不会用额外的分支弄乱我的遥控器。所以我可以通过以下方式做到这一点:(

git push origin stash@{0}:refs/stashes/$(git rev-parse --short stash@{0})

rev-parse 命令获取存储的短哈希,这对于存储库来说是唯一的。)

接下来,我需要从另一台计算机获取存储。默认情况下,Git 仅获取分支,因此我需要专门获取存储:

git fetch origin refs/stashes/*:refs/stashes/*

现在将存储提交转换回实际存储。如前所述,虽然我可以像往常一样检查存储提交、重置和存储,但我不喜欢它需要额外的步骤,或者它可能不会维护存储的索引状态。我在网上寻找一种自动执行此操作的方法,但我的搜索失败了。最后我查看了 git stash 的手册页,在那里我发现了这个:

创建
创建一个存储(这是一个常规提交对象)并返回其对象名称,而不将其存储在 ref 命名空间中的任何位置。这对于脚本很有用。这可能不是您想要使用的命令;请参阅上面的“保存”。

商店
将通过 git stash create 创建的给定存储(这是一个悬空合并提交)存储在存储引用中,更新存储引用日志。这对于脚本很有用。这可能不是您想要使用的命令;请参阅上面的“保存”。

因为我已经提交了,所以 store 听起来就像我想要的。所以我可以这样做:

git stash store --message "$(git show --no-patch --format=format:%s <SHA>)" <SHA>

用刚刚获取的存储替换

git show 命令从存储提交中获取提交消息,用作存储日志的消息。)

存储现在在我的本地存储库中正常显示:

$ git stash list
stash@{0}: On master: temp
...

要清理远程,可以像这样从远程删除存储:

git push origin :refs/stashes/<SHA>

此方法还具有幂等性的优点:如果您再次运行 push 命令,它将报告一切都是最新的. fetch 命令也可以安全地重复运行。虽然存储库将跳过存储与最新存储相同的存储,但它并不能防止旧存储的重复。不过,这可以解决,就像我在 git-rstash 脚本中所做的那样,请参见下文。


为了完成,您还可以轻松推送所有存储(使用

for i in $(seq 0 $(expr $(git rev-list --walk-reflogs --count stash) - 1))
do
  git push origin stash@{$i}:refs/stashes/$(git rev-parse --short stash@{$i})
done

或导入所有获取的存储:

for stash in $(ls .git/refs/stashes)
do
  git stash store --message "$(git show --no-patch --format=format:%s $stash)" $stash
done

我创建了一个 脚本可以作为子命令调用(例如 git rstash push 0),所以我不必记住所有这些。 git-rstash 可以在这里找到。

The currently accepted answer is technically correct, you can't directly tell Git to push all your stashes to a remote, and then pull everything into your local stashes on another computer.

And while the currently top-upvoted answer should work, I didn't like that it creates a bunch of temporary branches, and that it requires manually checking out the stash commit and saving it as a stash, which can lead to issues like this comment mentioned, and leads to a duplicate On (no branch): On testing:. Surely there must be a better way!

So while you can't directly push stashes, a stash is just a commit (actually two commits), and per the git push man page you can push commits:

The <src> is often the name of the branch you would want to push, but it can be any arbitrary "SHA-1 expression"...

I chose to push the stashes to refs/stashes/* so that I wouldn't clutter up my remote with extra branches. So I can do that with:

git push origin stash@{0}:refs/stashes/$(git rev-parse --short stash@{0})

(The rev-parse command gets the short hash of the stash, which will be unique for the repo.)

Next, I need to fetch the stash from the other computer. Git only fetches branches by default, so I need to fetch the stashes specifically:

git fetch origin refs/stashes/*:refs/stashes/*

Now to convert the stash commit back into an actual stash. As mentioned, while I could just check out the stash commit, reset, and stash as usual, I don't like that it requires extra steps, or that it might not maintain the index state for the stash. I was looking online for a way to do that automatically, but my search-fu failed me. Finally I looked through the man page for git stash, where I found this:

create
Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace. This is intended to be useful for scripts. It is probably not the command you want to use; see "save" above.

store
Store a given stash created via git stash create (which is a dangling merge commit) in the stash ref, updating the stash reflog. This is intended to be useful for scripts. It is probably not the command you want to use; see "save" above.

Since I already have the commit, store sounds like what I want. So I can do:

git stash store --message "$(git show --no-patch --format=format:%s <SHA>)" <SHA>

Replacing <SHA> with the stash that was just fetched.

(The git show command gets the commit message from the stash commit, to use as the message for the stash log.)

The stash now shows up as normal in my local repo:

$ git stash list
stash@{0}: On master: temp
...

To clean up the remote, the stashes can be deleted from the remote like so:

git push origin :refs/stashes/<SHA>

This method also has the benefit of being idempotent: if you run the push command again, it will report Everything up-to-date. The fetch command can also be safely run repeatedly. While the stash store will skip storing the stash if it is the same as the most recent stash, it doesn't prevent duplicates of older stashes. This can be worked around though, as I do in my git-rstash script, see below.


For completion, you can also easily push all stashes (with ):

for i in $(seq 0 $(expr $(git rev-list --walk-reflogs --count stash) - 1))
do
  git push origin stash@{$i}:refs/stashes/$(git rev-parse --short stash@{$i})
done

or import all fetched stashes:

for stash in $(ls .git/refs/stashes)
do
  git stash store --message "$(git show --no-patch --format=format:%s $stash)" $stash
done

I've created a script that can be called as a subcommand (e.g. git rstash push 0) so I don't have to remember all this. git-rstash can be found here.

泪之魂 2024-08-14 03:52:27

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

此处也对此进行了解释。

There seems to be a very neat trick to solve this. you can use git diff > file.diff (and commit the file) , then restore the changes using git apply file.diff (from anywhere) to achieve the same result.

This was explained here as well.

錯遇了你 2024-08-14 03:52:27

我会选择第二种方法,尽管不知道为什么你不能将其提交到主/功能分支。也可以进行樱桃采摘。

I'd go with second approach although no idea why you can't commit it to main/feature branch. It is possible to do cherry-picking too.

北斗星光 2024-08-14 03:52:27

据我所知,隐藏的整个想法就是将一些不那么重要的东西隐藏在本地地毯下。没有人应该知道你最喜欢的垃圾;-) 唯一的“但是”是:但是如果我在几个工作站上开发呢?那么 scp 就好多了。

AFAIK the whole idea of stash is to hide something not-so-important under the local carpet. Nobody should know about your favorite crap ;-) The only "but" is: But if I develop on a couple of workstations? Then scp is way better.

云淡风轻 2024-08-14 03:52:27

我只需创建一个新的存储分支,并在不需要该分支时将其删除。

在机器 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

I would simply create a new stash branch and the remove whenever that branch is not required.

On Machine 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

On Machine 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
绅刃 2024-08-14 03:52:27

我将混合上面的 2 个答案,只是为了分享我如何使用 zsh shell 做到这一点。

非常感谢 @Scott Weldon (answer-link) 和 @sehe (answer-link) 了解他们对此问题的答案!我从他们身上学到了很多!!由于这个问题,我还广泛了解了 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 文件夹中仅包含您想要在本地存储库中存储的内容。

I am going to mix 2 answers from above just to share how I do it with zsh shell.

A huge thanks to @Scott Weldon (answer-link) and @sehe (answer-link) for their answers to this question! I learned a lot from them!! Also I learned about shell scripting extensively thanks to this problem!


A very simple explanation of what the codes does

Please refer to above answer-links for better (recommended) understanding.:

What the code does:

Stash goes from Machine 1 --> to remote --> to Machine 2

Steps On Machine 1:

1. Push the stash to remote (using for loop for multiple stashes)

Steps On Machine 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)

bonus:

1. Check stash(es) in remote
2. Delete stash(es) from remote 

Codes:

On Machine 1:

1. git push origin $(for sha in $(git rev-list -g stash); do echo $sha:"refs/stashes/$(git rev-parse --short $sha)"; done)

On Machine 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

bonus:

1. git ls-remote
2. git push origin :refs/stashes/<stashFile-1> :refs/stashes/<stashFile-2>

The above codes are for multiple stashes and can be used for one stash too. Just make sure that your remote ref/stashes folder has only the stashes you want in your local repo.

玩心态 2024-08-14 03:52:27

请参阅下面的两个单行代码,我成功地将所有存储从多个源存储库推送到远程主机上的单个目标存储库。

这是基于sehe的原始答案艾伦·克鲁格的评论基思·罗伯逊的评论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 的原始答案下添加评论)

See below two one-liners I successfully used to push all stashes from multiple source repo's to a single destination repo on a remote host.

This is based on sehe's original answer merged with Alan Krueger's comment, Keith Robertson's comment, Sebastian Schrader's comment and by adding the remote host: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

(I don't have enough reputation points yet, otherwise I would have added a comment under sehe's original answer)

美男兮 2024-08-14 03:52:27

以下内容不适用于存储,但适用于工作目录中未提交的更改。它创建一个分支,自动提交所有当前更改,然后推送到远程:

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_

The following does not work with the stash, but with the uncommitted changes in the working dir. It creates a branch, autocommits all current changes, and pushes to the remote:

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 ..."
}

Use like:

commit_and_push_ my-temp-branch
commit_and_push_
鱼忆七猫命九 2024-08-14 03:52:27

如果您有一个(或几个存储)想要发送到另一台机器,您可以将它们保存为分支并推送。当我想将旧机器上用于测试/调试/验证的存储移植到新机器时,我就这样做了。

在源计算机上:

git checkout -b TEMP_STASH_BRACH1       # create new branch
git stash pop                           # unstash the changes you want on other machine. If you don't want the top stash, make sure to specify stash{x} properly
git add .                               # stage content
git commit -m "Stash changes to push"   # commit
git push origin TEMP_STASH_BRACH1       # push

在目标计算机上:

git fetch                               # fetch
git checkout TEMP_STASH_BRACH1          # checkout the branch

假设您希望更改集也作为目标计算机中的存储,请执行以下操作:

git reset HEAD~                         # undoes the last commit, and keeps the changes
git add .                               # stage them back, if not already staged
git stash                               # stash the change set

请注意,源计算机上的存储已弹出,因此不再存在。如果您也想维护该机器的存储,只需在源机器上运行上面的代码片段即可!

如果有多个存储要移植,您可能需要将上述命令放入脚本中。在这种情况下,还要指定要在第一个片段中弹出的确切存储。您可能还想在这些操作之后删除临时分支。

If you have one (or few stashes) you want to send to another machine, you may save them as a branch and push. I've done this when I wanted to port my stash used for testing/debugging/verification on an old machine to a new machine.

On the source machine:

git checkout -b TEMP_STASH_BRACH1       # create new branch
git stash pop                           # unstash the changes you want on other machine. If you don't want the top stash, make sure to specify stash{x} properly
git add .                               # stage content
git commit -m "Stash changes to push"   # commit
git push origin TEMP_STASH_BRACH1       # push

On target machine:

git fetch                               # fetch
git checkout TEMP_STASH_BRACH1          # checkout the branch

Assuming you want the change set also as a stash in the target machine, do the following:

git reset HEAD~                         # undoes the last commit, and keeps the changes
git add .                               # stage them back, if not already staged
git stash                               # stash the change set

Note that your stash on the source machine was popped and so no longer exists. If you want to maintain that machine's stash too, simply run the above snippet on the source machine as well!

You might want to put the above commands in a script if there are multiple stashes to be ported over. In that scenario, also specify the exact stash to pop in the first snippet. You might also want to delete the temporary branches after these operations.

長街聽風 2024-08-14 03:52:27

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

同步更改:提交、推送到专用远程上的分支、拉动目标设备,然后执行软/混合重置。

Building on other answers, if you have a public repository on a platform like GitHub, but don't want your in-progress changes to be public, you can create a private repository and add it as a remote.

To sync changes: commit, push to a branch on the private remote, pull on the target device, and do a soft/mixed reset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文