简体   繁体   English

如何在多台计算机上使用 Git?

[英]How do you use Git with multiple computers?

I'm trying to use git and I have many computers.我正在尝试使用 git 并且我有很多计算机。 I usually work on my desktop, but sometimes use laptops.我通常在台式机上工作,但有时会使用笔记本电脑。

How do you sync source code on git when you go home from work and you didn't finish working on the code?当你下班回家并且没有完成代码工作时,你如何在 git 上同步源代码? Do you send a patch file through email or just commit to git the incomplete codes?你是通过电子邮件发送补丁文件还是只提交不完整的代码?

I agree the latter is more convenient, but I think it makes the git changelog dirty.我同意后者更方便,但我认为它会使 git changelog 变脏。

Commit your work in progress and push it to a remote branch.提交正在进行的工作并将其推送到远程分支。 When you're ready to merge it into your main branch you can use an interactive rebase to rewrite your history and eliminate "work in progress" commits.当您准备好将它合并到您的主分支时,您可以使用交互式 rebase 来重写您的历史记录并消除“正在进行的工作”提交。

Git has a concept of a "remote", such as a central server to push all of your code. Git 有一个“远程”的概念,比如一个中央服务器来推送你的所有代码。 You would push all of your changes to the remote (say from work), then when you get home, you can pull the commits you made from work.您可以将所有更改推送到远程(例如在工作中),然后当您回到家时,您可以拉取您在工作中所做的提交。

GitHub is an excellent hosted Git solution that saves you the pain of having to set it up yourself. GitHub是一个出色的托管 Git 解决方案,可让您免去必须自己设置的痛苦。 You can learn more about remotes, pushing, and pulling from ProGit .您可以从ProGit 中了解有关遥控器、推和拉的 更多信息

As far as "incomplete" work, you could use a branch and push your branch to the remote.至于“不完整”的工作,您可以使用分支并将您的分支推送到远程。 Branches allow you to create a "branch" of code off of the master (sometimes called a trunk).分支允许您从主程序(有时称为主干)创建代码的“分支”。 When you are done, you can merge all of the changes back to master.完成后,您可以将所有更改合并回母版。 You can read more about branching here您可以在此处阅读有关分支的更多信息

Well first of all you probably want to make sure your employer is okay with this... they might not like it if you're taking company IP home with you.首先,您可能想确保您的雇主对此感到满意……如果您将公司 IP 带回家,他们可能不喜欢它。 :-) :-)

The answers about having a dedicated server like GitHub are good, but realize you're limited to either open source (which I doubt your employer would like) or having to pay for a small number of private repositories ($7 for 5 private repos, currently).关于拥有像 GitHub 这样的专用服务器的答案很好,但要意识到您仅限于开源(我怀疑您的雇主会喜欢)或必须支付少量私有存储库(目前 5 个私有存储库 7 美元) )。

IMO the simplest thing to keep a copy of your git repo on your phone or a USB stick, adding the main repo as a remote. IMO 最简单的方法是在手机或 USB 记忆棒上保留 git 存储库的副本,将主存储库添加为远程存储。 Sync this repo with your main one before you leave to go home.在您离开回家之前,将此存储库与您的主要存储库同步。 You don't need to set up a server at all;您根本不需要设置服务器; git works happily at the filesystem level. git 在文件系统级别愉快地工作。

What you really need is a single server side repository that is available publicly.您真正需要的是一个公开可用的单一服务器端存储库。 You can use http://github.com/ for the same.您可以使用http://github.com/进行相同的操作。 Github allows you to create public repositories for free, creating private one's is a paid feature. Github 允许您免费创建公共存储库,创建私有存储库是一项付费功能。 If you wish to setup your own repository server, you can have a look at https://github.com/sitaramc/gitolite it's reasonably easy to setup.如果您想设置自己的存储库服务器,可以查看https://github.com/sitaramc/gitolite ,设置起来相当容易。

Once you have a public repository then you can push from your desktop at work and then when home on your laptop, you can then pull to get the latest.拥有公共存储库后,您可以在工作时从桌面推送,然后在笔记本电脑上回家后,您可以拉取以获取最新信息。

You can also use the git reset command to "erase" all "unfinished" commits that you don't wish to appear in history.您还可以使用git reset命令“擦除”所有您不希望出现在历史记录中的“未完成”提交。

Say you have in a branch master a commit_1.假设您在分支 master 中有一个 commit_1。 Then you commit from different computers save1, save2 and save3 to master, pushing and pulling your work each time you change your computer.然后你从不同的计算机上提交 save1、save2 和 save3 到 master,每次更改计算机时推拉你的工作。 Then with:然后:

git reset --soft <commit_1_hash>

master (and HEAD) will point to commit_1. master(和 HEAD)将指向 commit_1。 But the trick here is that your working tree and your index (staging area) still have the files from save3.但这里的技巧是您的工作树和索引(暂存区)仍然有来自 save3 的文件。 You then immediately commit, for exemple:然后您立即提交,例如:

git commit -m "commit_2"

Now all the saves are not going to appear on the linear history.现在所有的保存都不会出现在线性历史记录中。 Also by leaving a branch pointing to commit_1 you can just use the branch name instead of the hash value.此外,通过留下指向 commit_1 的分支,您可以只使用分支名称而不是哈希值。

If you forget the --soft flag your index will be populated with the commit_1 files, but not your working tree, so you just have to add the files manually with add .如果您忘记了--soft标志,您的索引将填充 commit_1 文件,而不是您的工作树,因此您只需使用 add 手动add文件。 DO NOT run reset with the --hard flag, as you will replace the working tree and lose any reference to save3.不要使用--hard标志运行reset ,因为您将替换工作树并丢失对 save3 的任何引用。 If you are afraid of that just create a new temporary branch pointing to save3, so if anything happens during the process you have a backup reference.如果您害怕,只需创建一个指向 save3 的新临时分支,那么如果在此过程中发生任何事情,您就有一个备份参考。

Also if you mess things up you can run git reflog and look for save3 in there, it should be there for a few months.此外,如果你把事情搞砸了,你可以运行git reflog并在那里寻找 save3,它应该在那里几个月。 More on the git reset command and the similarities and differences with git checkout can be found on Pro Git , specially on the chapter Reset Demystified.更多关于git reset命令以及与git checkout的异同可以在Pro Git上找到,特别是在 Reset Demystified 一章。

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

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