如何在本地使用 Git?

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

How can I use Git locally?

gitgithubgithub-for-mac

提问by theonlygusti

I'm working on a project at the moment, which I'd like to eventually make public on github, but, for the moment, needs to remain private.

我目前正在做一个项目,我希望最终在 github 上公开,但目前需要保持私密。

Github needs users to pay in order to host a private repository, which I'm unwilling to do, so just creating a private github repository is not an option for me.

Github 需要用户付费才能托管私有存储库,我不愿意这样做,因此仅创建私有 github 存储库对我来说不是一个选择。

However, I would still like to use git for version tracking etc. whilst I'm working on the project locally, so that when I do eventually put the project on github, all of this information, the project's changes start-to-finish, will be available.

但是,我仍然想使用 git 进行版本跟踪等,而我正在本地处理项目,因此当我最终将项目放在 github 上时,所有这些信息,项目的更改从开始到完成,将可用。

But, I have no clue how to use git without a remote server. I'm wondering now if it just exactly the same, simply without the need for git push.

但是,我不知道如何在没有远程服务器的情况下使用 git。我现在想知道它是否完全相同,只是不需要git push.

The perfect answer for me would be a step-by-step walkthrough, telling me exactly what I should type into the terminal to set up and maintain a local git repository.

对我来说,完美的答案是分步演练,准确地告诉我应该在终端中输入什么来设置和维护本地 git 存储库。

采纳答案by rikard

You only need to pushif you want to use a remote server.

push当您想使用远程服务器时才需要。

When working locally you only need to do the steps

在本地工作时,您只需执行以下步骤

git add
git commit -m "new commit"

And your changes are saved ("committed").

并且您的更改被保存(“提交”)。

Don't git pushafterwards, and there's also no need to git pull, because there's no remote to sync changes with.

git push之后不要,也没有必要git pull,因为没有遥控器可以同步更改。

Type:

类型:

git log

To see that this works (your changes should be recorded)

看看这是否有效(您的更改应该被记录)

回答by jfadich

A remote server is never required. You can just do git initin your project. If you decide to add a remote server later, it will maintain all the history when you push it.

永远不需要远程服务器。你可以git init在你的项目中做。如果您决定稍后添加远程服务器,它会在您推送时保留所有历史记录。

Also if you want you can use Bitbucket or GitLab. Both of them allow private repositories for free.

此外,如果您愿意,您可以使用 Bitbucket 或 GitLab。它们都允许免费使用私有存储库。

回答by nogenius

Actually you just have to run

其实你只需要跑

git init

on your local folder. This will already create you a repository within the existing folder as a minimal setup.

在您的本地文件夹中。这将已经在现有文件夹中为您创建一个存储库作为最小设置。

If you would like to have a setup more similar to a distributed setup with a repository at some other place/server, use

如果您希望设置更类似于在其他地方/服务器上具有存储库的分布式设置,请使用

git init --bare your_project.git

to create a repository (similar to the server side repository), and

创建存储库(类似于服务器端存储库),以及

git clone <path_to_repository>

in the local folder where you would like to work

在您要工作的本地文件夹中

回答by recycler

Just make a local git project and dont push it. You can do it later. Or you make a empty github project and pull the empty project. Now you can work locally and if you are ready you can push it to github.

只做一个本地的git项目,不要推送。你可以稍后再做。或者你创建一个空的 github 项目并拉取空项目。现在你可以在本地工作,如果你准备好了,你可以把它推送到 github。

No worry, just try.

不用担心,试试吧。

回答by Makoto

Git allows you to create a local repository on your machine. Only when you're actually ready to publish it to a remoteis when it becomes available to the public.

Git 允许您在您的机器上创建本地存储库。只有当您真正准备好将其发布到远程时,它才可供公众使用。

Otherwise, it's no different than working with Git without an internet connection; you can still commit, tag, rebase, create branches, and all of that wonderful stuff, but you can't push or pull.

否则,这与在没有互联网连接的情况下使用 Git 没有什么不同;你仍然可以提交、标记、rebase、创建分支以及所有这些美妙的东西,但你不能推或拉。

In the project directory, initialize it just like you would any other Git project.

在项目目录中,像初始化任何其他 Git 项目一样对其进行初始化。

git init

Work on it like you would any other Git project with commits. Since you don't have a remote server to push it to, any attempts to push or pull would fail anyhow.

像处理任何其他带有提交的 Git 项目一样处理它。由于您没有将其推送到的远程服务器,因此任何推送或拉取的尝试都将失败。

When you're ready to create your remote server, GitHub will do a very good job of walking you through what you have to do in order to get that bootstrapped.

当您准备好创建远程服务器时,GitHub 会非常好地引导您完成必须执行的操作以进行引导。

回答by Nosratullah Mohammadi

I'm dealing with the same thing. Unless that I cannot really have a private project on Github, because of unfair sanctions to some country. I don't want to repeat the other answers, because they were straight forward, and I'm doing just the same. To add a bit information, if you don't like terminal environment and you don't want to go through every change and record them by your terminal, simply install Atom editor, install the Github plugin, in newer versions the plugin installed from the beginning and you really don't need to worry about it. When you created a folder you can initialize the git by either the Git icon or simply typing in the terminal at the exact directory :

我正在处理同样的事情。除非我真的不能在 Github 上有一个私人项目,因为对某个国家的不公平制裁。我不想重复其他答案,因为它们很直接,我也在做同样的事情。补充一点信息,如果你不喜欢终端环境并且你不想经历每一个变化并通过你的终端记录它们,只需安装 Atom 编辑器,安装 Github 插件,在较新版本中安装的插件从开始,你真的不需要担心它。创建文件夹后,您可以通过 Git 图标或在终端中直接输入确切目录来初始化 git:

git init

Then, when you add changes to your code or whatever, the git tracks it and you can commit your changes through the Atom editor. enter image description hereThe only problem might be that you cannot use all of the commands that are available via terminal, but it's enough to actually work. But you can fill the gaps by using the terminal when you really need other git commands. This plugin is available on other IDEs as well. And works just fine.

然后,当您向代码或其他内容添加更改时,git 会跟踪它,您可以通过 Atom 编辑器提交更改。在此处输入图片说明唯一的问题可能是您无法使用通过终端提供的所有命令,但这足以实际工作。但是当您确实需要其他 git 命令时,您可以使用终端来填补空白。此插件也可在其他 IDE 上使用。并且工作得很好。