简体   繁体   中英

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?

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.


So just make a commit, give it a useful message like wip , and push. 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 . NB. the pushtemp alias force-pushes to the temp branch, overwriting anything that is different on your remote. It's easy to lose work that way. Remove the + to do a normal push.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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