git 使用入门篇

最近准备给同事培训git,发现了一个不错的资源,在这里:http://www.gitguys.com/topics/creating-a-shared-repository-users-sharing-the-repository/

原文如下,有空再译:

ommands 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 remote command 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 the master 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.

时间: 2024-11-24 21:24:26

git 使用入门篇的相关文章

GIT使用入门篇(管理自已的代码)

1.Git介绍 Git 是一款免费的.开源的.分布式的版本控制系统.旨在快速高效地处理无论规模大小的任何软件工程. 每一个 Git克隆 都是一个完整的文件库,含有全部历史记录和修订追踪能力,不依赖于网络连接或中心服务器.其最大特色就是“分支”及“合并”操作非常快速.简便. 2.Git和Svn的区别 SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器.集中式版本控

Git入门篇之环境搭建&基本功能的使用

网上关于GitHub的使用教程还是比较丰富,不过部分教程比较陈旧抑或写得不够详细,在我实践的过程中遇到了一些麻烦,记录下来,当是自己的一个总结吧,也供大家参考.欢迎留言交流. 本文主要讲解Windows Msysgit软件平台的使用,windows的GitHub for Windows方式和苹果系统的Github或git方式也在用,这方面网上的教程比较丰富,也就不做过多阐述了,欢迎留言讨论. Windows版本: 在windows中搭建Git环境使用Github有两种方式: 方式一: GitHu

玩转Git入门篇

最近项目使用到Git管理项目,所以就学习了一番,随然网上关于 Git的文章铺天盖地,我还是整理下总结下自己学习Git相关笔记,希望也能帮助到需要他的小伙伴们,O(∩_∩)O~ 简介 Git 是分布式版本控制和源代码管理系统,重点使用和管理代码的速度. Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发.Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件. Git官方网址是:https://git-scm.com/ Git的术语 Workspace:

微信小程序入门篇

微信小程序入门篇: 准备工作 IDE搭建 就不多说了,没有内测码去下载个破解版吧,我用了一下,学习完全够了!IDE破解版+安装教程 图片发自简书App 知识准备 JavaScrip还是要看看的,推荐教程 廖雪峰大神的博客 HTML+CSS 大概知道是干啥的就行 从零开始 微信小程序中就四种类型的文件 js ---------- JavaScrip文件 json -------- 项目配置文件,负责窗口颜色等等 wxml ------- 类似HTML文件 wxss ------- 类似CSS文件

基于Rebound制造绚丽的动画效果-入门篇

基于Rebound制造绚丽的动画效果-入门篇 Rebound是什么? Rebound是一个来自 Facebook 公司的 Java物理和动画库.Rebound spring 模型可用于创建动画,让你感觉很自然. Rebound的运作原理是什么? Rebound拥有两个参数:tension.friction. tension是张力,拉力. friction是摩擦力. 演示: tension:50,friction:1 rebound_t50_f1.gif 拉力为50时,摩擦为1.摩擦对拉力的损耗十

Erlang Rebar 使用指南之一:入门篇

Erlang Rebar 使用指南之一:入门篇 全文目录: https://github.com/rebar/rebar/wiki 本章原文: https://github.com/rebar/rebar/wiki/Getting-started Rebar 是功能丰富的 Erlang 构建工具.用于Erlang/OTP项目的编译,测试,依赖管理,打包发布等. Rebar 是自包含的脚本,可以方便地嵌入到项目中. 1 编译 rebar $ git clone git://github.com/r

git 小白入门( 三 ) —— 远程仓库

还不会基本操作的可以翻看上一篇 git 小白入门( 二 ) -- 入门操作 ____________________________________________________ 五:远程仓库. 在了解之前,先注册github账号,由于你的本地Git仓库和github仓库之间的传输是通过SSH加密的,所以需要一点设置: 第一步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果有的话,直接跳过此如下命令,

Git原理入门解析

前言: 之前听过公司大佬分享过 Git 原理之后就想来自己总结一下,最近一忙起来就拖得久了,本来想塞更多的干货,但是不喜欢拖太久,所以先出一版足够入门的: 一.Git 简介 Git 是当前流行的分布式版本控制管理工具,最初由 Linux Torvalds (Linux 之父) 创造,于 2005 年发布. Git,这个词其实源自英国俚语,意思大约是 “混账”.Linux 为什么会以这样自嘲的名字来命名呢?这其中还有一段儿有趣的历史可以说一说: 以下摘自:https://www.liaoxuefe

《Java从入门到放弃》入门篇:springMVC数据校验

昨天我们扯完了数据传递,今天我们来聊聊数据校验的问题.来,跟着我一起读:计一噢叫,一按艳. 在springMVC中校验数据也非常简单,spring3.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. Spring的DataBinder在进行数据绑定时,会同时调用校验框架完成数据校验工作. 具体使用步骤如下: 1)导入数据校验的JAR包 2)在springmvc的配置文件中添加校验Bean 3)修改实体类,在属性上加上校验的注解 4)修改昨天的login4方法,加上校验的相关代码