准备学下git,才发现1年前就在电脑上装上git了。当时PySide的windows版尚未出来,用它下载了PySide的源码,后来因编译未果,竟然把这事忘了。
Git?
词典中解释: | git[ɡit]n.饭桶,无用的人 |
由此而来? | git: The stupid content tracker(傻瓜内容追踪器) |
简单点说: | Git 是一个由 Linus Torvalds 创建的源代码管理软件 |
官方说法: | Git 是一个快速、可扩展的分布式版本控制系统,它具有极为丰富的命令集, |
当我们讨论Git时,可能指代两种东西(后者是我们关心的):
-
内容追踪:git探索?本质?(git plumbing)
- git-fetch-pack, git-send-pack, git-hash-object等命令
- ...
-
源代码管理器:git外壳?(git porcelain)
- git commit 等命令 (git默认的外壳)
- 其他外壳: easy-git, yap, widgit, darcs-git.py 等
安装
- ubuntu下:
- 安装 git-core, gitk, git-gui 3个包
- windows下:
-
安装 msysgit 或 TortoiseGit(据说还不太完善,但很值得一试)
-
- 注:
-
gitkis a simple Tcl/Tk GUI for browsing history of Git repositories easily, distributed with Git.
-
git-guiis a tool for creating commits and managing branches. Written in Tcl/Tk. Stable versions are shipped with Core Git since version 1.5.
-
部分命令
最重要一个:
-
git help
创建仓库
本地创建 | gitinit |
克隆远端 | gitclone |
stage 操作
添加 | gitadd |
查看状态 | gitstatus |
查看差别 | gitdiff |
提交 | gitcommit |
unstage | gitreset HEAD |
移除 | gitrm |
分支(branch)操作
列出 | gitbranch |
创建 | gitbranchbranchname |
切换 | gitcheckoutbranchname |
删除 | gitbranch -dbranchname |
合并 | gitmergebranchname |
- 创建和切换可以合并成
git checkout -b branchname
远端仓库(remote repository)操作
管理 | gitremote[add rm ...] |
拉取内容 ↓ | gitfecthalias |
拉取内容(merge) ↓ | gitpullalias |
推送内容 ↑ | gitpushalias branch |
- pull 相当于 fetch 和 merge 两条命令
几条与svn对比的命令:
查看diff | git diff | svn diff | less |
查看diff | git diff rev path | svn diff -rrev path |
应用patch | gitapply | patch -p0 |
恢复 | gitcheckoutpath | svn revert path |
提交 | gitcommit -a | svn commit |
注意事项
- Google到的不少资料都是git 1.4之前的。所以要注意看官方的文档。
- git 的 master 分支 不同于 subversion 的 trunk!
It’s a common error to think of the master branch as being equivalent to Subversion’s trunk. However, a custom development branch is much closer in practice to the Subversion trunk, where experimental work is done.
- 不同于svn,分支不是单独的目录。需要自己跟踪各个分支?
- 每次commit时需要用户名和email。设置:
$ git config --global user.name 'Your Name' $ git config --global user.email you@somedomain.com
- git 有个以stage的概念,所有的东西提交之前需要加入到staged区域
-
可以使用 gitcommit -a来避免繁琐的添加
-
注意 gitreset HEAD的含义。只是 unstage !
-