关闭

Git及Github使用方法总结

标签: githubgit版本控制系统使用方法总结
6151人阅读 评论(0) 收藏 举报
分类:

    很久之前就想系统的学习一下版本控制系统了,这两天抽空看了一下《Progit》和《GotGithub》这两本书,学习了Git这个版本控制系统以及只支持Git作为唯一版本库格式的项目托管平台Github,这两者在项目开发中都是很好的工具,下面总结一下使用方法,以方便以后使用时查阅。


原理

下面的两张图片很好的解释了Git和Github的原理:


分布式版本控制系统


Github的协同模式


添加SSH认证

1. 生成SSH密钥

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/schacon/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/schacon/.ssh/id_rsa.
Your public key has been saved in /Users/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local


2. 添加公钥到Github

$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== schacon@agadorlaptop.local

3. SSH认证

$ ssh -T git@github.com
Hi gotgithub! You've successfully authenticated, but GitHub does not provide shell access.
出现上面的提示则认证成功,以后就可以使用ssh协议(自动认证,不用输入口令)克隆远程仓库了。



配置用户信息

1. 配置全局信息

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com 

2. 配置本地仓库局部信息

首先进入当前仓库工作区

$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

3. 查看当前配置信息

$ git config --list
user.name=Scott Chacon
user.email=schacon@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

$ git config user.name
Scott Chacon



本地仓库常用命令

1. 初始化

本地初始化:

$ git init
$ git commit -m 'initial project version
$ git remote add origin git@github.com:gotgithub/helloworld.git
$ git push -u origin master 
或者从远程仓库克隆:
$ git clone git://github.com/schacon/grit.git
$ git clone git://github.com/schacon/grit.git mygrit

2. 检查当前文件状态

$ git status
# On branch master
nothing to commit (working directory clean)

3. 添加文件到缓存区

$ vim README
$ git status
# On branch master
# Untracked files:
#
(use "git add <file>..." to include in what will be committed)
#
# README
nothing added to commit but untracked files present (use "git add" to track)
$
$ git add README
$ git status
# On branch master
# Changes to be committed:
#
(use "git reset HEAD <file>..." to unstage)
#
# new file:   README
#

4. 忽略某些文件

$ cat .gitignore
*.[oa]
*~
修改.gitignore文件即可。


5. 查看更新具体内容

查看工作目录中当前文件和缓存区域快照之间的差异:

$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..da65585 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
@commit.parents[0].parents[0].parents[0]
end
+
run_code(x, 'commits 1') do
+
git.commits.size
+
end
+
run_code(x, 'commits 2') do
log = git.commits('master', 15)
log.size

查看缓存区文件和上次提交时的快照之间的差异:
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README2
@@ -0,0 +1,5 @@
+grit
+ by Tom Preston-Werner, Chris Wanstrath
+ http://github.com/mojombo/grit
+
+Grit is a Ruby library for extracting information from a Git repository


6. 提交更新到本地仓库

从缓存区提交:

$ git commit -m "Story 182: Fix benchmarks for speed"
[master]: created 463dc4f: "Fix benchmarks for speed"
2 files changed, 3 insertion


从工作区提交:
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 files changed, 5 insertions(+), 0 deletions(-)


7. 移除文件
$ rm grit.gemspec
$ git status
# On branch master
#
# Changed but not updated:
#
(use "git add/rm <file>..." to update what will be committed)
#
#
deleted:   grit.gemspec
#

$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
# On branch master
#
# Changes to be committed:
#
(use "git reset HEAD <file>..." to unstage)
#
#
deleted:   grit.gemspec
#

直接从缓存区移除文件:
$ git rm --cached readme.txt

8. 移动文件

$ git mv src/main.cpp ./main.cpp
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    src/main.cpp -> main.cpp
#


9. 查看提交历史
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the verison number
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:
Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test code
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date:
Sat Mar 15 10:31:28 2008 -0700
first commit
如果使用Github托管项目的话到Github上去看比较清楚。


10. 覆盖上一次提交

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
新增的文件并入到上次提交的内容一起提交,覆盖上次提交。

11. 取消缓存区中的文件到工作区

$ git reset HEAD benchmarks.rb
benchmarks.rb: locally modified
$ git status
# On branch master
# Changes to be committed:
#
(use "git reset HEAD <file>..." to unstage)
#
#
modified:   README.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#
#
modified:   benchmarks.rb

12. 新建以及切换分支

$ git branch testing        #新建分支

$ git checkout testing      #切换分支

$ git checkout -b testing   #新建并切换分支


13. 合并分支

$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast forward
README |
1 -
1 files changed, 0 insertions(+), 1 deletions(-)

14. 删除分支

$ git branch -d hotfix
Deleted branch hotfix (3a0874c).

#强制删除未合并的分支

$ git branch -D mybranch1
Deleted branch mybranch1 (was f46a284). 


15. 分支管理

$ git branch iss53 
* master 
testing 

#查看哪些分支已被并入当前分支 
$ git branch --merged 
iss53 
* master 

#查看尚未合并的分支 
$ git branch --no-merged testing 



与远程仓库交互

1. 克隆仓库到本地

$ git clone git://github.com/schacon/ticgit.git
Initialized empty Git repository in /private/tmp/ticgit/.git/
remote: Counting objects: 595, done.
remote: Compressing objects: 100% (269/269), done.
remote: Total 595 (delta 255), reused 589 (delta 253)
Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.
Resolving deltas: 100% (255/255), done.

2. 查看当前远程库

$ git remote
origin
$ git remote -v
origin git://github.com/schacon/ticgit.git
$ git remote -v
bakkdoor git://github.com/bakkdoor/grit.git
cho45 git://github.com/cho45/grit.git
defunkt git://github.com/defunkt/grit.git
koke git://github.com/koke/grit.git
origin git@github.com:mojombo/grit.git


3. 添加远程仓库

$ git remote
origin
$ git remote add pb git://github.com/paulboone/ticgit.git
$ git remote -v
origin git://github.com/schacon/ticgit.git
pb git://github.com/paulboone/ticgit.git


4. 从远程仓库抓取数据

$ git fetch [remote-name]


5. 推送数据到远程仓库

$ git push [remote-name] [branch-name]

$ git push origin master

$ git push [远程名] [本地分支]:[远程分支]

$ git push [远程名] :[远程分支]       #删除远程分支


6. 查看远程仓库信息

$ git remote show [remote-name]

$ git remote show origin
* remote origin
URL: git://github.com/schacon/ticgit.git
Remote branch merged with 'git pull' while on branch master
master
Tracked remote branches
master
ticgit


7. 远程仓库的删除和重命名

$ git remote rename pb paul
$ git remote
origin
paul

$ git remote rm paul
$ git remote
origin


8. 在远程仓库中创建分支

将mybranch1分支推送到远程仓库中,在远程仓库中新建分支,并添加追踪

$ git push -u origin mybranch1
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:gotgithub/helloworld.git
 * [new branch]      mybranch1 -> mybranch1
Branch mybranch1 set up to track remote branch mybranch1 from origin.



Tag管理

1. tag的创建

$ git tag <tagname> [<commit>]

$ git tag -m "Tag on initial commit" mytag1 HEAD^
$ git tag -m "Tag on new commit"     mytag2


2. 查看tag

$ git tag -l -n1
mytag1          Tag on initial commit
mytag2          Tag on new commit


3. 将本地tag推送到远程仓库中

$ git push origin refs/tags/*
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 548 bytes, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@github.com:gotgithub/helloworld.git
 * [new tag]         mytag1 -> mytag1
 * [new tag]         mytag2 -> mytag2

4. 删除本地tag

$ git tag -d mytag3
Deleted tag 'mytag3' (was c71231c)

5. 删除远程仓库中的tag

$ git push origin :mytag3
To git@github.com:gotgithub/helloworld.git
  [deleted]         mytag3



0
0
查看评论

Git和GitHub原理讲解及区别

作者庄淑芹博士,是台湾著名的妇产科专家。她提出,女性在一生中有三次机会可以调整自己的体型,使之恢复青春、美丽与健康,那就是初潮期、生产后、更年期。这里最重要的就是生产后这个阶段。这个时期,你的身体就好像一个打开的大门,可以把怀孕时积累的多余的积水和毒素(这也是产后发胖的主要原因)排出体外,然后通过正...
  • lifuxiangcaohui
  • lifuxiangcaohui
  • 2013-12-09 09:24
  • 3418

git教程 - 概念 原理 使用

一些名称 git ---- 版本控制工具。 git是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。Torvalds开始着手开发git是为了作为一种过渡方案来替代BitKeeper,后者之前一直是Linux内核开发人员在全球使用的主要源代码工具。开放源码...
  • chenj_freedom
  • chenj_freedom
  • 2016-01-19 16:50
  • 12867

gotgithub github中文说明书 chm格式 离线版 超详细,超简单,强烈推荐

  • 2013-07-02 15:44
  • 19.46MB
  • 下载

YYKit系列之——YYModel使用(摘自GitHub)

1 转换 json 到 model User *user = [User yy_modelWithJSON:json]; 2转换model 到 json NSDictionary *json = [user yy_modelToJSONObject];
  • muzhenhua
  • muzhenhua
  • 2015-12-15 20:22
  • 19739

Git和github使用方法总结

Git是一个版本控制系统,用于跟踪文件随时间变化状态的一款软件。更具体的说,git是一个分布式的版本控制系统,在git中参与项目的每程序员不仅能拥有文件的当前状态,还能拥有项目的完整历史记录。
  • u010111016
  • u010111016
  • 2016-04-29 11:43
  • 1996

git和github菜鸟使用步骤

git版本控制器安装
  • u010367506
  • u010367506
  • 2014-05-14 22:55
  • 2976

GitHub学习总结

花了半天的功夫,终于把github这个东西搞秦楚了,算是入了门,以后再慢慢学习实践,不要想一口吃成个大胖子,先入门,随之时间的向前推进,你是理解和使用熟练度会有很大的提高 的。 ======== 为了更好地进行团队开发,所以写了这篇短文,简略介绍了一下 git 的使用(主要在 Win...
  • u013467442
  • u013467442
  • 2015-06-13 11:09
  • 1137

Linux下Git和GitHub使用方法总结

1 Linux下Git和GitHub环境的搭建     第一步: 安装Git,使用命令 “sudo apt-get install git”     第二步: 创建GitHub帐号     第三步: 生成ssh key,使用命令 “ssh-...
  • cgl1079743846
  • cgl1079743846
  • 2014-02-25 20:24
  • 14780

git和github,oschina用法

(1)安装git,打开git bash (2)身份验证 git config --global user.name "用户名" git config -global user.email "邮箱" (3)在本地建立版本库(oschina叫项目)(其实就是创建一...
  • xingxinmanong
  • xingxinmanong
  • 2016-07-01 08:40
  • 1312

Github使用方法 (包含本地上传)

图文介绍本地VS远程关系 参照:一.远程初始步骤 (1)github 帐户注册 — 也就是帐户名,密码 github:https://github.com 若是不放心注册流程参照:http://jingyan.baidu.com/article/455a9950abe0ada...
  • CL18652469346
  • CL18652469346
  • 2016-12-21 10:32
  • 439
    个人资料
    • 访问:804293次
    • 积分:5591
    • 等级:
    • 排名:第5646名
    • 原创:82篇
    • 转载:44篇
    • 译文:1篇
    • 评论:101条
    最新评论