如何将本地文件夹连接到 Git 存储库并开始在分支上进行更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36132956/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:02:59  来源:igfitidea点击:

How to connect local folder to Git repository and start making changes on branches?

gitversion-controlrepositorygitlabcollaboration

提问by codr

I'm new to source control; in the past, I've manually backed up copies of files and made changes on clones then transferred changes manually to master files once debugged. I realize this is similar to how branches work with Git repositories, however I've never used one.

我是源代码控制的新手;过去,我手动备份文件副本并对克隆进行更改,然后在调试后手动将更改传输到主文件。我意识到这类似于分支与 Git 存储库的工作方式,但是我从未使用过。

I downloaded Git and made an account on GitLab, and started a new project. My site is hosted on a local server and my files are saved locally. How do I connect these files to a Git repository and continue developing with branches?

我下载了 Git 并在 GitLab 上注册了一个帐户,然后开始了一个新项目。我的网站托管在本地服务器上,我的文件保存在本地。如何将这些文件连接到 Git 存储库并继续使用分支进行开发?

回答by Harmelodic

To register a project as a local Git repository the first thing you need to do is perform the following command at your project root:

要将项目注册为本地 Git 存储库,您需要做的第一件事是在项目根目录下执行以下命令:

git init

This will create a .gitfolder at your project root and will allow you to start using Git in that repository.

这将.git在您的项目根目录下创建一个文件夹,并允许您开始在该存储库中使用 Git。



If you want to "push" your local Git repository to a remote Git server (in your case, to GitLab), you'll need to perform the following command first:

如果要将本地 Git 存储库“推送”到远程 Git 服务器(在您的情况下,是到 GitLab),您需要先执行以下命令:

git remote add origin <Repository_Location>

You can call originwhatever you like, really, but originis the standard name for Git remote repositories. <Repository_Location>is the URL to your remote repository. For example, if I had a new project called MyNewProjectthat I wanted to push to GitLab, I'd perform:

origin实际上,您可以随意调用任何origin名称,但这是 Git 远程存储库的标准名称。<Repository_Location>是远程存储库的 URL。例如,如果我有一个名为MyNewProject我想推送到 GitLab的新项目,我会执行:

git remote add origin https://gitlab.com/Harmelodic/MyNewProject.git

You can then "push" your changes from your local machine to your remote repo using the following command:

然后,您可以使用以下命令将您的更改从本地机器“推送”到远程存储库:

git push origin <branch_name>

where branch name is the name of the branch you want to push, e.g. master.

其中分支名称是您要推送的分支的名称,例如master.



You can find a good beginners guide to Git here.

你可以在这里找到一份很好的 Git 初学者指南