git 快速参考手册

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
 
http://blog.csdn.net/zjujoe/article/details/4221864
 

Command Alias(快捷命令设置)

git config --global alias.st status git config --global alias.ci commit git config --global alias.co checkout git config --global alias.br branch git config --global alias.dc dcommit git config --global alias.rb rebase

Interactive Color(打开交互色彩)

git config --global color.branch auto git config --global color.diff auto git config --global color.interactive auto git config --global color.status auto

User Information(设置用户信息)

git config --global user.name "jianingy" git config --global user.email "detrox@gmail.com"

Clone New Project(克隆新项目)

git clone git://gitorious.org/bamboo/mainline.git bamboo

Setup Remote Repository(设置远程仓库)

添加(origin 为一个标示,可以随意更换)  git remote add origin git@gitorious.org:bamboo/mainline.git # 删除  git remote remove origin

Operations on Remote Repository(操作远程仓库)

提交本地修改(将本地修改提交到远程的master分支  git push origin master # 合并远程修改(将远程的master分支合并进来  git pull origin master # 删除远程仓库里的分枝  git push :branch

Basic Operations(基本操作)

提交修改  git add /path/to/file git commit -m reason # 提交全部修改  git commit -a -m reason # 创建本地分枝  git co -b branch_name # 查看分枝  git branch # 删除分枝  git branch -D branch_name # 查看分支之间的差异  git diff master branch # 查看最新版本和上一个版本的差异(一个^表示向前推进一个版本)  git diff HEAD HEAD ^ # 查看状态  git status # 合并分支  git pull . branch # 销毁自己的修改  git reset --hard

git-svn (与svn互操作)

从subversion仓库中克隆  git svn clone https://nlpbamboo.googlecode.com/svn --username detrox # 将本地修改提交到subversion仓库  git svn dcommit # 导入新的subversion更新  git svn rebase

 

http://blog.jianingy.com/2008/10/git-daily-commands-git.html


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值