Git 快速参考 (git fast tutorial)

本文提供了一个简洁实用的Git教程,涵盖了版本库创建、分支管理、历史勘查、变更制作、合并操作、变更共享以及版本库维护等内容。通过命令行示例介绍了如何使用Git进行版本控制,适合初学者和有一定经验的开发者参考。

http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html

转自

http://blog.youkuaiyun.com/zjujoe/archive/2009/05/28/4221864.aspx

 

1. 创建一个新的版本库

从一个压缩包中创建:

$ tar xzf project.tar.gz
$ cd project
$ git init #Initialized empty Git repository in .git/
$ git add .
$ git commit

从远程版本库创建:

$ git clone git://example.com/pub/project.git
$ cd project

2. 管理分支

$ git branch         # list all local branches in this repo
$ git checkout test  # switch working directory to branch "test"
$ git branch new     # create branch "new" starting at current HEAD
$ git branch -d new  # delete branch "new"

创建一个不以当前的 HEAD 为起点的分支,用:

$ git branch new test    # branch named "test"
$ git branch new v2.6.15 # tag named v2.6.15
$ git branch new HEAD^   # commit before the most recent
$ git branch new HEAD^^  # commit before that
$ git branch new test~10 # ten commits before tip of branch "test"

创建并同时切换至新的分支:

$ git checkout -b new v2.6.15

更新和检验从远程版本库中克隆过来的分支:

$ git fetch             # update
$ git branch -r         # list
  origin/master
  origin/next
  ...
$ git checkout -b masterwork origin/master

从不同的版本库中抓取分支,并给予一个在你的版本库中新的分支名称:

$ git fetch git://example.com/project.git theirbranch:mybranch
$ git fetch git://example.com/project.git v2.6.15:mybranch

给你要定期地协同工作的版本库制作一个列表:

$ git remote add example git://example.com/project.git
$ git remote                    # list remote repositories
example
origin
$ git remote show example       # get details
* remote example
  URL: git://example.com/project.git
  Tracked remote branches
    master
    next
    ...
$ git fetch example             # update branches from example
$ git branch -r                 # list all remote branches

3. 勘查历史

$ gitk                      # visualize and browse history
$ git log                   # list all commits
$ git log src/              # ...modifying src/
$ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
$ git log master..test      # ...in branch test, not in branch master
$ git log test..master      # ...in branch master, but not in test
$ git log test...master     # ...in one branch, not in both
$ git log -S'foo()'         # ...where difference contain "foo()"
$ git log --since="2 weeks ago"
$ git log -p                # show patches as well
$ git show                  # most recent commit
$ git diff v2.6.15..v2.6.16 # diff between two tagged versions
$ git diff v2.6.15..HEAD    # diff with current head
$ git grep "foo()"          # search working directory for "foo()"
$ git grep v2.6.15 "foo()"  # search old tree for "foo()"
$ git show v2.6.15:a.txt    # look at old version of a.txt

查找撤退点:

$ git bisect start
$ git bisect bad                # current version is bad
$ git bisect good v2.6.13-rc2   # last known good revision
Bisecting: 675 revisions left to test after this
                                # test here, then:
$ git bisect good               # if this revision is good, or
$ git bisect bad                # if this revision is bad.
                                # repeat until done.

4. 制作变更

配置 git

vi ~/.gitconfig

[user]
    name = Phoenix
    email = phoenixtoday@gmail.com
[alias]
  co = checkout
  ci = commit -a
  st = status
  br = branch
  oneline = log --pretty=oneline --since='2 days ago'
  onelog = log -p -1
[color]
  status = auto
  branch = auto
  ui = auto

选择这下次提交的时候要包含那些文件,接着制作交付:

$ git add a.txt    # updated file
$ git add b.txt    # new file
$ git rm c.txt     # old file
$ git commit

或者是准备提交和创建交付一步完成:

$ git commit d.txt # use latest content only of d.txt
$ git commit -a    # use latest content of all tracked files

5. 合并

$ git merge test   # merge branch "test" into the current branch
$ git pull git://example.com/project.git master
                   # fetch and merge in remote branch
$ git pull . test  # equivalent to git merge test

6. 共享你的变更

引入或者导出补丁:

$ git format-patch origin..HEAD # format a patch for each commit
                                # in HEAD but not in origin
$ git am mbox # import patches from the mailbox "mbox"

抓取一个不同的 git 版本库的分支,并合并进当前分支:

$ git pull git://example.com/project.git theirbranch

在合并至当前分支之前,将远程分支的变更保存为本地的分支:

$ git pull git://example.com/project.git theirbranch:mybranch

创建了本地分支的交付之后,用这些交付更新远程分支。

$ git push ssh://example.com/project.git mybranch:theirbranch

当本地和远程分支都是叫 "test" 时:

$ git push ssh://example.com/project.git test

对于经常通讯的远程版本库,有快捷命令的版本:

$ git remote add example ssh://example.com/project.git
$ git push example test

7. 版本库的维护

检查损坏:

$ git fsck

重新打包,删除无用的杂物:

$ git gc

 

8. 其它

忽略某些文件及目录

$ vi .gitignore

可以使用通配符,目录最后不要加斜杠。

 

丢弃当前所有未提交内容

$ git reset --hard HEAD

 

有未提交内容时快速切换到另一分支

git stash save "work in progress for foo feature"
切换到另一分支,并修改、提交
切换回原来分支
git stash apply
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值