`
hongtoushizi
  • 浏览: 376557 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Creating a Shared Repository; Users Sharing The Repository

    博客分类:
  • git
git 
阅读更多

转载自:  http://www.gitguys.com/topics/creating-a-shared-repository-users-sharing-the-repository/

Commands discussed in this section:

  • git init –bare
  • git clone
  • git remote
  • git pull
  • git push

Scenario: Example Remote Repository

Let’s set up our own little “remote” repository and then share it. (The repository will be “remote” to the users sharing it.)

In these examples, the other users sharing the repository will not be very remote since the repository will be on the same disk as the users’ home directories. But the git workflow and commands are identical, whether the users and repositories are just a few millimeters away on the same disk, or on a remote network across the world.

Creating The Shared Repository

We’ll have the repository created by the user gitadmin. The gitadmin‘s repository will be be the repository where everybody on the project both publishes their work and also retrieves the latest work done by others.

The scenario:

  • gitadmin will create a repository.
  • Other users, like Amy and Zack will then get (“git clone”) copies of gitadmin‘s remote repository.
  • Changes will be pulled and pushed to and from gitadmin‘s repository.

Create Shared Repositories “Bare”

If you are creating a git repository for only your own use on projects or days when you just don’t feel like sharing, you type:

gitadmin$ git init project1
Initialized empty Git repository in /home/gitadmin/project1/.git/

However, if you are creating a git repository for sharing with git clone/pull/fetch/push, Use the –bare option to git init:

gitadmin$ git init --bare project1.git
Initialized empty Git repository in /home/gitadmin/project1.git/

If you want to know why, see Shared Repositories Should Be Bare Repositories.

Bare Repositories End in “.git”

You might have noticed the –bare repository created above ended in .git. By convention, bare git repositories should end in .git. For example,project1.git or usplash.git, etc. The .git ending of a directory signals to others that the git repository is bare.

Amy is ready to add to the remote repository

In our example, since Amy’s name begins with the first letter of the alphabet, she gets to work on the repository first.

Amy clones it:

amy$ git clone file:///home/gitadmin/project1.git
Initialized empty Git repository in /home/amy/project1/.git/
warning: You appear to have cloned an empty repository.

Git just told us the repository that Amy just cloned is empty.

We can now start creating files and publishing (“git push“) them to the shared repository.

Amy wants to see if there are any branches in the repository she just retrieved/cloned:

amy$ cd project1
amy$ git branch
amy$

The empty output from the git branch command showed are no branches in the new repository.

Amy creates her first file and commit’s the new file to the repository.

amy$ echo The beginnings of project1 > amy.file
amy$ git add .
amy$ git commit -m"Amy's initial commit"
[master (root-commit) 01d7520] Amy's initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 amy.file
amy$ git branch
* master

The cloned, bare repository didn’t have any branches, not even the master repository. When Amy did the first git commit, the master branch was created in Amy’s local repository.

Amy tries to publish her local repository to the remote repository:

amy$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'file:///home/gitadmin/project1.git'

Oops, that didn’t work. The above happens on brand new, completely empty, branchless repositories (immediately after doing the git init –bare …).

Amy’s local repository created the master branch, but the shared repository that gitadmin created does not have any branches on it still.

Amy will take git’s advice and tell git the name of the branch she wants pushed to which remote repository. She must specify both the remote repository name and branch name.

What are the branch and repository names? Amy has been distracted lately and forgot the name of remote repository, so she’ll use the git remotecommand to list the names of her remote repositories:

amy$ git remote
origin

She is shown there is only one remote repository named origin. The default remote repository when you git clone a repository is named origin, so the above output isn’t surprising.

Similarly, Amy can find out the branch name in her local repository by using the git branch command:

amy$ git branch
* master

The branch name master isn’t surprising either, since master is the default branch name for git.

Armed with the remote repository name (origin) and local branch name (master) Amy can now push (publish) the changes.

The git push syntax is:
git push [remote-repository-name] [branch-or-commit-name].
Amy will push the branch named master to the remote repository named origin:

amy$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 245 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To file:///home/gitadmin/project1.git
 * [new branch]      master -> master

The last line above reports a new branch was created: the master branch (referred to in some places as the “source”) on the local repository was mapped to the master branch (referred to in some places as the “destination”) on the remote repository.

Amy will no longer need to type git push origin master, but will be able to type git push, since the master branch now exists on the remote repository named origin:

amy$ git push
Everything up-to-date

Zack wants to play too

Now it’s Zack’s turn to play with the repository. He clones it:

zack$ git clone file:///home/gitadmin/project1.git
Initialized empty Git repository in /home/zack/project1/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
zack$ ls
amy.file

Above, the file Amy added, amy.file is copied from the shared repository to Zack’s working directory.

Zack adds a file and pushes it up to the shared repository:

zack$ cd project1
zack$ echo I am zack > zack.file
zack$ git add .
zack$ git commit -m 'zack initial commit'
[master 05affb3] zack initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 zack.file
zack$ git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To file:///home/gitadmin/project1.git
   01d7520..05affb3  master -> master

Note that Zack didn’t have to do the git push origin master to create the master branch on the remote repository, since Amy had already created themaster branch on the remote repository.

Amy wants to get the latest

amy$ git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From file:///home/gitadmin/project1
   01d7520..05affb3  master     -> origin/master
Updating 01d7520..05affb3
Fast-forward
 zack.file |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 zack.file
amy$ ls
amy.file  zack.file

Things are working pretty well: Amy and Zack are sharing nicely: They are contributing to (“git push“) and receiving from (“git pull“) the shared repository.

The above summarizes how to get moving with shared, remote repostitories. But there’s a lot more fun you can have with remote repositories.

Next: Shared Repositories Should Be Bare Repositories
or
Adding and Removing Branches with Remote Repositories
Previous: Git and Remote Repositories
Related:
Git Remotes Example: Creating a Shared Repository; Users Sharing The Repository
Git Remotes Behind The Scenes: “Tracking Branches” and “Remote-Tracking Branches”
Git Remotes Up Close: The Configuration File – “remote” section
Git Remotes Up Close: The Configuration File – “branch” section
Git Remotes: Fun Commands You Can Use

分享到:
评论

相关推荐

    解决docker报错The push refers to a repository_docker load

    解决docker报错The push refers to a repository

    A work-in-progress repository for breaking the security of iOS 11.2 up to 11.2.6 .zip

    A work-in-progress repository for breaking the security of iOS 11.2 up to 11.2.6 .zip,A work-in-progress repository for breaking the security of iOS 11.2 up to 11.2.6

    The PCI ID Repository v2.0(显卡型号十六进制代码列表)

    同时,通过提供的三个文件(The PCI ID Repository.txt、The PCI ID Repository_3.txt、The PCI ID Repository_2.txt),用户可以对比不同版本,查找最新的显卡信息。 总的来说,了解并利用"The PCI ID Repository ...

    Ansys API example repository for demonstrating the use of REST a

    Ansys API example repository for demonstrating the use of REST and gRPC in different SW languages (Python, C++...)

    Rancher下Docker拉取镜像出现unauthorized to access repository问题.docx

    When we try to pull an image from a private Docker registry, such as Harbor, we may encounter an error message indicating that we are unauthorized to access the repository. This is because the Docker ...

    repository

    repository

    The PCI ID Repository V2.2(显卡型号十六进制代码列表)

    PCI ID(显卡型号十六进制代码列表),linux下查看显卡型号,有时会返回十六进制代码,这个列表是The PCI ID Repository网站的列表,可以根据十六进制代码查询到相应显卡型号。 2.2版本,官方更新日期:2022-05-18 ...

    斯坦福 兔子3d模型 The Stanford 3D Scanning Repository 用于点云配准、表面重建

    斯坦福大学发布的"The Stanford 3D Scanning Repository"是一个广为人知的数据集,其中包含了一个著名的3D模型——斯坦福兔子。这个模型因其精确的细节和广泛的应用而被广大研究人员和开发者所采用,特别是在点云...

    This repository contains the source code of Pidroid, a bot used

    This repository contains the source code of Pidroid, a bot used by the official TheoTown server.

    repository management with nexus

    Nexus Repository Manager OSS 是 Nexus Repository Manager 的开源版本,它们提供了方便的软件组件生命周期管理功能,从开发到部署,再到提供服务,覆盖了软件供应链的各个方面。 Nexus Repository Manager的安装...

    基于PowerDesigner Repository建立模型库

    基于 PowerDesigner Repository 建立模型库 PowerDesigner 是 Sysbase 公司的一款优秀产品,它提供了强大的模型设计和版本控制功能。然而,在实际应用中,我们发现很多公司对设计模型的版本控制并不是很好,导致...

    10.1-Repository

    在.NET Core开发中,Repository模式是一种常见的设计模式,它为应用程序提供了一种抽象的数据访问层。这个"10.1-Repository"项目很可能是为了演示或教学如何在.NET Core环境中实现这种模式。让我们深入探讨一下...

    google_m2repository_r22

    【标题】"google_m2repository_r22"是Google为Android开发者提供的一个重要的资源库,主要包含了一系列的库文件和依赖项,用于支持Android应用程序的开发。这个版本(r22)是特定时间点的更新,可能包含了那时的最新...

    Nexus Repository Manager OSS 2.14.21

    Nexus Repository OSS is distributed with Sencha Ext ...Nexus Repository Manager 2 OSS is a repository manager which can be freely used and is distributed under the Eclipse Public License (EPL Version 1).

    Pydroid repository plugin.xapk

    Pydroid repository plugin.xapk Pydroid repository plugin.xapkPydroid repository plugin.xapk

    repository.zip

    标题中的"repository.zip"指的是一个压缩包文件,其内容是一个Maven的本地仓库(.m2文件夹)。在Java开发中,Maven是一个强大的项目管理和依赖管理工具,它通过一个中央仓库来下载所需的库和框架,这个仓库就是我们...

    MVC中使用Repository模式vs2013

    一、在MVC中开发的时候,避免在Controllers中直接访问数据,为了构建更加适应未来变化以及更加易于测试的MVC应用程序,应使用Repository模式。当你使用Repository模式时,你会创建一个独立的repository类,它包含了...

    Laravel开发-model-repository

    在Laravel框架中,Model Repository是一种设计模式,用于在业务逻辑层和数据访问层之间创建一个抽象层。这种模式有助于保持代码的整洁和可维护性,使得应用的业务逻辑与数据库交互方式分离,从而实现更好的代码组织...

    Laravel开发-repository

    在Laravel框架中,Repository模式是一种设计模式,用于在业务逻辑层和数据访问层之间创建一个抽象层。这种模式的主要目的是提高代码的可测试性、可维护性和解耦度。"Laravel开发-repository CRCMS存储库扩展了...

Global site tag (gtag.js) - Google Analytics