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.
git 使用入门篇的更多相关文章
- Git快速入门进阶篇
本文接着Git快速入门篇,继续探讨Git在管理项目中的一些应用. 远程仓库的使用 查看远程仓库 查看你已经配置的远程仓库服务器,可以运行 git remote 命令.指定选项 -v,会显示需要读写远程 ...
- GIT入门篇-基本概念与操作
GIT 首先必须说明的是, 这篇文章不是阐述GIT原理性和比较深入的文章.只是对于日常开发中比较常用的需求的总结和GIT这些命令大体的原理解释.所以掌握这个只能说能够应付一定的开发需求.但是如果你是个 ...
- .NET Core实战项目之CMS 第四章 入门篇-Git的快速入门及实战演练
写在前面 上篇文章我带着大家通过分析了一遍ASP.NET Core的源码了解了它的启动过程,然后又带着大家熟悉了一遍配置文件的加载方式,最后引出了依赖注入以及控制反转的概念!如果大家把前面几张都理解了 ...
- Git【入门】这一篇就够了
前言 欢迎关注公众号,白嫖原创PDF,也可以催更,微信搜:JavaPub,回复:[666] Git 在生产工作中是使用频率很高的工具,但我发现很多文章只是对它做了简单的提交命令说明,真正遇到 版本冲突 ...
- Git轻松入门2:分支篇
什么是分支 在玩剧情类游戏时,不同的选择会触发不同的剧情路线,每条剧情路线都会独立发展,最终走向不同的结局. Git中所谓的"分支(branch)"就如同游戏中的剧情路线,用户可以 ...
- 玩转Git入门篇
最近项目使用到Git管理项目,所以就学习了一番,随然网上关于 Git的文章铺天盖地,我还是整理下总结下自己学习Git相关笔记,希望也能帮助到需要他的小伙伴们,O(∩_∩)O~ 简介 Git 是分布式版 ...
- 服务端技术进阶(八)GitHub入门篇
服务端技术进阶(八)GitHub入门篇 前言 在投递简历的过程中,发现有的公司会要求填写自己的GitHub地址,而自己却还没有GitHub帐号,准确点说是自己还不太会使用GitHub.(貌似开源社区中 ...
- Erlang Rebar 使用指南之一:入门篇
Erlang Rebar 使用指南之一:入门篇 全文目录: https://github.com/rebar/rebar/wiki 本章原文: https://github.com/rebar/reb ...
- .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...
随机推荐
- js正则,电话,邮箱
1. <script type="text/javascript"> var str="Is this all th05777-89856825ere is5 ...
- Orchard源码分析(5.2):BeginRequest事件处理(DefaultOrchardHost.BeginRequest方法)
BeginRequest事件处理的作用是确保所有Shell已经加载,或者在扩展有变化的时候重新加载. void IOrchardHost .BeginRequest() { ...
- JSTL I18N 格式标签库
<%@ page language="java" pageEncoding="gbk"%> <%@ taglib prefix="c ...
- 清除SQL server2008 记住的用户名和密码
删除以下文件即可: C:\Users\%username%\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStud ...
- [Json.net]忽略不需要的字段
摘要 在序列化对象,总会遇到一些敏感的信息,这些信息,并不想对调用接口的用户暴露出来,又或者移动端调用接口的时候,为了不返回没用的信息占用流量,这个时候也需要把一些信息给过滤掉. 系列文章 [Json ...
- jq实现点击弹出框代码
废话不多说,先贴代码吧 <script> function showBg() { //定义 showBg 函数 var bh = $("body").height(); ...
- hdu 4960 Another OCD Patient (最短路 解法
http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...
- seajs的那点事(很坑的事),和本白的一点事(更坑的事)
在开始之前,偶先吐槽加逗比一下,2天前,CCAV的本白和百度的菊花成功潜入到了携程大楼 然后在没有找到他们运维的情况下,四处乱逛,企图把他们的服务器给root一下,然后再瞎逛之后到了一个很神奇的地方 ...
- 记录在xx公司被考核的15天及自己的感想
在大学有两件事让我很遗憾. 第一:在2013年7月我和自己的前任女朋友分手,这是两年前的事了,我们谈了七个月. 第二:在2015年4月我被xx公司淘汰了,正如我的前任女朋友是我遇到的最好女孩,这家公司 ...
- jquery动态改变my97日期格式
$('#qsrq').unbind('focus'); $('#zzrq').unbind('focus'); $('#qsrq').bind('focus', function () { Wdate ...