如何在Eclipse中本地编辑,但使用我编写的基于git的脚本(sync_git_repo_from_pc1_to_pc2.sh)来同步和远程构建
我为此编写的脚本是sync_git_repo_from_pc1_to_pc2.sh。
自述:README_git-sync_repo_from_pc1_to_pc2.md
更新:参见这个替代/竞争对手:GitSync:
如何在SSH上使用Sublime
https://github.com/jachin/GitSync
这个答案目前只适用于使用两台Linux电脑[或者也适用于Mac电脑?]——未在Mac上测试](从一个同步到另一个),因为我用bash写了这个同步脚本。不过,它只是一个git的包装器,所以如果您愿意,可以随意将其转换为跨平台的Python解决方案或其他东西
这并没有直接回答OP的问题,但它是如此接近,我保证它会回答许多其他登陆这个页面的人的问题(实际上,包括我的,因为我是在写我自己的解决方案之前先来这里的),所以我还是把它贴在这里。
我想:
然后在轻量级Linux计算机上使用强大的IDE(如Eclipse)开发代码
在另一台功能更强大的Linux计算机上通过ssh构建代码(从命令行,而不是从Eclipse内部)
让我们把我写代码的第一台计算机称为“PC1”(个人计算机1),把我写代码的第二台计算机称为“PC2”。我需要一个工具来轻松地从PC1同步到PC2。我尝试了rsync,但是对于大型回购来说,它太慢了,而且占用了大量的带宽和数据。
So, how do I do it? What workflow should I use? If you have this question too, here's the workflow that I decided upon. I wrote a bash script to automate the process by using git to automatically push changes from PC1 to PC2 via a remote repository, such as github. So far it works very well and I'm very pleased with it. It is far far far faster than rsync, more trustworthy in my opinion because each PC maintains a functional git repo, and uses far less bandwidth to do the whole sync, so it's easily doable over a cell phone hot spot without using tons of your data.
设置:
Install the script on PC1 (this solution assumes ~/bin is in your $PATH):
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -s "${PWD}/sync_git_repo_from_pc1_to_pc2.sh" ~/bin/sync_git_repo_from_pc1_to_pc2
cd ..
cp -i .sync_git_repo ~/.sync_git_repo
Now edit the "~/.sync_git_repo" file you just copied above, and update its parameters to fit your case. Here are the parameters it contains:
# The git repo root directory on PC2 where you are syncing your files TO; this dir must *already exist*
# and you must have *already `git clone`d* a copy of your git repo into it!
# - Do NOT use variables such as `$HOME`. Be explicit instead. This is because the variable expansion will
# happen on the local machine when what we need is the variable expansion from the remote machine. Being
# explicit instead just avoids this problem.
PC2_GIT_REPO_TARGET_DIR="/home/gabriel/dev/eRCaGuy_dotfiles" # explicitly type this out; don't use variables
PC2_SSH_USERNAME="my_username" # explicitly type this out; don't use variables
PC2_SSH_HOST="my_hostname" # explicitly type this out; don't use variables
Git clone your repo you want to sync on both PC1 and PC2.
Ensure your ssh keys are all set up to be able to push and pull to the remote repo from both PC1 and PC2. Here's some helpful links:
https://help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Ensure your ssh keys are all set up to ssh from PC1 to PC2.
Now cd into any directory within the git repo on PC1, and run:
sync_git_repo_from_pc1_to_pc2
That's it! About 30 seconds later everything will be magically synced from PC1 to PC2, and it will be printing output the whole time to tell you what it's doing and where it's doing it on your disk and on which computer. It's safe too, because it doesn't overwrite or delete anything that is uncommitted. It backs it up first instead! Read more below for how that works.
下面是这个脚本使用的过程(即:它实际上在做什么)
From PC1: It checks to see if any uncommitted changes are on PC1. If so, it commits them to a temporary commit on the current branch. It then force pushes them to a remote SYNC branch. Then it uncommits its temporary commit it just did on the local branch, then it puts the local git repo back to exactly how it was by staging any files that were previously staged at the time you called the script. Next, it rsyncs a copy of the script over to PC2, and does an ssh call to tell PC2 to run the script with a special option to just do PC2 stuff.
Here's what PC2 does: it cds into the repo, and checks to see if any local uncommitted changes exist. If so, it creates a new backup branch forked off of the current branch (sample name: my_branch_SYNC_BAK_20200220-0028hrs-15sec <-- notice that's YYYYMMDD-HHMMhrs--SSsec), and commits any uncommitted changes to that branch with a commit message such as DO BACKUP OF ALL UNCOMMITTED CHANGES ON PC2 (TARGET PC/BUILD MACHINE). Now, it checks out the SYNC branch, pulling it from the remote repository if it is not already on the local machine. Then, it fetches the latest changes on the remote repository, and does a hard reset to force the local SYNC repository to match the remote SYNC repository. You might call this a "hard pull". It is safe, however, because we already backed up any uncommitted changes we had locally on PC2, so nothing is lost!
That's it! You now have produced a perfect copy from PC1 to PC2 without even having to ensure clean working directories, as the script handled all of the automatic committing and stuff for you! It is fast and works very well on huge repositories. Now you have an easy mechanism to use any IDE of your choice on one machine while building or testing on another machine, easily, over a wifi hot spot from your cell phone if needed, even if the repository is dozens of gigabytes and you are time and resource-constrained.
资源:
整个项目:https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
在这个项目的源代码中看到更多的链接和引用。
如何做一个“硬拉”,正如我所说的:我如何迫使“git拉”覆盖本地文件?
相关:
Git仓库在计算机之间同步,当移动?