简体   繁体   English

如何使用 git 将未提交的更改从一台机器传输到另一台机器?

[英]How to use git to transfer uncommitted changes from one machine to another?

I often work on my desktop and end up with changes not worthy of a commit.我经常在我的桌面上工作,最终做出不值得提交的更改。 I want to then switch to my laptop and continue work where I left off on my desktop (including all uncommitted changes).然后我想切换到我的笔记本电脑并继续我在桌面上停止的工作(包括所有未提交的更改)。 How do I best do this with git?我如何最好地使用 git 做到这一点?

I almost, like, wanna do a temporary commit, pull that, then undo the commit both locally and remotely as if it never ever happened.我几乎,就像,想做一个临时提交,拉那个,然后在本地和远程撤消提交,就好像它从未发生过一样。 Is this the approach or is it something else?这是方法还是其他? And how do I do this?我该怎么做?

Yes, what you've described is exactly what you'd need to do.是的,您所描述的正是您需要做的。

The notion of Git doing anything with "uncommitted changes" makes no sense at all, because commits are the only thing Git traffics in. Therefore you must commit in order to do the transfer. Git 对“未提交的更改”进行任何操作的概念根本没有意义,因为提交是 Git 传输的唯一内容。因此,您必须提交才能进行传输。


So just make a commit, give it a useful message like wip , and push.所以只需提交,给它一个有用的消息,比如wip ,然后推送。 Once you've pulled onto the other machine, if you don't want to preserve that commit, you can "undo" it as a commit by saying一旦你拉到另一台机器上,如果你不想保留那个提交,你可以通过说“撤消”它作为一个提交

git reset @~1

...but be advised that if you do that, then later on you will need to force push in order to carry on. ...但请注意,如果您这样做,那么稍后您将需要强制推动才能继续。


Perhaps a better approach, because it avoids the force push, is to make a temporary branch:也许更好的方法是创建一个临时分支,因为它避免了强制推送:

git switch -c wip
git add .
git commit -m wip
git push

# and on the other machine

git fetch origin
git switch wip
git reset main # or wherever you were working when you created wip
git switch main # or wherever you were working
git branch -D wip

You can always just create a new commit and push that to a (temorary) branch.您始终可以创建一个新提交并将其推送到(临时)分支。 As matt noted, you should not do this on a shared branch and you have to rewind your local branch after the push:正如马特所指出的,您不应该在共享分支上执行此操作,并且您必须在推送后回退本地分支:

git commit -am 'temp work'
git push origin HEAD:refs/heads/temp
git reset HEAD^

At least the rewinding part could be replaced by a clever alias:至少倒带部分可以用一个聪明的别名代替:

git config --global alias.pushtemp '!git add -u && git push origin +$(git write-tree):refs/heads/temp'

And then just call git pushtemp .然后只需调用git pushtemp NB.注意。 the pushtemp alias force-pushes to the temp branch, overwriting anything that is different on your remote. pushtemp别名强制推送到temp分支,覆盖遥控器上的任何不同内容。 It's easy to lose work that way.这样很容易丢掉工作。 Remove the + to do a normal push.删除+进行正常推送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM