git 工作目录和本地存储库有什么区别?

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

What's the difference between working directory and local repository?

gitgithubrepositoryworking-directory

提问by Zoe

I've created a new GitHub repository. I'm quite confused by working directory and local repository. When working on my own, my stuff all resides on working directory. But when work with repo, I should check my stuff there. But are they the same thing? or should they be separate. Then how to commit my stuff in working directory to my local repo. How to commit my local repo to remote repo. How to do this by Git Bash? Thanks!

我创建了一个新的 GitHub 存储库。我对工作目录和本地存储库感到很困惑。当我自己工作时,我的东西都驻留在工作目录中。但是当使用 repo 时,我应该在那里检查我的东西。但它们是同一回事吗?或者他们应该分开。然后如何将我在工作目录中的东西提交到我的本地仓库。如何将我的本地仓库提交到远程仓库。如何通过 Git Bash 做到这一点?谢谢!

采纳答案by Alexey Voytenko

Working directory is the directory with your source files under git control (in the root of all dirs under control .git file is present). Git is tracking the difference between your working directory and local repository, and between your local repository and (one of) remote repositories.

工作目录是包含 git 控制下的源文件的目录(在所有控制下的目录的根目录中,存在 .git 文件)。Git 正在跟踪您的工作目录和本地存储库之间以及本地存储库和(其中一个)远程存储库之间的差异。

To see what was changed, use $ git status.

要查看更改的内容,请使用 $ git status.

To commit your changes (edits and/or new files) to the local repository, use $ git addand then $ git commit.

要将更改(编辑和/或新文件)提交到本地存储库,请使用$ git add,然后使用$ git commit

To see what was committed use $ git log.

要查看提交的内容,请使用$ git log.

Then, if you want to commit your changed to the remote repository, use $ git push.

然后,如果要将更改提交到远程存储库,请使用$ git push.

Pay attention to what git commands are printing, it often contain advice what to do.

注意 git 命令正在打印什么,它通常包含要做什么的建议。

If you are working with GitHub, their help is also useful: pushing to a remote.

如果您正在使用 GitHub,他们的帮助也很有用:推送到远程.

Good basic information is also in atlassian site.

atlassian 网站上也有很好的基本信息。



FOR EXAMPLE:

例如:

Suppose, you initiated your git repository with $ git initin JavaRepo dir and checkouted there the project with $ git pullor $ git clone. Then JavaRepo should contain .git file (among others git dotted files) - you can check it with $ ls -a. These filesitself are the local git repository, and there is no need to distinguish'working directory' and 'local repository' directory.

假设您$ git init在 JavaRepo 目录中启动了 git 存储库,并在那里使用$ git pull或签出项目$ git clone。然后 JavaRepo 应该包含 .git 文件(以及其他 git dotted 文件)-您可以使用 .git 文件进行检查$ ls -a这些文件本身就是本地git仓库不需要区分“工作目录”和“本地仓库”目录。

So, start working with files in JavaRepo: say you changed file example.java and created new file example2.java. Execute $ git statusin JavaRepo (or in any its subdirs). It should show: 'modified: example.java', 'Untracked files: example2.java'.

因此,开始处理 JavaRepo 中的文件:假设您更改了文件 example.java 并创建了新文件 example2.java。执行 $ git status中JavaRepo(或任何其子目录)。它应该显示:'modified: example.java''Untracked files: example2.java'

Add your files to the staging area with $ git add example.javaand $ git add example2.javacommands from the directory with these files. (Notice that $ git addis both for changed and new files.)

使用这些文件所在目录中的$ git add example.java$ git add example2.java命令将您的文件添加到暂存区。(请注意,$ git add这既适用于已更改的文件,也适用于新文件。)

And finally commit your files by $ git commit -m "example changes"(-m stays for message - comments to the commit).

最后通过$ git commit -m "example changes"(-m 保留消息 - 对提交的评论)提交您的文件。

$ git logshould show this commit.

$ git log应该显示这个提交。

回答by Shubham Sawant

There is a layer between working directory and local repository. It is called staging area. When you add your changed files using 'git add', the changes first gets stored in staging area for checking. Then you can use 'git commit' to further push your changes from staging area to local repo.

工作目录和本地存储库之间有一层。它被称为暂存区。当您使用“git add”添加更改的文件时,更改首先存储在暂存区以供检查。然后您可以使用 'git commit' 将您的更改从暂存区进一步推送到本地存储库。

回答by Anna Skoulikari

The contents of your project folder(the folders and files you find within it) are represented by the working directory.

项目文件夹的内容(您在其中找到的文件夹和文件)由工作目录表示。

The working directory is sort of like a workbench, it's where you work on your files (you edit them, you add new files, you delete files etc.).

工作目录有点像工作台,它是您处理文件的地方(您可以编辑它们、添加新文件、删除文件等)。

On the other hand, the .git folder(which is a hidden folder) represents the repository.

另一方面,.git 文件夹(这是一个隐藏文件夹)代表存储库

Within the .git folder there are two "places" that should be mentioned, the staging area(represented by the index file) and the commit history(represented by the objects folder).

在 .git 文件夹中有两个“地方”需要提及,暂存区(由索引文件表示)和提交历史(由对象文件夹表示)。

The staging areais sort of like a rough draft space. Whenever you are done working on a file (or files) in your working directory, you want to copy them to the staging area (using the git addcommand).

临时区域是有点像一个粗略的空间草案。每当您处理完工作目录中的一个(或多个)文件时,您都希望将它们复制到暂存区(使用git add命令)。

Once you have all the files that you want to update in the next version of your project in the staging area, you are ready to save them in the next version of your project which is called a commit. You do this using the git commitcommand.

一旦您在暂存区的项目的下一个版本中拥有要更新的所有文件,您就可以将它们保存在项目的下一个版本中,该版本称为commit。您可以使用git commit命令执行此操作。

A commitis basically a version of your project and each commit has a 40 character hash (40 letters and numbers) and this hash acts like a name for the commit, it's a way to refer to it.

一个承诺基本上是项目的一个版本,每次提交有40个字符的散列(40个字母和数字),这哈希就像一个名称提交,这是指它的方式。

The commits are in the commit history(objects folder).

提交位于提交历史记录(对象文件夹)中。

So in order to add a file to our repository:

因此,为了向我们的存储库添加文件:

This videoexplains things very simply in a visual manner (also the video right before and right after it help!)

该视频以视觉方式非常简单地解释了事情(前后视频也有帮助!)