很久之前就想系统的学习一下版本控制系统了, 这两天抽空看了一下《Progit》和《GotGithub》这两本书, 学习了 Git 这个版本控制系统以及只支持 Git 作为唯一版本库格式的项目托管平台 GitHub, 这两者在项目开发中都是很好的工具, 下面总结一下使用方法, 以方便以后使用时查阅.
添加 SSH 认证
生成 SSH 密钥
在 CODE 上查看代码片派生到我的代码片
01.$ SSH-keygen
02.Generating public/private rsa key pair.
- 03.Enter file in which to save the key (/Users/schacon/.SSH/id_rsa):
- 04.Enter passphrase (empty for no passphrase):
- 05.Enter same passphrase again:
- 06.Your identification has been saved in /Users/schacon/.SSH/id_rsa.
- 07.Your public key has been saved in /Users/schacon/.SSH/id_rsa.pub.
- 08.The key fingerprint is:
- 09.43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local
添加公钥到 GitHub
在 CODE 上查看代码片派生到我的代码片
- 01.$ cat ~/.SSH/id_rsa.pub
- 02.SSH-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
- 03.GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
- 04.Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
- 05.t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
- 06.mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
- 07.NrRFi9wrf+M7Q== schacon@agadorlaptop.local
SSH 认证
在 CODE 上查看代码片派生到我的代码片
01.$ SSH -T Git@GitHub.com
02.Hi gotgithub! You've successfully authenticated, but GitHub does not provide shell access.
出现上面的提示则认证成功, 以后就可以使用 SSH 协议 (自动认证, 不用输入口令) 克隆远程仓库了.
配置用户信息
配置全局信息
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git config --global user.name "John Doe"
- 02.$ Git config --global user.email johndoe@example.com
配置本地仓库局部信息
首先进入当前仓库工作区
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git config user.name "John Doe"
- 02.$ Git config user.email johndoe@example.com
查看当前配置信息
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git config --list
- 02.user.name=Scott Chacon
- 03.user.email=schacon@gmail.com
- 04.color.status=auto
- 05.color.branch=auto
- 06.color.interactive=auto
- 07.color.diff=auto
- 08....
- 10.$ Git config user.name
- 11.Scott Chacon
本地仓库常用命令
初始化
本地初始化:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git init
- 02.$ Git commit -m 'initial project version
- 03.$ Git remote add origin Git@GitHub.com:gotgithub/helloworld.Git
- 04.$ Git push -u origin master
或者从远程仓库克隆:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git clone Git://GitHub.com/schacon/grit.Git
- 02.$ Git clone Git://GitHub.com/schacon/grit.Git mygrit
检查当前文件状态
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git status
- 02.# On branch master
- 03.nothing to commit (working directory clean)
添加文件到缓存区
在 CODE 上查看代码片派生到我的代码片
- 01.$ VIM README
- 02.$ Git status
- 03.# On branch master
- 04.# Untracked files:
- 05.#
- 06.(use "git add <file>..." to include in what will be committed)
- 07.#
- 08.# README
- 09.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
- #
忽略某些文件
在 CODE 上查看代码片派生到我的代码片
- 01.$ cat .gitignore
- 02..[oa]
- 03.~
修改. gitignore 文件即可.
查看更新具体内容
查看工作目录中当前文件和缓存区域快照之间的差异:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git diff
- 02.diff --Git a/benchmarks.rb b/benchmarks.rb
- 03.index 3cb747f..da65585 100644
- 04.--- a/benchmarks.rb
- 05.+++ b/benchmarks.rb
- 06.@@ -36,6 +36,10 @@ def main
- 07.@commit.parents[0].parents[0].parents[0]
- 08.end
- 09.+
- run_code(x, 'commits 1') do
- +
- Git.commits.size
- +
- end
- +
- run_code(x, 'commits 2') do
- log = Git.commits('master', 15)
- log.size
查看缓存区文件和上次提交时的快照之间的差异:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git diff --cached
- 02.diff --Git a/README b/README
- 03.new file mode 100644
- 04.index 0000000..03902a1
- 05.--- /dev/null
- 06.+++ b/README2
- 07.@@ -0,0 +1,5 @@
- 08.+grit
- 09.+ by Tom Preston-Werner, Chris Wanstrath
- + http://github.com/mojombo/grit
- +
- +Grit is a Ruby library for extracting information from a Git repository
提交更新到本地仓库
从缓存区提交:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git commit -m "Story 182: Fix benchmarks for speed"
- 02.[master]: created 463dc4f: "Fix benchmarks for speed"
- 03.2 files changed, 3 insertion
从工作区提交:
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git commit -a -m 'added new benchmarks'
- 02.[master 83e38c7] added new benchmarks
- 03.1 files changed, 5 insertions(+), 0 deletions(-)
移除文件
在 CODE 上查看代码片派生到我的代码片
- 01.$ rm grit.gemspec
- 02.$ Git status
- 03.# On branch master
- 04.#
- 05.# Changed but not updated:
- 06.#
- 07.(use "git add/rm <file>..." to update what will be committed)
- 08.#
- 09.#
- 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
- #
直接从缓存区移除文件:
在 CODE 上查看代码片派生到我的代码片
01.$ Git rm --cached readme.txt
移动文件
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git mv src/main.cpp ./main.cpp
- 02.$ Git status
- 03.# On branch master
- 04.# Your branch is ahead of 'origin/master' by 3 commits.
- 05.#
- 06.# Changes to be committed:
- 07.# (use "git reset HEAD <file>..." to unstage)
- 08.#
- 09.# renamed: src/main.cpp -> main.cpp
- #
查看提交历史
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git log
- 02.commit ca82a6dff817ec66f44342007202690a93763949
- 03.Author: Scott Chacon <schacon@gee-mail.com>
- 04.Date:
- 05.Mon Mar 17 21:52:11 2008 -0700
- 06.changed the verison number
- 07.commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
- 08.Author: Scott Chacon <schacon@gee-mail.com>
- 09.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 上去看比较清楚.
覆盖上一次提交
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git commit -m 'initial commit'
- 02.$ Git add forgotten_file
- 03.$ Git commit --amend
新增的文件并入到上次提交的内容一起提交, 覆盖上次提交.
取消缓存区中的文件到工作区
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git reset HEAD benchmarks.rb
- 02.benchmarks.rb: locally modified
- 03.$ Git status
- 04.# On branch master
- 05.# Changes to be committed:
- 06.#
- 07.(use "git reset HEAD <file>..." to unstage)
- 08.#
- 09.#
- 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
新建以及切换分支
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git branch testing #新建分支
- 03.$ Git checkout testing #切换分支
- 05.$ Git checkout -b testing #新建并切换分支
合并分支
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git checkout master
- 02.$ Git merge hotfix
- 03.Updating f42c576..3a0874c
- 04.Fast forward
- 05.README |
- 06.1 -
- 07.1 files changed, 0 insertions(+), 1 deletions(-)
删除分支
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git branch -d hotfix
- 02.Deleted branch hotfix (3a0874c).
- 04.# 强制删除未合并的分支
- 06.$ Git branch -D mybranch1
- 07.Deleted branch mybranch1 (was f46a284).
分支管理
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git branch iss53
- 02.* master
- 03.testing
- 05.# 查看哪些分支已被并入当前分支
- 06.$ Git branch --merged
- 07.iss53
- 08.* master
- 10.# 查看尚未合并的分支
- 11.$ Git branch --no-merged testing
与远程仓库交互
克隆仓库到本地
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git clone Git://GitHub.com/schacon/ticgit.Git
- 02.Initialized empty Git repository in /private/tmp/ticgit/.Git/
03.remote: Counting objects: 595, done.
- 04.remote: Compressing objects: 100% (269/269), done.
- 05.remote: Total 595 (delta 255), reused 589 (delta 253)
- 06.Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.
- 07.Resolving deltas: 100% (255/255), done.
查看当前远程库
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git remote
- 02.origin
- 03.$ Git remote -v
- 04.origin Git://GitHub.com/schacon/ticgit.Git
- 05.$ Git remote -v
- 06.bakkdoor Git://GitHub.com/bakkdoor/grit.Git
- 07.cho45 Git://GitHub.com/cho45/grit.Git
- 08.defunkt Git://GitHub.com/defunkt/grit.Git
- 09.koke Git://GitHub.com/koke/grit.Git
- origin Git@GitHub.com:mojombo/grit.Git
添加远程仓库
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git remote
- 02.origin
- 03.$ Git remote add pb Git://GitHub.com/paulboone/ticgit.Git
- 04.$ Git remote -v
- 05.origin Git://GitHub.com/schacon/ticgit.Git
- 06.pb Git://GitHub.com/paulboone/ticgit.Git
从远程仓库抓取数据
在 CODE 上查看代码片派生到我的代码片
01.$ Git fetch [remote-name]
推送数据到远程仓库
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git push [remote-name] [branch-name]
- 03.$ Git push origin master
05.$ Git push [远程名] [本地分支]:[远程分支]
07.$ Git push [远程名] :[远程分支] #删除远程分支
查看远程仓库信息
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git remote show [remote-name]
- 03.$ Git remote show origin
- 04.* remote origin
- 05.URL: Git://GitHub.com/schacon/ticgit.Git
- 06.Remote branch merged with 'git pull' while on branch master
- 07.master
- 08.Tracked remote branches
- 09.master
- 10.ticgit
远程仓库的删除和重命名
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git remote rename pb paul
- 02.$ Git remote
- 03.origin
- 04.paul
- 06.$ Git remote rm paul
- 07.$ Git remote
- 08.origin
在远程仓库中创建分支
将 mybranch1 分支推送到远程仓库中, 在远程仓库中新建分支, 并添加追踪
在 CODE 上查看代码片派生到我的代码片
01.$ Git push -u origin mybranch1
02.Counting objects: 4, done.
03.Delta compression using up to 2 threads.
- 04.Compressing objects: 100% (2/2), done.
- 05.Writing objects: 100% (3/3), 281 bytes, done.
- 06.Total 3 (delta 0), reused 0 (delta 0)
- 07.To Git@GitHub.com:gotgithub/helloworld.Git
- [new branch] mybranch1 -> mybranch1
09.Branch mybranch1 set up to track remote branch mybranch1 from origin.
Tag 管理
tag 的创建
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git tag <tagname> [<commit>]
- 03.$ Git tag -m "Tag on initial commit" mytag1 HEAD^
- 04.$ Git tag -m "Tag on new commit" mytag2
查看 tag
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git tag -l -n1
- 02.mytag1 Tag on initial commit
- 03.mytag2 Tag on new commit
将本地 tag 推送到远程仓库中
在 CODE 上查看代码片派生到我的代码片
01.$ Git push origin refs/tags/*
02.Counting objects: 6, done.
03.Delta compression using up to 2 threads.
- 04.Compressing objects: 100% (4/4), done.
- 05.Writing objects: 100% (5/5), 548 bytes, done.
- 06.Total 5 (delta 0), reused 0 (delta 0)
- 07.To Git@GitHub.com:gotgithub/helloworld.Git
- [new tag] mytag1 -> mytag1
- [new tag] mytag2 -> mytag2
删除本地 tag
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git tag -d mytag3
- 02.Deleted tag 'mytag3' (was c71231c)
删除远程仓库中的 tag
在 CODE 上查看代码片派生到我的代码片
- 01.$ Git push origin :mytag3
- 02.To Git@GitHub.com:gotgithub/helloworld.Git
- [deleted] mytag3
来源: http://www.qdfuns.com/article/15019/32b0d342bd66e1ac910061a643c5fca6.html