理解 git init
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13525629/
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
Understanding git init
提问by Jaanus
What is git init
for exactly? Must I do it once per computer or once per project that uses git? I downloaded my project by git clone
and got it working, but now it is storing my project also to C:/Users/myUser/git
, is that certain folder or I can change it?
究竟是git init
为了什么?我必须每台计算机执行一次还是每个使用 git 的项目执行一次?我下载了我的项目git clone
并让它工作,但现在它也将我的项目存储到C:/Users/myUser/git
,是那个特定的文件夹还是我可以更改它?
I don't really know much about that folder, it seems to be like a local git repo or something, but what "manages" it, or why it is using that path, can you explain it please?
我对那个文件夹不太了解,它似乎像一个本地的 git repo 什么的,但是什么“管理”它,或者它为什么使用那个路径,你能解释一下吗?
This is what I understand, fix me if I am incorrect, need to get facts straight:
这就是我的理解,如果我不正确,请纠正我,需要弄清楚事实:
git init
is for every project- The git folder under "Users" is local repo and everytime i do
git commit
, that folder is updated. - When I do
git push
, it takes from that local repo, and puts to remote repository. - When I want to update to "HEAD", I just do
git pull
git init
适用于每个项目- “用户”下的 git 文件夹是本地存储库,每次我这样做时
git commit
,该文件夹都会更新。 - 当我这样做时
git push
,它从该本地存储库中获取,并放入远程存储库。 - 当我想更新为“HEAD”时,我只是这样做
git pull
回答by slebetman
That's actually a lot of questions and misunderstandings. I'm not sure I'd be able to address them all so I'm only going to address what's directly asked.
这实际上是很多问题和误解。我不确定我是否能够解决所有这些问题,所以我只会解决直接询问的问题。
git init is for every project
Almost correct.
git init
is used to start using git on a project that's not under git. For projects that are already under git you usegit clone
.The git folder under "Users" is local repo
Almost correct. The folder is actually
.git
notgit
. This comes from the unix convention that all files and folders that start with a dot are considered hidden.Secondly, the folder is not under your Users folder. It is under your project folder. So the folder
C:/Users/myUser/
is one project. If this is not your intention then you most likely have accidentally executedgit init
in your User folder.Each project has one
.git
folder in the project's root directory and that is the project's repository. This is one of the reasons git is so fast compared to svn or cvs - the entire repository is processed on the local hard disk without any network traffic.When I do git push , it takes from that local repo, and puts to remote
Correct, but only for repos that have remotes (which are usually repos that you create by using
git clone
to copy a remote repo).Note that the remote repo does not need to be on another machine. You can git clone a project from a local folder into another folder and then you can push changes from the new folder back to the original folder.
The
git clone
command automatically sets up the necessary config for your repo to connect back to a remote. But you can also manually configure a repo set up withgit init
to connect to a remote.what "manages" it
The
.git
folder manages your project's repo. Git doesn't run as a server*. Instead the.git
folder acts as your local 'server' that all the git commands communicate with. Basically, running a git command edits the contents of the.git
folder.*note: Remote repos do run servers so you can connect to them. But technically they're not really gitservers. They're file servers that git can download from and upload to.
git init 适用于每个项目
几乎正确。
git init
用于在不在 git 下的项目上开始使用 git。对于已经在 git 下的项目,您使用git clone
.“用户”下的git文件夹是本地repo
几乎正确。该文件夹实际上
.git
不是git
. 这来自 unix 约定,即所有以点开头的文件和文件夹都被认为是隐藏的。其次,该文件夹不在您的用户文件夹下。它位于您的项目文件夹下。所以文件夹
C:/Users/myUser/
是一个项目。如果这不是您的意图,那么您很可能不小心git init
在您的用户文件夹中执行了。每个项目
.git
在项目的根目录中都有一个文件夹,即项目的存储库。这是 git 与 svn 或 cvs 相比如此之快的原因之一——整个存储库在本地硬盘上处理,没有任何网络流量。当我执行 git push 时,它会从该本地存储库中获取,然后放到远程
正确,但仅适用于具有遥控器的存储库(通常是您通过
git clone
用于复制远程存储库创建的存储库)。请注意,远程仓库不需要在另一台机器上。您可以将项目从本地文件夹 git clone 到另一个文件夹,然后您可以将更改从新文件夹推送回原始文件夹。
该
git clone
命令会自动为您的 repo 设置必要的配置以连接回远程。但是你也可以手动配置一个 repo 设置git init
来连接到远程。什么“管理”它
该
.git
文件夹管理您项目的存储库。Git 不作为服务器运行*。相反,该.git
文件夹充当所有 git 命令与之通信的本地“服务器”。基本上,运行 git 命令会编辑.git
文件夹的内容。*注意:远程存储库确实运行服务器,因此您可以连接到它们。但从技术上讲,它们并不是真正的git服务器。它们是 git 可以下载和上传的文件服务器。
回答by Brian Willis
Your threefour understandings are more or less correct, though you're missing a few details.
你3四所认识都或多或少正确的,但是你错过了几个细节。
git init
initialises (i.e. creates) a repository. Each project should be in its own repository.
git init
初始化(即创建)一个存储库。每个项目都应该在自己的存储库中。
If you downloaded your project using git clone
then you don't need to run git init
again.
如果您使用下载项目,git clone
则无需git init
再次运行。
You should be able to copy your project to another directory without any adverse effects. That path was probably chosen by default. Be sure to move the whole directory. The metadata that git needs to run is stored in hidden files in the project's directory.
您应该能够将您的项目复制到另一个目录而不会产生任何不利影响。该路径可能是默认选择的。请务必移动整个目录。git 需要运行的元数据存储在项目目录下的隐藏文件中。
Pushing and pulling can get complicated, especially when you're working on a project with others, and when you're using branches. It's not really sensible for me to write out a complete intro to the topic here, so I'd suggest you go read Pro Gitfor a more thorough explanation.
推和拉可能会变得复杂,尤其是当您与其他人一起处理项目时,以及当您使用分支时。在这里写出一个完整的主题介绍对我来说并不明智,所以我建议你去阅读Pro Git以获得更彻底的解释。
回答by user4815162342
git init
is only for when you create your own new repository from scratch. It turns a directory into an empty git repository.
git init
仅适用于从头开始创建自己的新存储库。它将一个目录变成一个空的 git 存储库。
When you use git clone
to clone an existing repository into a new one, git init
is neither necessary nor desired.
当您使用git clone
将现有存储库克隆到新存储库时,git init
既没有必要也不需要。
回答by Aman
@Jaanus just one addition to what @slebetman explained regarding git pull
. It's not exactly syncing but rather fetching the commits which are not on your local. This is more of a corner case, consider the following -
@Jaanus 只是@slebetman 解释的一个补充git pull
。它不是完全同步,而是获取不在本地的提交。这更像是一种极端情况,请考虑以下事项-
Assuming to be dealing with a branch test_branch. A, B and C commits exist for
origin/test_branch
(the branch on your git server) where C is the most recent commit. You have taken a pull and now have A, B and C on your local branch too.Let's say for some reason you had to reset the commit B and force re-write history of
origin/test_branch
leaving the history to be A and C.Now when you perform
git pull
onto your local. It is going to sayeverything already up to date
but notice you have additional changes of commit B. Therefore, do not consider this as a sync operation but more of aget what I don't have
operation.
假设正在处理一个分支 test_branch。A、B 和 C 提交存在于
origin/test_branch
(您的 git 服务器上的分支),其中 C 是最近的提交。您已经接受了,现在您的本地分支上也有 A、B 和 C。假设出于某种原因,您必须重置提交 B 并强制重写将历史记录
origin/test_branch
保留为 A 和 C 的历史记录。现在,当您
git pull
在本地演出时。它会说everything already up to date
但注意你有提交 B 的额外更改。因此,不要将其视为同步操作,而是更多的get what I don't have
操作。
Hope that was helpful.
希望这是有帮助的。
回答by lkq
#!bin/bash
DATE=`date +%m%d%Y.%H%M%S`
TARGET=".$DATE"
DIR=`pwd`
function batch_convert() {
for file in `ls `
do
if [ -d "/"$file ]
then
batch_convert "/"$file
else
dos2unix "/"$file
#echo "/"$file
fi
done
}
##################
echo $TARGET
cd $DIR/DCW
git pull
cd ../
batch_convert DCW
cp -R DCW tmp/$TARGET
cd tmp/$TARGET
find . -type d -name "*git*"| xargs -n20 rm -rf
for db in `cat $DIR/tmp/$TARGET/Dblist` ; do
echo "********** DB IS $db *******"
for dbfolder in `find * -maxdepth 0 -type d` ;do
echo `ls -a`
echo "***** DBFolder is $dbfolder *****"
if [ ! $dbfolder = $db ];then
cp -R $dbfolder $db
find $db -name "*.ctl"| xargs -I '{}' mv '{}' $db/${db}.ctl
fi
done
done
cd ../
tar -cf $TARGET.tar $TARGET