个人 GIT 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Personal GIT repository
提问by Alex Angelini
I use BitKeeper at work, and I would like to have a basic code backup for myself at home (considering I back up very rarely)
我在工作中使用 BitKeeper,我想在家里为自己做一个基本的代码备份(考虑到我很少备份)
// I have never used git before so I need lots of help
// 我以前从未使用过 git,所以我需要很多帮助
I thought it might be a good idea to have a git repository on my home server, and then as I write code for school, work or personally I could simply clone branches from my home server work on them then push them back when I am done my work.
我认为在我的家庭服务器上有一个 git 存储库可能是个好主意,然后当我为学校、工作或个人编写代码时,我可以简单地从我的家庭服务器克隆分支,然后在我完成后将它们推回去我的工作。
// Please correct me if this is not how git works, or how I should be doing this
// 如果这不是 git 的工作方式,或者我应该如何做,请纠正我
I have installed git on my home server and would now like to know the best way to set it up, over ssh, git deamon ??
我已经在我的家庭服务器上安装了 git,现在想知道通过 ssh、git deamon 设置它的最佳方法?
My server's port for ssh is already forwarded must I add new ones for git?
我的服务器的 ssh 端口已经转发,我必须为 git 添加新端口吗?
And finally does this organization make sense to use git or is there a better product for me
最后这个组织是否有意义使用 git 或者有更好的产品对我来说
Thanks
谢谢
回答by semperos
Git is great for version control and "backup" uses. If you want to access files from more than one computer, as you describe, the most pain-free way of getting a Git repository "up and running" is to use Github.com.
Git 非常适合版本控制和“备份”用途。如果您想从多台计算机访问文件,如您所描述的,让 Git 存储库“启动并运行”的最轻松的方法是使用 Github.com。
Github.com provides free space to host public Git repositories (it's geared towards open source software). With a paid plan (starting $7/month), Git will give you space for completely private repositories, which only you (or people you allow) can access.
Github.com 提供免费空间来托管公共 Git 存储库(它面向开源软件)。通过付费计划(每月 7 美元起),Git 将为您提供完全私有的存储库空间,只有您(或您允许的人)才能访问。
Otherwise, you can install Git yourself on your own server, in which case I'd recommend you setup SSH keys and access your repo over SSH (for ease of configuration and security). On your server, you can go into the folder you want to store your repo in and setup an "empty" repo like this:
否则,您可以自己在自己的服务器上安装 Git,在这种情况下,我建议您设置 SSH 密钥并通过 SSH 访问您的存储库(为了便于配置和安全)。在您的服务器上,您可以进入要存储存储库的文件夹并设置一个“空”存储库,如下所示:
git init --bare
Then locally, you can add the location of this new repo by adding a git "remote" to your local codebase:
然后在本地,您可以通过向本地代码库添加一个 git“remote”来添加这个新仓库的位置:
git remote add origin ssh://myserver.com:/var/repos/my_repo.git
Now you have an "origin" server, which you can push to/pull from at will.
现在您有一个“原始”服务器,您可以随意推入/拉出。
Installing Git
安装 Git
If you're on Windows, you should install msysgitand accept the defaults (I like to enable the option for Git to be added to my right-click context menu). I then use the Git Bash command-line utility to use Git, but it comes with a basic GUI tool as well.
如果您使用的是 Windows,则应该安装msysgit并接受默认设置(我喜欢启用将 Git 选项添加到我的右键单击上下文菜单中的选项)。然后我使用 Git Bash 命令行实用程序来使用 Git,但它也带有一个基本的 GUI 工具。
If you're on a Mac, you can download the Mac installer imageand follow its instructions.
如果您使用的是 Mac,则可以下载Mac 安装程序映像并按照其说明进行操作。
If you're on Linux, you can use your package manager to install git. On the most recent version of Ubuntu, for example, you'd run:
如果你在 Linux 上,你可以使用你的包管理器来安装 git。例如,在最新版本的 Ubuntu 上,您将运行:
sudo apt-get install git
Using Git
使用 Git
There's an online Git Bookand the git man pages, but here are some basics.
有一个在线Git Book和git man pages,但这里有一些基础知识。
Make a folder "git-enabled":
创建一个文件夹“git-enabled”:
git init
Add all of your current files in this folder to git's version control:
将此文件夹中的所有当前文件添加到 git 的版本控制:
git add .
Commit these files to your local Git "staging area":
将这些文件提交到您本地的 Git“暂存区”:
git commit -m "My first commit message"
When you're ready, you can push this local staging area to a remote repo, like github or your own server (assumes you already have a remote called "origin" setup, see above):
准备好后,您可以将此本地暂存区推送到远程存储库,例如 github 或您自己的服务器(假设您已经有一个名为“origin”的远程设置,请参见上文):
git push origin master
Which pushes the default "master" branch out to your remote repo. If you need to update your local copy with files from your remote repo's master branch, do a "pull" instead:
这会将默认的“主”分支推送到您的远程存储库。如果您需要使用远程仓库主分支中的文件更新本地副本,请改为执行“拉取”:
git pull origin master
Whenever you do new work, you want to create a branch and work there, so you don't muddle the master branch, and so you can merge your changes once you know they're working. So...
每当你做新工作时,你都想创建一个分支并在那里工作,这样你就不会混淆主分支,所以一旦你知道它们正在工作,你就可以合并你的更改。所以...
To create a new branch and start working in it, you can "checkout" the branch and create it simultaneously with the following:
要创建一个新分支并开始在其中工作,您可以“检出”分支并同时使用以下命令创建它:
git checkout -b new_branch
When you're done in new_branch
, checkout the master branch again and merge your changes:
完成后new_branch
,再次签出 master 分支并合并您的更改:
git checkout master
git merge new_branch
If you want to see a diff of the two branches before merging, use the git diff
command:
如果要在合并前查看两个分支的差异,请使用以下git diff
命令:
git diff master new_branch
To see a log of all your commit, use git log
:
要查看所有提交的日志,请使用git log
:
git log
Press 'q' to exit the log view.
按“q”退出日志视图。
In any given day, those are the commands I use most.
在任何一天,这些都是我最常用的命令。
回答by RDL
You can clone repos or branches to any location to work on them.
您可以将存储库或分支克隆到任何位置以对其进行处理。
I personally go over ssh which does not require a deamon to be setup.
我个人使用 ssh,它不需要设置守护进程。
It will use the same ssh port, nothing new required.
它将使用相同的 ssh 端口,不需要任何新内容。
Here's a tutorial to get you startedand some otherhelpfulresources.